Always On Top/Bottom

Share your advanced PureBasic knowledge/code with the community.
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Always On Top/Bottom

Post by Foz »

Due to a little tool I'm building these functions were necessary, and I required it to work on both Windows & Linux (the two OSs I use) - the code is dotted around the forum, but not nicely tied up like I have it here.

If you have any suggestions, or you have the Mac version handy, let me know so I can add to it! :)

Code: Select all

Procedure TopMost(Window, Enable)
  CompilerSelect #PB_Compiler_OS 
    CompilerCase #PB_OS_Linux
      *Widget.GtkWidget = WindowID(Your_Window)
      *GdkWindow = *Widget\Window
      gdk_window_set_keep_above_(*GdkWindow, Enable)
    CompilerCase #PB_OS_Windows
      If Enable
        SetParent_(WindowID(Window), 0)
        SetWindowPos_(WindowID(Window),#HWND_TOPMOST,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE)
      Else
        SetWindowPos_(WindowID(Window),#HWND_NOTOPMOST,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE)
      EndIf
  CompilerEndSelect
EndProcedure

Procedure BottomMost(Window, Enable)
  CompilerSelect #PB_Compiler_OS 
    CompilerCase #PB_OS_Linux
      *Widget.GtkWidget = WindowID(Your_Window)
      *GdkWindow = *Widget\Window
      gdk_window_set_keep_below_(*GdkWindow, Enable)
    CompilerCase #PB_OS_Windows
      If Enable
        SetWindowPos_(WindowID(Window),#HWND_NOTOPMOST,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE)
        SetParent_(WindowID(Window), FindWindow_(0,"Program Manager"))
      Else
        SetParent_(WindowID(Window), 0)
      EndIf
  CompilerEndSelect
EndProcedure

TopMostEnabled = #False
BottomMostEnabled = #False

OpenWindow(0, 0, 0, 200, 230, "Notes", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
ButtonGadget(1, 10,  10, 180, 100, "Top Most Toggle")
ButtonGadget(2, 10, 120, 180, 100, "Bottom Most Toggle")

Repeat
  Event = WaitWindowEvent()
  If Event = #PB_Event_Gadget
    Select EventGadget()
      Case 1
        TopMostEnabled = ~TopMostEnabled & #True
        TopMost(0, TopMostEnabled)
        BottomMostEnabled = #False
      Case 2
        BottomMostEnabled = ~BottomMostEnabled & #True
        BottomMost(0, BottomMostEnabled)
        TopMostEnabled = #False
    EndSelect
  EndIf  
Until Event = #PB_Event_CloseWindow
User avatar
STARGÅTE
Addict
Addict
Posts: 2235
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Always On Top/Bottom

Post by STARGÅTE »

BottomMost() ist oke, but for TopMost you can use PB-Function:
StickyWindow(#Window, Status)
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Re: Always On Top/Bottom

Post by Foz »

:shock: I totally missed that one!

*** edit: actually, it StickyWindow() doesn't work very well when flicking between Bottom Most and Top Most - which is the behaviour I need for my app.

However, I will make note of that for future notice.
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Re: Always On Top/Bottom

Post by Foz »

And a further look confirmed that I was talking out of my backside :lol:

Here's just the BottomMost then, with the toggles between top most and bottom most.

Code: Select all

Procedure BottomMost(Window, Enable)
  CompilerSelect #PB_Compiler_OS 
    CompilerCase #PB_OS_Linux
      *Widget.GtkWidget = WindowID(Your_Window)
      *GdkWindow = *Widget\Window
      gdk_window_set_keep_below_(*GdkWindow, Enable)
    CompilerCase #PB_OS_Windows
      If Enable
        SetWindowPos_(WindowID(Window),#HWND_NOTOPMOST,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE)
        SetParent_(WindowID(Window), FindWindow_(0,"Program Manager"))
      Else
        SetParent_(WindowID(Window), 0)
      EndIf
  CompilerEndSelect
EndProcedure

TopMostEnabled = #False
BottomMostEnabled = #False

OpenWindow(0, 0, 0, 200, 230, "Notes", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
ButtonGadget(1, 10,  10, 180, 100, "Top Most Toggle")
ButtonGadget(2, 10, 120, 180, 100, "Bottom Most Toggle")

Repeat
  Event = WaitWindowEvent()
  If Event = #PB_Event_Gadget
    Select EventGadget()
      Case 1
        BottomMostEnabled = #False
        BottomMost(0, BottomMostEnabled)

        TopMostEnabled = ~TopMostEnabled & #True
        StickyWindow(0, TopMostEnabled)
      Case 2
        TopMostEnabled = #False
        StickyWindow(0, TopMostEnabled)

        BottomMostEnabled = ~BottomMostEnabled & #True
        BottomMost(0, BottomMostEnabled)
    EndSelect
  EndIf  
Until Event = #PB_Event_CloseWindow
Post Reply