qt 给win32 发送消息很简单,但是要获取windows 消息却十分复杂,最后想了一个不是很完美 但是也是以现在本人能力所能实现的唯一途径了,基本原理是 利用vc编写一个mfc 的dll,这个dll 中创建一个透明窗体,利用这个dll 获取win32 API 消息。
源码 已经在vs2010 vs6.0 qt 4.7 下试验通过
下面贴出 重要的实现源码:
VC dll- ReceiveMessage.cpp
#include "stdafx.h"
#include "resource.h"
typedef int (*CALLBACKFUNC)(DWORD Type, DWORD position);
CALLBACKFUNC pfnCallback_tmp;
HINSTANCE hInstance;
unsigned longWM_UNITOKEN_NOTIFY = RegisterWindowMessage("notify_HuFeng");
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
hInstance = (HINSTANCE)hModule;
break;
}
return TRUE;
}
HWND m_hWnd = 0;
unsigned long nStep = 0;
unsigned long TotalStep = 0;
//INT_PTR __stdcall
BOOL CALLBACK DialogProcedure(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
case WM_CREATE:{
break;};
case WM_COMMAND:
{
SendMessage(hWnd, WM_DESTROY, 0, 0);
break;
};
case WM_DESTROY:{PostQuitMessage(0);break;};
case WM_INITDIALOG : { break;};
default :{
if(Message==WM_UNITOKEN_NOTIFY)
{pfnCallback_tmp(wParam,lParam);};break;}
}
return 0;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message==WM_DESTROY)
{
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
DWORD __stdcall ThreadFuction(LPVOID pParam)
{
MSG Msg;
HWND hWnd_HuFeng= CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOGH), NULL, DialogProcedure);
int erro = GetLastError();
ShowWindow(hWnd_HuFeng, SW_HIDE);
while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return 0;
}
extern "C" void __stdcall ReceiveMessageFun(CALLBACKFUNC pfnCallback)
{
DWORDThreadID;
HANDLE m_hThread;
pfnCallback_tmp = pfnCallback;
m_hThread=CreateThread(NULL, 0, ThreadFuction, NULL, 0, &ThreadID);
CloseHandle(m_hThread);
}
QT 接收部分
头文件.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "windows.h"
#include "qthread.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void changeEvent(QEvent *e);
private:
Ui::MainWindow *ui;
};
class MyThread : public QThread
{
public:
void run();
void foo();
};
#endif // MAINWINDOW_H
QT 接收部分
receive.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QtCore/qobject.h>
#include <limits.h>
#include "QLibrary"
typedef int (* CALLBACKFUNC)(unsigned long Type, unsigned long position);
//extern "C" void __stdcall ReceiveMessageFun(CALLBACKFUNC pfnCallback);
CALLBACKFUNC pfnCallback;
int outputMsg(unsigned long Type, unsigned long position)
{
qDebug("Received Message");
return 0;
}
int main(int argc, char *argv[])
{
int m;
QApplication a(argc, argv);
//MainWindow w;
// outputMsg(12,34);
MyThread aa;
// aa.start();
aa.run();
// w.show();
return a.exec();
}
void MyThread::run()
{
QLibrary myLib("../ReceiveMessage");
if(myLib.load())
{
qDebug("link lib success");}
else
{
qDebug("link lib failed");
}
pfnCallback = outputMsg;
typedef void(*ReceiveMessageFun_HF)(CALLBACKFUNC pfnCallback);//库版本
ReceiveMessageFun_HF pGetLibraryVerion =(ReceiveMessageFun_HF)myLib.resolve("ReceiveMessageFun");
pGetLibraryVerion(pfnCallback);
// sleep(1);
}
QT 发送部分 头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "windows.h"
#include "stdlib.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void changeEvent(QEvent *e);
void _SendNotify(DWORD fuccode,DWORD retcode);
private:
Ui::MainWindow *ui;
DWORD a,b;
private slots:
void on_pushButton_clicked();
};
#endif // MAINWINDOW_H
QT 发送部分
receive.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QFileDialog"
#include "QDebug"
#include "QTextCodec"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::on_pushButton_clicked()
{
a=200;
b=50;
MainWindow::_SendNotify(a, b);
}
void MainWindow::_SendNotify(DWORD fuccode, DWORD retcode)
{
unsigned long WM_UNITOKEN_NOTIFY=::RegisterWindowMessageW(L"notify_HuFeng");
PostMessageW(HWND_BROADCAST, WM_UNITOKEN_NOTIFY, fuccode, retcode);
}
代码不尽完善 只是提供一个思路
分享到:
相关推荐
Qt 获取Windows内存信息
windows平台下,libusb-win32库的使用。以及在Qt编译环境下的HID通信
最近要在windows下获取CPU的温度信息,网上有些介绍CPU温度获取方法的文档,也有VS的源代码,但是运行后总是显示固定的100度,我的开发环境是Qt5.9.9, 64位的win10,四核CPU,由于无法等待、时间紧急,只能自己动手...
win32 调用 含qt界面的DLL.win32程序,https://blog.csdn.net/libin88211/article/details/38183791
基于qt5.1写的,在windows平台的系统热键的注册与注销...直接调用了win32的api。 事例中,使用ctrl + shift + alt + s,四个按键,组合呼出和隐藏主窗口。 事件过滤类,继承了QObject 和 QAbstractNativeEventFilter。
windows下qt获取所有桌面窗口、类名和标题,辅助开发的小工具 qDebug()窗口句柄:"窗口类名:"窗口标题:";
VerQueryValue 获取文件详细信息。 Qt API获取exe版本等详细信息 https://blog.csdn.net/scz653037148/article/details/83307252
Qt界面最小化获取键盘鼠标事件-windows消息-windows_hook
基于QT和windows api实现的远程桌面控制(包括客户端和服务端),使用vlc-qt进行屏幕画面数据的传输和显示
使用qt调用windows api获取U盘信息,并可以拔插自动更新,调用cmd fomat实现U盘的格式化
在Qt5.6.1环境下基于WindowsAPI的按键精灵,需要源码的留言,仅供学习使用。
Qt Windows 休眠唤醒信号
Qt实现串口通信(利用windows API实现串口枚举,多线程、生产者消费者模型实现数据解析); 自定义事件,向UI界面提供数据交互接口; 自定义队列,支持线程安全。
QT 获取 进程名、PID、CPU时间、内存、用户对象、GDI对象,获取盘符的容量和监控硬盘的插拔
QT实现获取USB信息,显示到界面,含完整工程
QTAPI,指导QT开的有用文档。 QTAPI,指导QT开的有用文档。
QT利用hidapi操作hid设备的例子,工程中已含hidapi.h,hidapi.lib,hidapi.dll,hidapi版本0.7.0
Qt API4_3_4Qt API4_3_4Qt API4_3_4Qt API4_3_4Qt API4_3_4Qt API4_3_4Qt API4_3_4Qt API4_3_4Qt API4_3_4Qt API4_3_4Qt API4_3_4Qt API4_3_4Qt API4_3_4Qt API4_3_4Qt API4_3_4Qt API4_3_4Qt API4_3_4Qt API4_3_4Qt...
Qt5案例之利用QProcess获取计算机硬件信息,介绍利用QProcess获取计算机的CPU、主板、硬盘等电脑相关硬件信息。windows提供了“wmic”(Windows Management Instrumentation,Windows管理工具),提供了从命令行接口...
一个例子:通过Windows窗口消息实现 QT进程间通信,实现消息在进程间互发