要想 windows下抓取Qt进程主界面,并嵌入到自己的程序中显示,需要首先设置qt窗口的windowTitle属性,然后就可以通过 windows api 中的 FindWindow 函数查找到窗口的hWnd了,最后通过SetParent将QT窗口装入即可。

抓取Qt界面窗口的时候,最好选用FindWindow的方式,因为通过进程查找主窗口的方式可能导致抓取到的窗口句柄不是主窗口(一个进程可能对应多个主窗口或者没有主窗口)

遇到抓取的Qt程序界面坐标响应异常,会导致菜单栏和工具栏无法正常鼠标事件,解决方案如下

1.考虑Qt界面有自己的坐标系机制,可以通过抓取QT程序界面开发接口所示方式解决坐标系不一致问题。

 #include "doecwidget.h"   

 #include <QApplication>
#include <QMouseEvent>
#include <QMessageBox> #include "mainwindow.h" class PosEventFilter : public QObject
{ public: PosEventFilter(QObject * parent = ); virtual ~PosEventFilter(); QWidget * m_curWindow; protected: bool eventFilter(QObject *obj, QEvent *event); }; PosEventFilter::PosEventFilter(QObject * parent)
: QObject(parent)
{
m_curWindow = NULL;
} PosEventFilter::~PosEventFilter()
{
} bool PosEventFilter::eventFilter(QObject *obj, QEvent *event)
{
QWidget * t_curWidget = qobject_cast<QWidget *>(obj);
///多重判断,以免导致winId函数引起事件无限循环
if ((event->type() == QEvent::MouseMove) && (NULL != t_curWidget) && (!t_curWidget->internalWinId()))
{
t_curWidget->winId();
} return QObject::eventFilter(obj, event); if ((event->type() == QEvent::MouseButtonRelease) && (m_curWindow == obj)) {
QMouseEvent * t_mouseEvent = static_cast<QMouseEvent *>(event);
QMessageBox::information(, "", QString("Mouse press %1,%2").arg(t_mouseEvent->pos().x()).arg(t_mouseEvent->pos().y()));
return true;
} else {
// standard event processing
return QObject::eventFilter(obj, event);
}
} doecwidget::doecwidget()
{ } doecwidget::~doecwidget()
{ } void doecwidget::creatDockWidget(HWND _hwnd)
{
int t_argc = ;
char * t_argv = "";
QApplication t_app(t_argc, &t_argv);
PosEventFilter t_posEventFilter(&t_app);
t_app.installEventFilter(&t_posEventFilter); CMainWindow * t_mainWindow = new CMainWindow();
t_posEventFilter.m_curWindow = t_mainWindow;
t_mainWindow->show();
SetParent(t_mainWindow->winId(), _hwnd);
///ShowWindow(t_dockWidget->winId(), SW_SHOW);
t_app.exec();
} void doecwidget::creatDockWidget()
{
int t_argc = ;
char * t_argv = "";
QApplication t_app(t_argc, &t_argv);
PosEventFilter t_posEventFilter(&t_app);
t_app.installEventFilter(&t_posEventFilter); CMainWindow * t_mainWindow = new CMainWindow();
t_posEventFilter.m_curWindow = t_mainWindow;
t_mainWindow->show();
t_app.exec();
}

2.也可以在抓取程序端,通过SendMessage函数在位置移动或大小改变事件中发送消息的方式保证坐标系一致

     HWND t_curHWND;
///t_curHWND = FindWindow(L"QWidget", L"编码分析");
t_curHWND = FindWindow(L"QWidget", L"CMainWindow");
LONG style = GetWindowLong(t_curHWND, GWL_STYLE);// 14CF 0000
// 1=WS_VISIBLE 4=WS_CLIPSIBLINGS C=WS_CAPTION F= 0000
style &=~WS_CAPTION;
//style &=~WS_CLIPSIBLINGS;
//style |=WS_CHILD;
SetWindowLong(t_curHWND,GWL_STYLE,style);
RECT t_rc;
GetClientRect(AfxGetMainWnd()->GetSafeHwnd(), &t_rc); MoveWindow(t_curHWND, , , t_rc.right - t_rc.left, t_rc.bottom - t_rc.top, TRUE);
SetParent(t_curHWND, AfxGetMainWnd()->GetSafeHwnd());
///SetWindowLong(t_curHWND, GWL_STYLE, WS_VISIBLE);
///SendMessage(t_curHWND, WM_SYSCOMMAND, SC_MAXIMIZE, 0); ShowWindow(t_curHWND, SW_MAXIMIZE); POINT t_point;
t_point.x = t_rc.left;
t_point.y = t_rc.top;
ClientToScreen(AfxGetMainWnd()->GetSafeHwnd(), &t_point); RECT t_curWinRc, t_curCliRc;
GetWindowRect(t_curHWND, &t_curWinRc);
GetClientRect(t_curHWND, &t_curCliRc);
ClientToScreen(t_curHWND, (LPPOINT)&t_curCliRc.left);
ClientToScreen(t_curHWND, (LPPOINT)&t_curCliRc.right);
///t_point.x = t_point.x + t_curCliRc.left - t_curWinRc.left;
///t_point.y = t_point.y + t_curCliRc.top - t_curWinRc.top;
SendMessage(t_curHWND, WM_MOVE, , MAKELPARAM(t_point.x, t_point.y));
///MAKELPARAM将xy转换为lParam
///MAKEPOINTS将lParam转换为POINTS

最新文章

  1. 机器学习基础与实践(三)----数据降维之PCA
  2. mycat 9066管理端口 常用命令
  3. 配置jsp开发环境
  4. Android 环境常见问题
  5. (转)Shadow Map &amp; Shadow Volume
  6. linux服务之rsyslog
  7. MVC中的Routing
  8. 实用程序Commer的开发——U盘内容可选同步至FTP服务器
  9. bzoj1059: [ZJOI2007]矩阵游戏
  10. Java--格式化输出
  11. SpringMVC国际化支持
  12. JAVA之旅(十六)——String类,String常用方法,获取,判断,转换,替换,切割,子串,大小写转换,去除空格,比较
  13. 在Laravel项目中遇到的一些问题
  14. 设计模式のNullObjectPattern(空对象模式)----行为模式
  15. Flask-WTF表单的使用
  16. GridView的簡單使用
  17. (转)UCOSII在任务切换与出入中断时堆栈指针的使用
  18. 1.Linux命令
  19. no OPENSSL_Applink错误的解决方法
  20. 【git】git pull

热门文章

  1. 配置tomcat、nginx实现反向代理(需操作)
  2. 指令——pwd
  3. 086-PHP数组按数字排序和按字母排序
  4. Windows系统安装免费的开源虚拟机软件VirtualBox
  5. .NET via C#笔记17——委托
  6. 读书笔记 - js高级程序设计 - 第十章 DOM
  7. oracle(3)select语句中常用的关键字说明
  8. Day1-T4
  9. bzoj 4247挂饰
  10. Python 中 使用 HTMLTestRunner 模块生成测试报告