将QT窗⼝嵌⼊到WinForm窗⼝
要想 windows下抓取Qt进程主界⾯,并嵌⼊到⾃⼰的程序中显⽰,需要⾸先设置qt窗⼝的windowTitle属性,然后就可以通过 windows api 中的 FindWindow 函数查到窗⼝的hWnd了,最后通过SetParent将QT窗⼝装⼊即可。
抓取Qt界⾯窗⼝的时候,最好选⽤FindWindow的⽅式,因为通过进程查主窗⼝的⽅式可能导致抓取到的窗⼝句柄不是主窗⼝(⼀个进程可能对应多个主窗⼝或者没有主窗⼝)
遇到抓取的Qt程序界⾯坐标响应异常,会导致菜单栏和⼯具栏⽆法正常⿏标事件,解决⽅案如下
1.考虑Qt界⾯有⾃⼰的坐标系机制,可以通过抓取QT程序界⾯开发接⼝所⽰⽅式解决坐标系不⼀致问题。
1 #include "doecwidget.h"
2
3 #include <QApplication>
4 #include <QMouseEvent>
5 #include <QMessageBox>
6
7 #include "mainwindow.h"
8
9
10class PosEventFilter : public QObject
11{
12
13public:
14
15    PosEventFilter(QObject * parent = 0);
16
17virtual ~PosEventFilter();
18
19    QWidget * m_curWindow;
20
21protected:
22
23bool eventFilter(QObject *obj, QEvent *event);
24
25};
26
27 PosEventFilter::PosEventFilter(QObject * parent)
28    : QObject(parent)
29{
30    m_curWindow = NULL;
31}
32
33 PosEventFilter::~PosEventFilter()
34{
35}
36
37bool PosEventFilter::eventFilter(QObject *obj, QEvent *event)
38{
39    QWidget * t_curWidget = qobject_cast<QWidget *>(obj);
40///多重判断,以免导致winId函数引起事件⽆限循环
41if ((event->type() == QEvent::MouseMove) && (NULL != t_curWidget) && (!t_curWidget->internalWinId()))
42    {
43        t_curWidget->winId();
44    }
45
46return QObject::eventFilter(obj, event);
47
48if ((event->type() == QEvent::MouseButtonRelease) && (m_curWindow == obj)) {
49        QMouseEvent * t_mouseEvent = static_cast<QMouseEvent *>(event);
50        QMessageBox::information(0, "", QString("Mouse press %1,%2").arg(t_mouseEvent->pos().x()).arg(t_mouseEvent->pos().y()));
51return true;
52    } else {
53// standard event processing
54return QObject::eventFilter(obj, event);
55    }
56}
57
58
59doecwidget::doecwidget()
60{
61
61
62}
63
64 doecwidget::~doecwidget()
65{
66
67}
68
69void doecwidget::creatDockWidget(HWND _hwnd)
70{
71int t_argc = 1;
72char * t_argv = "";
73    QApplication t_app(t_argc, &t_argv);
74    PosEventFilter t_posEventFilter(&t_app);
75    t_app.installEventFilter(&t_posEventFilter);
76
77    CMainWindow * t_mainWindow = new CMainWindow();
78    t_posEventFilter.m_curWindow = t_mainWindow;
79    t_mainWindow->show();
80    SetParent(t_mainWindow->winId(), _hwnd);
81///ShowWindow(t_dockWidget->winId(), SW_SHOW);
82    ();
83}
84
85void doecwidget::creatDockWidget()
86{
87int t_argc = 1;
88char * t_argv = "";
89    QApplication t_app(t_argc, &t_argv);
90    PosEventFilter t_posEventFilter(&t_app);
91    t_app.installEventFilter(&t_posEventFilter);
92
93    CMainWindow * t_mainWindow = new CMainWindow();
94    t_posEventFilter.m_curWindow = t_mainWindow;
95    t_mainWindow->show();
96    ();
qt进程间通信97 }
2.也可以在抓取程序端,通过SendMessage函数在位置移动或⼤⼩改变事件中发送消息的⽅式保证坐标系⼀致
1    HWND t_curHWND;
2///t_curHWND = FindWindow(L"QWidget", L"编码分析");
3    t_curHWND = FindWindow(L"QWidget", L"CMainWindow");
4    LONG style = GetWindowLong(t_curHWND, GWL_STYLE);// 14CF 0000
5// 1=WS_VISIBLE 4=WS_CLIPSIBLINGS C=WS_CAPTION F= 0000
6    style &=~WS_CAPTION;
7//style &=~WS_CLIPSIBLINGS;
8//style |=WS_CHILD;
9    SetWindowLong(t_curHWND,GWL_STYLE,style);
10    RECT t_rc;
11    GetClientRect(AfxGetMainWnd()->GetSafeHwnd(), &t_rc);
12
13    MoveWindow(t_curHWND, 0, 0, t_rc.right - t_rc.left, t_rc.bottom - p, TRUE);
14    SetParent(t_curHWND, AfxGetMainWnd()->GetSafeHwnd());
15///SetWindowLong(t_curHWND, GWL_STYLE, WS_VISIBLE);
16///SendMessage(t_curHWND, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
17
18    ShowWindow(t_curHWND, SW_MAXIMIZE);
19
20    POINT t_point;
21    t_point.x = t_rc.left;
22    t_point.y = p;
23    ClientToScreen(AfxGetMainWnd()->GetSafeHwnd(), &t_point);
24
25    RECT t_curWinRc, t_curCliRc;
26    GetWindowRect(t_curHWND, &t_curWinRc);
27    GetClientRect(t_curHWND, &t_curCliRc);
28    ClientToScreen(t_curHWND, (LPPOINT)&t_curCliRc.left);
29    ClientToScreen(t_curHWND, (LPPOINT)&t_curCliRc.right);
30///t_point.x = t_point.x + t_curCliRc.left - t_curWinRc.left;
31///t_point.y = t_point.y + p - p;
32    SendMessage(t_curHWND, WM_MOVE, 0, MAKELPARAM(t_point.x, t_point.y));  33///MAKELPARAM将xy转换为lParam
34///MAKEPOINTS将lParam转换为POINTS

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。