Prevent my app becoming the active window when it starts

Just starting out? Need help? Post your questions and find answers here.
BlindMan
User
User
Posts: 32
Joined: Thu Aug 30, 2018 11:34 am

Prevent my app becoming the active window when it starts

Post by BlindMan »

Can I stop my PB app becoming the active window when it starts running?

OpenWindow(0, 0, 0, 200, 280, "", #PB_Window_SystemMenu)

I need the window to be visible but not to be the active window.

Tried #PB_Window_NoActivate but that has not done the trick.
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Prevent my app becoming the active window when it starts

Post by TI-994A »

BlindMan wrote:Can I stop my PB app becoming the active window when it starts running?

...I need the window to be visible but not to be the active window.

Tried #PB_Window_NoActivate but that has not done the trick.
The #PB_Window_NoActivate directive seems to work (PureBasic 5.70 LTS x64 on Windows 8.1 Professional):

Code: Select all

wFlags = #PB_Window_SystemMenu | 
         #PB_Window_ScreenCentered | 
         #PB_Window_NoActivate 
OpenWindow(0, 0, 0, 300, 200, "Non-Active Window", wFlags)
While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
BlindMan
User
User
Posts: 32
Joined: Thu Aug 30, 2018 11:34 am

Re: Prevent my app becoming the active window when it starts

Post by BlindMan »

Sorry, my mistake.

I forgot to remove a call to SetActiveGadget(). :oops:

#PB_Window_NoActivate now working as expected.

Just out of interest, if the window becomes active after it has been opened, what is the best way to reapply #PB_Window_NoActivate ?
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Prevent my app becoming the active window when it starts

Post by TI-994A »

BlindMan wrote:...what is the best way to reapply #PB_Window_NoActivate ?
Technically, an active window becomes inactive only when another window becomes active. The window loses focus when another window receives focus. So, the best way to de-activate a window would be to set the focus on the desktop.

Code: Select all

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(0, 0, 0, 300, 200, "WINDOW HAS FOCUS", wFlags)
ButtonGadget(0, 10, 140, 280, 50, "REMOVE FOCUS")
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = 1      
    Case #PB_Event_ActivateWindow
      SetWindowTitle(0, "WINDOW HAS FOCUS")
    Case #PB_Event_DeactivateWindow
      SetWindowTitle(0, "WINDOW LOST FOCUS")
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 0          
          SetForegroundWindow_(GetDesktopWindow_())
      EndSelect
  EndSelect
Until appQuit = 1
However, owing to the use of the two API functions (SetForegroundWindow & GetDesktopWindow), this is a Windows-only solution.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
BlindMan
User
User
Posts: 32
Joined: Thu Aug 30, 2018 11:34 am

Re: Prevent my app becoming the active window when it starts

Post by BlindMan »

Thank you.
Post Reply