【Qt】Qt之进程间通讯(Windows消息)【转】

简述

经过上一节的了解,咱们能够看出进程通讯的方式不少,今天分享下如何利用Windows消息机制来进行不一样进程间的通讯。windows

效果

这里写图片描述

发送消息

自定义类型与接收窗体

包含所需库,定义发送的自定义类型、接收消息的窗体标题。自定义类型能够处理消息过多状况下,对消息的区分,若是不须要也能够去掉。bash

#ifdef Q_OS_WIN
#pragma comment(lib, "user32.lib")
#include <qt_windows.h>
#endif

const ULONG_PTR CUSTOM_TYPE = 10000;
const QString c_strTitle = "ReceiveMessage";

发送数据

点击按钮,进行消息发送。里面的do{…}while用来忽略本窗口,固然自身也能够接受自身的消息。markdown

void onSendMessage()
{
    HWND hwnd = NULL;
    //do
    //{
       LPWSTR path = (LPWSTR)c_strTitle.utf16();  //path = L"SendMessage"
       hwnd = ::FindWindowW(NULL, path);
    //} while (hwnd == (HWND)effectiveWinId()); // 忽略本身

    if (::IsWindow(hwnd))
    {
        QString filename = QStringLiteral("进程通讯-Windows消息");
        QByteArray data = filename.toUtf8();

        COPYDATASTRUCT copydata;
        copydata.dwData = CUSTOM_TYPE;  // 用户定义数据
        copydata.lpData = data.data();  //数据大小
        copydata.cbData = data.size();  // 指向数据的指针

        HWND sender = (HWND)effectiveWinId();

        ::SendMessage(hwnd, WM_COPYDATA, reinterpret_cast<WPARAM>(sender), reinterpret_cast<LPARAM>(&copydata));
    }
}

接收消息

设置标题

这一步很重要,必须与上一步的c_strTitle保持一致,不然会找不到窗体。自定义类型CUSTOM_TYPE也必须保持一致,进行过滤。this

setWindowTitle("ReceiveMessage");

重写nativeEvent

bool nativeEvent(const QByteArray &eventType, void *message, long *result)
{
    MSG *param = static_cast<MSG *>(message);

    switch (param->message)
    {
    case WM_COPYDATA:
    {
        COPYDATASTRUCT *cds = reinterpret_cast<COPYDATASTRUCT*>(param->lParam);
        if (cds->dwData == CUSTOM_TYPE)
        {
            QString strMessage = QString::fromUtf8(reinterpret_cast<char*>(cds->lpData), cds->cbData);
            QMessageBox::information(this, QStringLiteral("提示"), strMessage);
            *result = 1;
            return true;
        }
    }
    }

    return QWidget::nativeEvent(eventType, message, result);
}


原文做者:一去丶二三里
做者博客:去做者博客空间
相关文章
相关标签/搜索