Change transparency of window

Mac OSX specific forum
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Change transparency of window

Post by Shardik »

On MacOS X 10.0 and later it is very easy to change the transparency of your window:

Code: Select all

ImportC ""
  SetWindowAlpha(WindowRef.L, AlphaValue.F)
EndImport

OpenWindow(0, 200, 100, 300, 60, "Change window's transparency")
TrackBarGadget(0, 10, 10, 280, 20, 0, 100)
SetGadgetState(0, 0)
TextGadget(1, 15, 35, 40, 20, "0%")
TextGadget(2, 260, 35, 40, 20, "100%")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 0
        SetWindowAlpha(WindowID(0), 1.0 - GetGadgetState(0) / 100.0)
      EndIf
  EndSelect
ForEver
delikanli_19_82
User
User
Posts: 38
Joined: Tue Jun 21, 2011 6:11 pm

Re: Change transparency of window

Post by delikanli_19_82 »

very good solution. thank you shardik.

thx

kurt
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Change transparency of window

Post by WilliamL »

That's pretty neat but what would you use it for?
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
delikanli_19_82
User
User
Posts: 38
Joined: Tue Jun 21, 2011 6:11 pm

Re: Change transparency of window

Post by delikanli_19_82 »

i am working on an own cross-platform toolkit for purebasic. and the forms (windows) shell be able to transparent also.
vwidmer
Enthusiast
Enthusiast
Posts: 286
Joined: Mon Jan 20, 2014 6:32 pm

Re: Change transparency of window

Post by vwidmer »

Sorry if this is stupid but using the code posted above I am getting this error.

Code: Select all

Undefined symbols for architecture x86_64:
  "_SetWindowAlpha", referenced from:
      _Case1 in purebasic.o
      PStub_SetWindowAlpha in purebasic.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Am I doing something wrong? I am on a MBP runing 10.9.2
WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 456
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: Change transparency of window

Post by Mindphazer »

I think this code was using carbon, which is not supported anymore.

Maybe this could help : http://www.purebasic.fr/english/viewtop ... 52#p393352
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Change transparency of window

Post by Shardik »

Mindphaser is right. My code example runs only with the Carbon framework. At the time of my posting the only available framework was Carbon. With PB 5.00 the default framework changed to Cocoa. So to run my above code example you have to use PB 5.11 or older. In PB 5.00 to 5.11 you have to set the "Library Subsystem:" to "Carbon" in "Compiler/Compiler options...". In PB 4.61 or older the code example runs without anything to change (even on OS X 10.9.2 Mavericks).

Beginning with PB 5.20 the subsystem Carbon was removed...

For your conveniance I have ported the code example to Cocoa:

Code: Select all

Define Alpha.CGFloat

OpenWindow(0, 200, 100, 300, 60, "Change window's transparency")
TrackBarGadget(0, 10, 10, 280, 20, 0, 100)
SetGadgetState(0, 0)
TextGadget(1, 15, 35, 40, 20, "0%")
TextGadget(2, 260, 35, 40, 20, "100%")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 0
        Alpha = 1.0 - GetGadgetState(0) / 100.0
        CocoaMessage(0, WindowID(0), "setAlphaValue:@", @Alpha)
      EndIf
  EndSelect
ForEver
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 456
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: Change transparency of window

Post by Mindphazer »

Nice example Shardik

Thank you :-)
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Change transparency of window

Post by Shardik »

Mindphazer wrote:Nice example Shardik

Thank you :-)
You are welcome. :)


Here is an improved example which uses BindEvent() for a real time change of the transparency while dragging the trackbar knob:

Code: Select all

Procedure TrackBarChangedCallback()
  Protected Alpha.CGFloat = 1.0 - GetGadgetState(0) / 100.0
  CocoaMessage(0, WindowID(0), "setAlphaValue:@", @Alpha)
EndProcedure

OpenWindow(0, 200, 100, 300, 60, "Change window's transparency")
TrackBarGadget(0, 10, 10, 280, 20, 0, 100)
SetGadgetState(0, 0)
BindEvent(#PB_Event_Gadget, @TrackBarChangedCallback(), 0, 0)
TextGadget(1, 15, 35, 40, 20, "0%")
TextGadget(2, 260, 35, 40, 20, "100%")

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Post Reply