Transparent windows

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

Re: Transparent windows

Post by Shardik »

Joubarbe wrote:If you have a magic code for the three OS, that'd be perfect ! :)
This is a cross-platform example which I tested successfully with PB 5.31 on these operating systems:
- MacOS X 10.6.8 (Snow Leopard)
- Ubuntu 14.04 x64 with KDE (tested with GTK 2 and GTK 3, tested also with PB 5.40 Beta 3 and GTK 2 and GTK 3)
- Windows XP SP3
- Windows 7 SP1 x64

Code: Select all

ImportC ""
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      gtk_widget_is_composited(*Widget.GtkWidget)
      gtk_window_set_opacity(*Window.GtkWindow, Opacity.D)          
  CompilerEndSelect
EndImport

OpenWindow(0, 200, 100, 300, 60, "Change window's transparency",
  #PB_Window_SystemMenu | #PB_Window_Invisible)
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%")

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Linux
    If gtk_widget_is_composited(WindowID(0)) = #False
      MessageRequester("Info",
        "Sorry, transparency is not supported on this system!")
      End
    EndIf
  CompilerCase #PB_OS_MacOS
    Define Alpha.CGFloat
  CompilerCase #PB_OS_Windows
    SetWindowLongPtr_(WindowID(0), #GWL_EXSTYLE, #WS_EX_LAYERED)
    SetLayeredWindowAttributes_(WindowID(0), 0, 255, #LWA_ALPHA)
CompilerEndSelect

HideWindow(0, #False)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 0
        CompilerSelect #PB_Compiler_OS
          CompilerCase #PB_OS_Linux
            gtk_window_set_opacity(WindowID(0), 1.0 - GetGadgetState(0) / 100.0)
          CompilerCase #PB_OS_MacOS
            Alpha = 1.0 - GetGadgetState(0) / 100.0
            CocoaMessage(0, WindowID(0), "setAlphaValue:@", @Alpha)
          CompilerCase #PB_OS_Windows
            SetLayeredWindowAttributes_(WindowID(0), 0,
              Int(255 - GetGadgetState(0) / 100 * 255), #LWA_ALPHA)
        CompilerEndSelect
      EndIf
  EndSelect
ForEver
Joubarbe wrote:@Shardik : what's the problem exactly ?
Trond's example won't compile on Linux if you set the subsystem to GTK2 (which is the default subsystem up to PB 5.31). You can set the subsystem by clicking in the menu bar onto
Compiler => Compiler Options...
select the tab "Compiler Options" and enter the wanted subsystem behind "Library Subsystem:"

And even on not so recent Linux distributions Trond's compiled example won't run because it requires at least GTK 3.8. The current stable GTK version is 3.16.6. The optimal solution would be to query the GTK version of the Linux distribution and to use gtk_widget_set_opacity() for GTK versions beginning with 3.8 and to use gtk_window_set_opacity() for GTK versions older than 3.8... :wink:
Joubarbe
Enthusiast
Enthusiast
Posts: 703
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Transparent windows

Post by Joubarbe »

That is awesome ! Thanks a lot Shardik. It always feels good to have something that works on all OSs.
Post Reply