QT 嵌入exe应用程序 并获取鼠标事件 mouse keyboard event

来源:互联网 发布:济南淘宝纸盒 编辑:程序博客网 时间:2024/05/29 08:28

QT 嵌入第三方程序方法:

QProcess *Process = new QProcess(this); //Process必须为指针,不然对象销毁时第三方exe会被关闭
    Process->start(“Everything\\Everything.exe"); //启动exe
    if (Process->waitForStarted())
    {
        Sleep(2000);
        WId wid = (WId)FindWindow(L"EVERYTHING", L"Everything"); 获取exe wid
        if (wid)
        {
            QWindow *mapWindow = QWindow::fromWinId(wid); //转换为QWindow
             QWidget *Widget = QWidget::createWindowContainer(mapWindow, this);//包含到QWidget
          //可以对Widget进行布局操作
            attachWindowThreadInput(wid); //获取鼠标键盘事件给第三方exe

}

}

//把当前程序的事件通过WId传递给第三方exe

void attachWindowThreadInput(WId remoteWindowId)
{
    DWORD currentThreadId = GetCurrentThreadId();
    DWORD remoteThreadId = GetWindowThreadProcessId((HWND)remoteWindowId, NULL);
    qDebug() << __FUNCTION__ << "win" << remoteWindowId << "thr" << remoteThreadId;
    if (currentThreadId != remoteThreadId) {
        if (!AttachThreadInput(remoteThreadId, currentThreadId, true))
            qErrnoWarning("AttachThreadInput");
    }
}

转载请注明出处