Solved

Find handle of previous active window (win32)

Posted on 2009-07-04
3
401 Views
Last Modified: 2013-12-04
I'm trying to write a small win32 application (in C or C++)  that sends pre-cooked keystrokes to another application. The user experience should be this:

The user works with application X (which can be anything), then uses the mouse to click somewhere in the window belonging to application Y (written by me), which is visible on the screen. Application Y uses SendInput to send keystrokes to application X.

The problem is: SendInput sends keystrokes to the foreground window. How can application Y determine which window was the foreground window immediately before it received the mouse click?

Back in 1997, someone asked a similar question here (http://www.experts-exchange.com/Programming/System/Windows__Programming/Q_10010231.html), and the answer was to look at the wparam of the WM_SETFOCUS message. However,  the answer was later withdrawn (and in Vista that parameter appears to always be 0). One of the last comments on the question from 1997 says: "It seems there is no way to obtain handle of deactivated window."

Is that really true?
0
Comment
Question by:cto
3 Comments
 
LVL 9

Accepted Solution

by:
JohnGaby earned 250 total points
Comment Utility
You might try creating a window that does not take the focus in the first place.  That way you wouldn't need to determine where the focus originated  Check out the WS_EX_NOACTIVATE style:

http://msdn.microsoft.com/en-us/library/ms632680(VS.85).aspx
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
Comment Utility
If we believe MSDN, it should be WM_SETFOCUS (http://msdn.microsoft.com/en-us/library/ms646283(VS.85).aspx), where it says: "wParam - Handle to the window that has lost the keyboard focus. This parameter can be NULL."

However, if that does not work for you, you might want to consider setting a global hook (see http://msdn.microsoft.com/en-us/library/ms997537.aspx - "Win32 Hooks") that filters for WM_KILLFOCUS and sends the handle of the window that message was targetted at to your app. E.g.
LRESULT CALLBACK HookProc	(	int		nCode,	// hook code

                                WPARAM	wParam,	// removal flag 

                                LPARAM  lParam 	// address of structure with message 

                            )

{

    PMSG	pmsg	=	( PMSG)	lParam;
 

    if	(	0	>	nCode	||	PM_NOREMOVE	==	wParam)	

        {

            return	(	CallNextHookEx	(	g_hhk,

                                            nCode,

                                            wParam,

                                            lParam

                                        )

                    );

        }
 

    if	(WM_KILLFOCUS == pmsg->message)

        {

            /* this one is for us, so grab the window handle here */

        }
 

    return	(	CallNextHookEx	(	g_hhk,

                                    nCode,

                                    wParam,

                                    lParam

                                )

            );

}

Open in new window

0
 

Author Comment

by:cto
Comment Utility
Thank you, JohnGaby and jkr. Both solutions are useful, and I have split the points between you.

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now