Qt sends messages to Win32 very easily, but it is very complicated to obtain Windows messages. Finally, I thought of a method that is not perfect, but it is also the only way that I can achieve it now, the basic principle is to use VC to compile an mfc dll, which creates a transparent form and uses this DLL to obtain Win32 API messages.
The source code has been tested in vs2010 vs6.0 QT 4.7
Below is an important implementation source code:
Vc dll-receivemessage. cpp
# Include "stdafx. H"
# Include "resource. H"
Typedef int (* callbackfunc) (DWORD type, DWORD position );
Callbackfunc pfncallback_tmp;
Hinstance;
Unsigned long wm_unitoken_notify = registerwindowmessage ("yy_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, uint message, wparam, 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_policy)
{Pfncallback_tmp (wparam, lparam) ;}; break ;}
}
Return 0;
}
Lresult callback wndproc (hwnd, uint message, wparam, lparam)
{
If (Message = wm_destroy)
{
Postquitmessage (0 );
Return 0;
}
Return defwindowproc (hwnd, message, wparam, lparam );
}
DWORD _ stdcall threadfuction (lpvoid pparam)
{
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)
{
DWORD threadid;
Handle m_hthread;
Pfncallback_tmp = pfncallback;
M_hthread = createthread (null, 0, threadfuction, null, 0, & threadid );
Closehandle (m_hthread );
}
Qt Receiver
Header file. 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 Receiver
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.exe C ();
}
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); // library version
Receivemessagefun_hf pgetlibraryverion = (receivemessagefun_hf) mylib. Resolve ("receivemessagefun ");
Pgetlibraryverion (pfncallback );
// Sleep (1 );
}
Some header files sent by 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 Transmission
Receive. cpp
# Include "mainwindow. H"
# Include "ui_main1_1_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_policy =: registerwindowmessagew (L "policy_hufeng ");
Postmessagew (hwnd_broadcast, wm_unitoken_notify, fuccode, retcode );
}
The code is not complete, but it provides an idea.