条件是无论鼠标点击了最前窗口内部或外部依然能得到最前窗口的句柄, 我之前用的getforegroundwindow 得到的是鼠标点击后的那个窗口句柄 它是得到激活后的窗口句柄 而不是最前台的窗口句柄 如何才能得到归前台的句柄 即使点击了窗口外部 谢谢
LONG style;
HWND hwnd = GetWindow(GetDesktopWindow(), GW_CHILD);
while (TRUE)
{
style = GetWindowLong(hwnd, GWL_STYLE);
if ( (style & WS_VISIBLE ) && (style & WS_BORDER))
break;
hwnd = GetWindow(hwnd, GW_HWNDNEXT);
}
循环结束后hwnd应该就是最前台窗口句柄了。
hf=getforegroundwindow
ht=gettopwindow(hf)
htt=gettopwindow(ht)
判断ht,htt是否为null,不为空的htt就是你想要的窗口的句柄了。
希望对你有用!
GetForegroundWindow
The GetForegroundWindow function returns a handle to the foreground window (the window with which the user is currently working). The system assigns a slightly higher priority to the thread that creates the foreground window than it does to other threads.
HWND GetForegroundWindow(VOID)
Parameters
This function has no parameters.
Return Values
The return value is a handle to the foreground window.
QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in winuser.h.
Import Library: Use user32.lib.
See Also
Windows Overview, Window Functions, SetForegroundWindow
GetTopWindow
The GetTopWindow function examines the Z order of the child windows associated with the specified parent window and retrieves a handle to the child window at the top of the Z order.
HWND GetTopWindow(
HWND hWnd // handle to parent window
);
Parameters
hWnd
Handle to the parent window whose child windows are to be examined. If this parameter is NULL, the function returns a handle to the window at the top of the Z order.
Return Values
If the function succeeds, the return value is a handle to the child window at the top of the Z order. If the specified window has no child windows, the return value is NULL. To get extended error information, use theGetLastError function.
QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Unsupported.
Header: Declared in winuser.h.
Import Library: Use user32.lib.
See Also
Windows Overview, Window Functions, GetNextWindow, GetWindow