Click to See Complete Forum and Search --> : Focus window's handle?


llst
February 4th, 2001, 08:44 AM
How can i get the current focus window's handle which is not belong to my process.

Thanks!

Alex Fedotov
February 4th, 2001, 01:00 PM
Try if this works (haven't tested it):


// returns a system-wide focus window, not just for the current
// thread
HWND GetGlobalFocus()
{
// remember focus window for the current thread
HWND hWndLocalFocus = GetFocus();

// find foreground window
HWND hWndFore = GetForegroundWindow();
if (hWndFore == NULL)
return NULL;

// get IDs of the current thread and the thread that
// owns foreground window
DWORD dwCurrID = GetCurrentThreadId();
DWORD dwForeID = GetWindowThreadProcessId(hWndFore, NULL);

// if the current thread owns foreground window then just
// return hWndLocalFocus
if (dwForeID == dwCurrID)
return hWndLocalFocus;

// attach input states of the current thread and the foreground
// thread
if (!AttachThreadInput(dwCurrID, dwForeID, TRUE))
return NULL;

// now the current thread and the foreground thread have common
// input state and we can query for a focus window
HWND hWndGlobalFocus = GetFocus();

// detach threads
AttachThreadInput(dwCurrID, dwForeID, FALSE);

// restore local focus
SetFocus(hWndLocalFocus);

return hWndGlobalFocus;
}

bridge zhou
April 21st, 2001, 08:28 AM
i just test it, it seemd work well in the single document, but doesn't work in the multiple document, such as in the VC. im am puzzled why the IME can well catch the current focus window .

thanks

Alex Fedotov
April 21st, 2001, 12:16 PM
Another, perhaps more reliable, way to find focus window is to use
GetGUIThreadInfo function. This function is supported since Windows 98
and Windows NT 4.0 SP3.


HWND GetGlobalFocus()
{
GUITHREADINGO info;
info.cbSize = sizeof(info);

if (!GetGUIThreadInfo(0, &info))
return NULL;

return info.hwndMenu;
}

Alex Fedotov
April 21st, 2001, 12:18 PM
Sorry, there is a typo in the function. Of course, it should
return info.hwndFocus, not info.hwndMenu.

bridge zhou
April 22nd, 2001, 03:18 AM
thanks for your request.
i use spy++ to get the VC++'s current edit window handle which is equal to the handle which is returned from GetGUIThreadInfo().

since i have get the right window's handle , then i use the function SendMessage(handle, WM_PASTE, 0,0); and the VC++'s edit window paste nothing.

i have test the notepad,ultraEdit, richedit, etc . and all works except the VC++. what's the diffent between vc++ and other edit? why it doesn't work?

thanks a lot

Donald Chen
April 22nd, 2001, 10:29 PM
Hi, Alex.

I tried to complie your code and it failed. I guess I missed some .h file, or link switches.

Please advise...

Regards and Cheers,

Donald Chen

Alex Fedotov
April 22nd, 2001, 10:46 PM
Put

#define WINVER 0x500

in the top of your stdafx.h file.

Donald Chen
April 22nd, 2001, 10:55 PM
Magic line!! Really works.



Regards and Cheers,

Donald Chen

Donald Chen
April 23rd, 2001, 03:10 PM
Hi, there.

I am doing a similar thing. Using PostMessage(winHandle, WM_COPY, 0, 0), though.

It seems like this works in notepad, not in IE. I thought it probably due to the non-editable nature of IE.

Any idea?



Regards and Cheers,

Donald Chen