The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: RedXVII on June 21, 2006, 08:29:00 PM

Title: Window is active?
Post by: RedXVII on June 21, 2006, 08:29:00 PM
Im looking for a command to test if m window is currently active. What i mean by active is - the user has actuated it and it is currently being worked on. I cant find it on msdn :(

The idea behind it is;

.if !window_is_being_worked_on
       playsound
.endif
Title: Re: Window is active?
Post by: P1 on June 21, 2006, 08:49:50 PM
Try this:
The GetActiveWindow function retrieves the window handle of the active window associated with the thread that calls the function.

HWND GetActiveWindow(VOID)


Parameters

This function has no parameters.

Return Value

If the function succeeds, the return value is the handle of the active window associated with the thread that calls the function. If the calling thread does not have an active window, the return value is NULL.

Regards,  P1  :8)
Title: Re: Window is active?
Post by: ToutEnMasm on June 21, 2006, 08:57:25 PM

     IsWindowEnabled ,can answer for any windows.You have to know it's handle and perhaps search it's class name to have it.
                                         ToutEnMasm
Title: Re: Window is active?
Post by: P1 on June 21, 2006, 09:56:46 PM
My thought for this was, no parameter to mess with.  Test for NULL when it come back, means not me.

But the real issue will be Loss of Focus, and what to do then.

Regards,  P1  :8)
Title: Re: Window is active?
Post by: P1 on June 21, 2006, 10:07:32 PM
Quote from: P1 on June 21, 2006, 09:56:46 PM
My thought for this was, no parameter to mess with.  Test for NULL when it come back, means not me.

IsWindowEnabled maybe a better function in the use of thread resources.

But the real issue will be Loss of Focus, and what to do then.

Regards,  P1  :8)
Title: Re: Window is active?
Post by: KSS on June 24, 2006, 04:37:13 PM
RedXVII,
not clearly what you want:
  where you use this code? (in msg proc?)
Quote.if !window_is_being_worked_on
       playsound
.endif

Try to use: GetForegroundWindow() and GetAncestor()
Title: Re: Window is active?
Post by: donkey on June 24, 2006, 04:59:13 PM
Quote from: P1 on June 21, 2006, 09:56:46 PM
My thought for this was, no parameter to mess with.  Test for NULL when it come back, means not me.

But the real issue will be Loss of Focus, and what to do then.

Regards,  P1  :8)

I have used the WM_ACTIVATE message with great success, the message is sent to your window each time it is being activated or deactivated. Simple and easy to use without having to check all the time. Event driven programming is almost always more efficient than calling an API constantly, this way you have only to set a flag.
Title: Re: Window is active?
Post by: KSS on June 24, 2006, 09:23:25 PM
WM_ACTIVATE is really best solution!  :red :U


(http://gta.com.ua/forum2/style_images/1/folder_post_icons/icon13.gif)
Does anybody known in what way I can prevent set active my window?
(My situation: MainWindow has hide status{icon in tray notify}, Wnd2 has show status, but not visible in Task Panel{On Alt+TAB it showed}. So.. When User open Notepad{or something else} and my App and user minimize Notepad — my app will show up{Such as user pressed Alt+TAB}  :'(This is a problem.)
{And I don't want use keyboard hooks ::)}
Title: Re: Window is active?
Post by: Tedd on June 26, 2006, 12:20:06 PM
WM_SETFOCUS ?

Will indicate gaining and losing 'focus' and also gives the handle of the window that had focus previously.
So to 'lose' focus, I assume you could just SetFocus to the hwnd given by WM_SETFOCUS in response to that message. However, I have a feeling this may not work all of the time. (Alternatively you could find the 'next' window for focus with GetWindow)
Title: Re: Window is active?
Post by: KSS on June 26, 2006, 06:47:40 PM
Tedd,
we have ONLY two program Notepad and MyProg. When user close notepad MyProg is stay one! So I can't use WM_SETFOCUS. :'(
Title: Re: Window is active?
Post by: Tedd on June 27, 2006, 01:03:26 PM
Then there is only one window that can get focus - where else would the focus go?
I suppose you could try disabling your window (EnableWindow, hwnd,FALSE) and see if that gives you what you want -- it will stop the user being able to focus it by clicking (once it loses focus the first time), but it can still be brought to the foreground by selecting it in the taskbar.
Title: Re: Window is active?
Post by: P1 on June 27, 2006, 01:40:04 PM
Quote from: Tedd on June 27, 2006, 01:03:26 PMThen there is only one window that can get focus - where else would the focus go?
The Desktop can receive focus as well.  I can not remember if a hidden( not shown ) window, can receive focus as well. 

Regards,  P1  :8)
Title: Re: Window is active?
Post by: KSS on June 27, 2006, 05:10:49 PM
Oh, guys! :dance:
GetDesktopWindow() return handle of desktop, so MyProg is not one in system.


Probably I have overheated. :bg In my room now 29°C (In the winter 21-24°C)