PB5.2 : set windows transparency (cross-platform)

Share your advanced PureBasic knowledge/code with the community.
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

PB5.2 : set windows transparency (cross-platform)

Post by eddy »

This function makes your window transparent.
It returns #TRUE if windows opacity has been changed.

I aggregate some past tips into a function :arrow: source1 + source2

@Fred
It could be a futur PB command :wink:

Code: Select all

; PB 5.2 - set windows transparency (Mac,Linux,Windows)
CompilerIf #PB_Compiler_OS=#PB_OS_Linux
   ImportC "-gtk"
      gtk_window_set_opacity(*Window.i, Opacity.d);
      gtk_widget_is_composited(*Widget)
   EndImport
   
   Procedure.i SetWindowTransparency(Window, Transparency=255)
      Protected *windowID=WindowID(Window), alpha.d=Transparency/255.0
      If Transparency>=0 And Transparency<=255
         If gtk_widget_is_composited(*windowID)
            gtk_window_set_opacity(*windowID, alpha.d)
            ProcedureReturn #True
         EndIf
      EndIf
   EndProcedure
CompilerElseIf #PB_Compiler_OS=#PB_OS_MacOS
   Procedure.i SetWindowTransparency(Window, Transparency=255)
      Protected *windowID=WindowID(Window), alpha.CGFloat=Transparency/255.0
      If Transparency>=0 And Transparency<=255
         CocoaMessage(0, *windowID, "setOpaque:", #NO)
         If CocoaMessage(0, *windowID, "isOpaque")=#NO
            CocoaMessage(0, *windowID, "setAlphaValue:@", @alpha)
            ProcedureReturn #True
         EndIf
      EndIf
   EndProcedure
CompilerElseIf #PB_Compiler_OS=#PB_OS_Windows
   Procedure.i SetWindowTransparency(Window, Transparency=255)
      Protected *windowID=WindowID(Window), exStyle=GetWindowLongPtr_(*windowID, #GWL_EXSTYLE)
      If Transparency>=0 And Transparency<=255
         SetWindowLongPtr_(*windowID, #GWL_EXSTYLE, exStyle | #WS_EX_LAYERED)
         SetLayeredWindowAttributes_(*windowID, 0, Transparency, #LWA_ALPHA)
         
         ProcedureReturn #True
      EndIf
   EndProcedure
CompilerEndIf
Last edited by eddy on Sun Sep 01, 2013 11:02 am, edited 18 times in total.
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Re: set windows transparency (cross-platform)

Post by flaith »

:D Thanks eddy
“Fear is a reaction. Courage is a decision.” - WC
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Re: set windows transparency (cross-platform)

Post by eddy »

updated
- fix window specific code (remove one useless "if" ...)
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: set windows transparency (cross-platform)

Post by Little John »

Smart, thank you!

//edit:
Better use Procedure.i
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Re: set windows transparency (cross-platform)

Post by eddy »

Little John wrote:Smart, thank you!
//edit:
Better use Procedure.i
Updated
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Re: set windows transparency (cross-platform)

Post by eddy »

updated
- fix linux : gtk_window_set_opacity(*windowID, alpha.d)
- fix mac : alpha.CGFloat
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
Post Reply