要想 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

将QT窗口嵌入到WinForm窗口的更多相关文章

  1. Wpf窗口中打开WinForm窗口

    获取wpf窗口对应的句柄窗口 using System; using System.Windows; using System.Windows.Interop; using IWin32Window ...

  2. 窗口嵌入到另一个窗口(VC和QT都有)

    1.用vc新建一个dialog1工程.属性默认. 2.insert一个dialog2,改为child. 3.在dialog1中包含dialog2头文件,在一个按钮事件中显示dialog2: Cdial ...

  3. windows窗口分析,父窗口,子窗口,所有者窗口

    (本文尝试通过一些简单的实验,来分析Windows的窗口机制,并对微软的设计理由进行一定的猜测,需要读者具备C++.Windows编程及MFC经验,还得有一定动手能力.文中可能出现一些术语不统一的现象 ...

  4. Qt实现嵌入桌面的半透明窗口 good

    这儿用上了前面一文提到的函数findDesktopIconWnd().见: http://mypyg.blog.51cto.com/820446/263349 一.将Qt窗口嵌入到桌面中.声明一个最简 ...

  5. WPF中用于嵌入其他进程窗口的自定义控件(AppContainer)

    原文:WPF中用于嵌入其他进程窗口的自定义控件(AppContainer) 版权声明:本文为博主原创文章,转载请注明作者和出处 https://blog.csdn.net/ZZZWWWPPP11199 ...

  6. [WPF]建立自适应窗口大小布局的WinForm窗口

    编写WinForm程序时,都会碰到一个问题.就是WinForm窗口在不同分辨率下的大小问题.举例说明,你编写的WinForm窗口在1024×768下是合适.匀称的.不过,如果用户的计算机的分辨率为14 ...

  7. qt如何实现一个渐隐窗口呢(开启的时候他是从上往下渐渐显示)

    qt如何实现一个渐隐窗口呢?就是比如说开启的时候他是从上往下渐渐显示的,关闭的时候从下往上渐渐小时的http://stackoverflow.com/questions/19087822/how-to ...

  8. QT笔记之实现阴影窗口

    方法一: 代码实现 在窗口构造函数中加入:setAttribute(Qt::WA_TranslucentBackground),保证不被绘制上的部分透明 重写void paintEvent(QPain ...

  9. Qt使用QStackedWidget实现堆栈窗口

    Qt使用QStackedWidget实现堆栈窗口 分类: QT2012-07-25 21:59 6997人阅读 评论(0) 收藏 举报 qtlistsignal 堆栈窗口可以根据选择项的不同显示不同的 ...

随机推荐

  1. thinkphp配置到二级目录,不配置到根目录,访问除首页的其他路径都是404报错

    1.在nginx的配置里面,进行重定向 vi /etc/nginx/conf.d/default.conf 2.进入编辑 location /thinkphp/public { if (!-e $re ...

  2. Dockerfile详解,以及构建自定义镜像

    Dockerfile使用 前面的操作我们一直下载下载官方已经构建好的镜像,直接下载后就可以run,如果我们想要在镜像中添加自己的应用,比如在tomcat中添加自己的app,构建一个自定义的镜像,那么我 ...

  3. JS - 使 canvas 背景透明

    canvas = document.getElementById('canvas1'); var context = canvas.getContext('2d');context.fillStyle ...

  4. [LeetCode] 934. Shortest Bridge 最短的桥梁

    In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected grou ...

  5. python面试题整理(一)

    python基础:1.列表生成式和生成器表达式有什么区别 我说的是首先写法不一样,列表生成式用[],生成器表达式用(),其次列表生成是一次性生成一个完整的列表,生成器表达式返回的是一个一个的值,占用内 ...

  6. java核心-JVM-gc面试题

    1.写一个memory leak的例子 public class MemonyLeak { //1.memoryLeak内存泄漏 /* 这类错误报错具体显示:java.lang.OutOfMemory ...

  7. getClass兼容ie

    if (!document.getElementsByClassName) { document.getElementsByClassName = function(className, elemen ...

  8. location - 修改url后 - 重新加载

    window.location.href = window.location.pathname + search;

  9. div 100% 填充页面

    css中 html,body{ margin:0; padding:0; height:100%; }

  10. ng : File C:\Users\baron\AppData\Roaming\npm\ng.ps1 cannot be loaded because running

    一. Windos PowerShell 选择 管理员身份运行二.set-ExecutionPolicy RemoteSigned 然后更改权限为A 三.get-ExecutionPolicy 查看当 ...