将焦点更改为另一个程序 Windows API
Changing focus to another program Windows API
我正在尝试让我的应用程序将焦点更改为鼠标悬停时发生的任何其他窗口。我正在尝试实现一些拖放功能,但似乎缺少的只是鼠标将我的应用程序移动到另一个应用程序时焦点的变化。
这是我当前的测试功能(我现在在主回调程序中WM_MOUSEMOVE进行,以致笑)
case WM_MOUSEMOVE:
{
POINT pt;
GetCursorPos(&pt);
HWND newHwnd = WindowFromPoint(pt);
if (newHwnd != g_hSelectedWindow)
{
cout << "changing windows" << endl;
cout << SetWindowPos(newHwnd, HWND_TOP, 0,0,0,0, SWP_NOMOVE|SWP_NOSIZE) << endl;
g_hSelectedWindow = newHwnd;
}
CallWindowProc(listproc, hwnd,message,wParam,lParam);
break;
}
我尝试使用AllowSetForegroundWindow,但它有助于它在给定的范围内找不到它,但是我已经包含了.
任何帮助或建议将不胜感激。
AllowSetForegroundWindow
无济于事,除非另一个窗口试图通过调用SetForegroundWindow
成为前景窗口。
我很好奇,如果你需要把另一个窗口带到前台,为什么不直接打电话给SetForegroundWindow
呢?
更新:所以这是你需要的代码,让它正常工作:
HWND ResolveWindow(HWND hWnd)
{ /* Given a particular HWND, if it's a child, return the parent. Otherwise, if
* the window has an owner, return the owner. Otherwise, just return the window
*/
HWND hWndRet = NULL;
if(::GetWindowLong(hWnd, GWL_STYLE) & WS_CHILD)
hWndRet = ::GetParent(hWnd);
if(hWndRet == NULL)
hWndRet = ::GetWindow(hWnd, GW_OWNER);
if(hWndRet != NULL)
return ResolveWindow(hWndRet);
return hWnd;
}
HWND GetTopLevelWindowFromPoint(POINT ptPoint)
{ /* Return the top-level window associated with the window under the mouse
* pointer (or NULL)
*/
HWND hWnd = WindowFromPoint(ptPoint);
if(hWnd == NULL)
return hWnd;
return ResolveWindow(hWnd);
}
只需从WM_MOUSEMOVE
处理程序调用GetTopLevelWindowFromPoint(pt)
,如果您返回有效的 HWND,那么它将是一个顶级窗口,可以使用 SetForegroundWindow 将其置于前台。
我希望这有所帮助。
相关文章:
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 无法在windows控制台中为C++程序提供必要的输入
- 如何从Windows应用程序输出到标准?
- 如何在 64 位 vb.net Windows 应用程序中引用 32 位 dll
- C++程序不会从 Windows 控制台运行
- 在Windows 10上使用Visual Studio 2019构建的Tesseract API c ++程序在Wind
- 将 Crashpad 与 Windows Qt 应用程序集成
- 在自定义程序中使用本机 Windows 自然顺序排序
- 在 Windows/C++ 上使用多进程应用程序的高精度定时操作
- 将控制台应用程序直接输出到Visual Studio而不是外部Windows控制台中
- 是否有可能构建面向Linux和Windows的.Net Core C++ / CLI应用程序?
- 将通用Windows应用程序转换为VisualStudio 2019上的常规应用程序
- 将焦点更改为另一个程序 Windows API
- 在由另一个程序(Windows)写入文件时读取文件
- 生成窗体的控制台应用程序(Windows窗体)
- 在 2 个程序 (Windows) 之间传输信息
- 编译C++源以在Windows应用商店应用程序(Windows 8)中使用它们
- 设备驱动程序:Windows ReadFile函数超时
- 如何知道进程是否为应用程序- Windows