Windows下,Qt激活指定句柄的窗口
2014-02-10 13:55
3511 查看
最近在用Qt写一个gui程序,其中有个置顶QWidget需要响应鼠标移动事件以激活自身,在linux下使用activateWindow()就可以达到目的。但是windows下就没这么简单了,windows不允许应用程序中断用户正在操作的其它应用,也就意味着activateWindow()失效了。那么就真的没有办法了吗?经上网搜索,发现可以使用win32 API的几个函数实现,下面分享下我的解决办法:
1.声明需要使用的函数指针变量
2.获取函数地址
3.调用函数激活指定句柄的窗口
这种方法核心是使用AttachThreadInput函数,它可以连接当前激活窗口的输入队列,并使其共享,如此一来才能调用SetForegroundWindow()函数激活指定窗口到前台,否则会无效。
1.声明需要使用的函数指针变量
/*user32.dll*/ typedef unsigned int (_stdcall*GetWindowThreadProcessId)(WId id, unsigned int *lpdwProcessId); GetWindowThreadProcessId getWindowThreadProcessId; typedef WId (_stdcall*GetForegroundWindow)(); GetForegroundWindow getForegroundWindow; typedef bool (_stdcall*SetForegroundWindow)(WId id); SetForegroundWindow setForegroundWindow; typedef WId (_stdcall*SetFocus)(WId id); SetFocus setFocus; typedef bool (_stdcall*AttachThreadInput)(unsigned int idAttach, unsigned int idAttachTo, bool fAttach); AttachThreadInput attachThreadInput; /*kernel32.dll*/ typedef unsigned int (_stdcall*GetCurrentThreadId)(); GetCurrentThreadId getCurrentThreadId;
2.获取函数地址
QLibrary user32("user32"); user32.load(); getWindowThreadProcessId = (GetWindowThreadProcessId)user32.resolve("GetWindowThreadProcessId"); getForegroundWindow = (GetForegroundWindow)user32.resolve("GetForegroundWindow"); setForegroundWindow = (SetForegroundWindow)user32.resolve("SetForegroundWindow"); setFocus = (SetFocus)user32.resolve("SetFocus"); attachThreadInput = (AttachThreadInput)user32.resolve("AttachThreadInput"); QLibrary kernel32("kernel32"); kernel32.load(); getCurrentThreadId = (GetCurrentThreadId)kernel32.resolve("GetCurrentThreadId");
3.调用函数激活指定句柄的窗口
void GlobalScrollBar::activateMsWindow(WId id)const { attachThreadInput(getWindowThreadProcessId(getForegroundWindow(), NULL), getCurrentThreadId(), true); setForegroundWindow(id); setFocus(id); attachThreadInput(getWindowThreadProcessId(getForegroundWindow(), NULL), getCurrentThreadId(), false); }
这种方法核心是使用AttachThreadInput函数,它可以连接当前激活窗口的输入队列,并使其共享,如此一来才能调用SetForegroundWindow()函数激活指定窗口到前台,否则会无效。
相关文章推荐
- QT里,windows下获取窗口句柄的方法
- C++ 在Windows下截取整个屏幕 和 指定句柄窗口的屏幕
- Qt通过句柄操作外部窗口
- 怎么用C#获取指定窗口的句柄(笔记)
- Qt在Windows下如何创建无CMD窗口控制台程序
- c++ windows app激活另一app的某窗口
- Qt 中如何捕获窗口停用和激活的消息
- 易语言获取焦点命令激活指定窗口
- windows 句柄 消息响应 窗口过程
- C#查找指定窗口的子窗口的句柄
- Windows进程间共享socket与窗口句柄的体会
- windows下tomcat指定命令窗口标题
- Windows 7桌面显示图标窗口句柄的获取
- 电脑出现“损坏的图像”窗口提示dll没有被指定在Windows上运行如何解决
- QT获取指定文件的图标以及通过句柄获取图标
- Windows程序获取鼠标下窗口句柄的例子
- Windows 7 C# OCX 无法获取“****”控件的窗口句柄,不支持无窗口的ActiveX控件的解决办法
- 初学Qt:初步了解Windows窗口基本控件
- 通过Windows进程ID获取窗口句柄
- Qt 中如何捕获窗口停用和激活的消息 Activate&Deactivate