Ensuring a Window does not have the focus

Just starting out? Need help? Post your questions and find answers here.
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Ensuring a Window does not have the focus

Post by RichardL »

I'm embarrassed by not knowing the answer to this simple question :-(

I need to guarantee that a window does not have the focus prior to using HideWindow(MyWindow,0) to reveal it. In other words I need to ensure that the title bar is grey; just like I have clicked on the desk top.

I can open another window first and use SetActiveWindow(ANOther) but that is a silly solution!

If the user clicks on my window it receives the focus as usual, thats what I want; but when I un-hide it it must not interrupt what he is doing in another application.

How can I ensure that the window does NOT have the focus?
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

I'm not sure what it is you're asking. Can you try rephrasing your question? Maybe provide example code?

You can identify and set the active window using the Win32 API commands GetActiveWindow_() and SetActiveWindow(). I hope this helps.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Code: Select all

OpenWindow(0, #PB_Ignore, #PB_Ignore, 640, 480, "bla")
DisableWindow(0, #True)

While WaitWindowEvent() <> #PB_Event_CloseWindow : Wend
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Post by RichardL »

Here is a skeleton of the code I'm writing...

Code: Select all

  MyWindow.l = OpenWindow(#PB_Any,0,0,200,100,"Test",#PB_Window_ScreenCentered)
  AddKeyboardShortcut(MyWindow.l, #PB_Shortcut_Escape, 100)    ; ESC hot key for exit
  StickyWindow(MyWindow.l,1)
  
  While 1
    
    HideWindow(MyWindow,1)     ; Hide the window
    Delay(2000)                ; Wait (Usually about 30 minutes)
    Beep_(1000,10)             ; Attract user's attention
    HideWindow(MyWindow,0)     ; Un-hide the window
    DisableWindow(MyWindow.l,#True)
    
    ; Skeleton of my code that handles user input, timing and comms.
    WaitTime = ElapsedMilliseconds() + 2000 ; Wait duration for user input (Usually 1 minute)
    While WaitTime  > ElapsedMilliseconds() 
      Delay(1)
      Select WindowEvent()
        Case #PB_Event_Menu 
          Select EventMenu()
            Case 100 : Break 2
              
              
              
          EndSelect
          
      EndSelect
      ; Async Comms here
      
    Wend
    
  Wend
How to see my problem...

Open another application that accepts keyboard input (eg: MS Word)
Run my code
Click in the Test window when visible (Test's title bar becomes active)
Wait for Test window to hide
Click on MS Word
Note how Word title bar toggles on/off and keys become inactive when Test window is visible

Wait for Test window to appear and immediately click on desktop.
Test window now toggles on / off but does not have focus.
MS Word title no longer toggles.

How can I ensure that the Test window does NOT have the focus?
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

I see your problem.

it occured to me e.g. when I write a posting in FiFo and a download is finished,
the "download ready" popup steals the focus from my text edit element.

I don't know an answer yet, but I think there could be an API way to solve it.
if you have the nerve, search the MSDN (ok, sounds like kidding)
for e.g. "unhiding window without retrieving the focus" or such.
oh... and have a nice day.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

Try:

Code: Select all

ShowWindow_(WindowID(#num),#SW_SHOWNOACTIVATE)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

PB wrote:Try:

Code: Select all

ShowWindow_(WindowID(#num),#SW_SHOWNOACTIVATE)
cool, works fine here! Image
oh... and have a nice day.
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Post by RichardL »

Hi PB,

That appears to do the trick! :D :D :D
Here is the modification to my example code...

Code: Select all

    HideWindow(MyWindow,1)     ; Hide the window
    Delay(2000)                ; Wait (Usually about 30 minutes)
    Beep_(1000,10)             ; Attract user's attention
    ;HideWindow(MyWindow,0)     ; Un-hide the window
    ;DisableWindow(MyWindow.l,#True)
    ShowWindow_(WindowID(MyWindow),#SW_SHOWNOACTIVATE)
Thanks to all,
RichardL
Post Reply