Qt之进程间通信之Windows消息(nativeEvent)

相关资料:

https://blog.csdn.net/liulihuo_gyh/article/details/79835468    原博客

https://www.cnblogs.com/fwycmengsoft/p/6642871.html    接口说明

https://download.csdn.net/download/zhujianqiangqq/13719629    代码包下载

发送方:

.pro

 1 QT       += core gui
 2 
 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 4 
 5 CONFIG += c++11
 6 
 7 # The following define makes your compiler emit warnings if you use
 8 # any Qt feature that has been marked deprecated (the exact warnings
 9 # depend on your compiler). Please consult the documentation of the
10 # deprecated API in order to know how to port your code away from it.
11 DEFINES += QT_DEPRECATED_WARNINGS
12 
13 # You can also make your code fail to compile if it uses deprecated APIs.
14 # In order to do so, uncomment the following line.
15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
17 
18 SOURCES += 
19     main.cpp 
20     mainwindow.cpp
21 
22 HEADERS += 
23     mainwindow.h
24 
25 FORMS += 
26     mainwindow.ui
27 
28 # Default rules for deployment.
29 qnx: target.path = /tmp/$${TARGET}/bin
30 else: unix:!android: target.path = /opt/$${TARGET}/bin
31 !isEmpty(target.path): INSTALLS += target
View Code

main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include <QApplication>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     MainWindow w;
 9     w.show();
10     return a.exec();
11 }
View Code

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 
 6 QT_BEGIN_NAMESPACE
 7 namespace Ui { class MainWindow; }
 8 QT_END_NAMESPACE
 9 
10 // 定义加载的系统库
11 #ifdef Q_OS_WIN
12 #pragma comment(lib, "user32.lib")
13 #include <qt_windows.h>
14 #endif
15 
16 const ULONG_PTR CUSTOM_TYPE = 10000;// 定义消息常量
17 const QString c_strTitle = "ReceiveMessage";// 定义接收窗体名字
18 
19 class MainWindow : public QMainWindow
20 {
21     Q_OBJECT
22 
23 public:
24     MainWindow(QWidget *parent = nullptr);
25     ~MainWindow();
26 
27 private slots:
28     void on_pushButton_clicked();
29 
30 private:
31     Ui::MainWindow *ui;
32 };
33 #endif // MAINWINDOW_H
View Code

mainwindow.cpp

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 
 4 MainWindow::MainWindow(QWidget *parent)
 5     : QMainWindow(parent)
 6     , ui(new Ui::MainWindow)
 7 {
 8     ui->setupUi(this);
 9 
10     setWindowTitle("SendMessage");
11 }
12 
13 MainWindow::~MainWindow()
14 {
15     delete ui;
16 }
17 
18 void MainWindow::on_pushButton_clicked()
19 {
20     HWND hwnd = NULL;
21     //do
22     //{
23     LPWSTR path = (LPWSTR)c_strTitle.utf16();  //path = L"SendMessage"
24     hwnd = ::FindWindowW(NULL, path);
25     //} while (hwnd == (HWND)effectiveWinId()); // 忽略自己
26 
27     if (::IsWindow(hwnd))
28     {
29         QString filename = QStringLiteral("进程通信-Windows消息");
30         QByteArray data = filename.toUtf8();
31 
32         COPYDATASTRUCT copydata;
33         copydata.dwData = CUSTOM_TYPE;  // 用户定义数据
34         copydata.lpData = data.data();  //数据大小
35         copydata.cbData = data.size();  // 指向数据的指针
36         HWND sender = (HWND)effectiveWinId();
37 
38         ::SendMessage(hwnd, WM_COPYDATA, reinterpret_cast<WPARAM>(sender), reinterpret_cast<LPARAM>(&copydata));
39     }
40 }
View Code

 .ui

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <ui version="4.0">
 3  <class>MainWindow</class>
 4  <widget class="QMainWindow" name="MainWindow">
 5   <property name="geometry">
 6    <rect>
 7     <x>0</x>
 8     <y>0</y>
 9     <width>416</width>
10     <height>228</height>
11    </rect>
12   </property>
13   <property name="windowTitle">
14    <string>MainWindow</string>
15   </property>
16   <widget class="QWidget" name="centralwidget"/>
17   <widget class="QMenuBar" name="menubar">
18    <property name="geometry">
19     <rect>
20      <x>0</x>
21      <y>0</y>
22      <width>416</width>
23      <height>22</height>
24     </rect>
25    </property>
26   </widget>
27   <widget class="QStatusBar" name="statusbar"/>
28  </widget>
29  <resources/>
30  <connections/>
31 </ui>
View Code

接收方:

.pro

 1 QT       += core gui
 2 
 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 4 
 5 CONFIG += c++11
 6 
 7 # The following define makes your compiler emit warnings if you use
 8 # any Qt feature that has been marked deprecated (the exact warnings
 9 # depend on your compiler). Please consult the documentation of the
10 # deprecated API in order to know how to port your code away from it.
11 DEFINES += QT_DEPRECATED_WARNINGS
12 
13 # You can also make your code fail to compile if it uses deprecated APIs.
14 # In order to do so, uncomment the following line.
15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
17 
18 SOURCES += 
19     main.cpp 
20     mainwindow.cpp
21 
22 HEADERS += 
23     mainwindow.h
24 
25 FORMS += 
26     mainwindow.ui
27 
28 # Default rules for deployment.
29 qnx: target.path = /tmp/$${TARGET}/bin
30 else: unix:!android: target.path = /opt/$${TARGET}/bin
31 !isEmpty(target.path): INSTALLS += target
View Code

main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include <QApplication>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     MainWindow w;
 9     w.show();
10     return a.exec();
11 }
View Code

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 #include <QMessageBox>
 6 
 7 QT_BEGIN_NAMESPACE
 8 namespace Ui { class MainWindow; }
 9 QT_END_NAMESPACE
10 
11 // 定义加载的系统库
12 #ifdef Q_OS_WIN
13 #pragma comment(lib, "user32.lib")
14 #include <qt_windows.h>
15 #endif
16 
17 const ULONG_PTR CUSTOM_TYPE = 10000;// 定义消息常量
18 const QString c_strTitle = "ReceiveMessage";// 定义接收窗体名字
19 
20 class MainWindow : public QMainWindow
21 {
22     Q_OBJECT
23 
24 public:
25     MainWindow(QWidget *parent = nullptr);
26     ~MainWindow();
27 
28     virtual bool nativeEvent(const QByteArray &eventType, void *message, long *result);
29 
30 private:
31     Ui::MainWindow *ui;
32 };
33 #endif // MAINWINDOW_H
View Code

mainwindow.cpp

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 
 4 MainWindow::MainWindow(QWidget *parent)
 5     : QMainWindow(parent)
 6     , ui(new Ui::MainWindow)
 7 {
 8     ui->setupUi(this);
 9 
10     setWindowTitle("ReceiveMessage");
11 }
12 
13 MainWindow::~MainWindow()
14 {
15     delete ui;
16 }
17 
18 bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
19 {
20     MSG *param = static_cast<MSG *>(message);
21     switch (param->message)
22     {
23     case WM_COPYDATA:
24     {
25         COPYDATASTRUCT *cds = reinterpret_cast<COPYDATASTRUCT*>(param->lParam);
26         if (cds->dwData == CUSTOM_TYPE)
27         {
28             QString strMessage = QString::fromUtf8(reinterpret_cast<char*>(cds->lpData), cds->cbData);
29             QMessageBox::information(this, QStringLiteral("提示"), strMessage);
30             *result = 1;
31             return true;
32         }
33     }
34     }
35 
36     return QWidget::nativeEvent(eventType, message, result);
37 }
View Code
原文地址:https://www.cnblogs.com/FKdelphi/p/14153857.html