zoukankan      html  css  js  c++  java
  • qt 获取windows 的消息(通过MFC的DLL的透明窗体转发消息)good

    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 long WM_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)

    {

       DWORD ThreadID;

       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);
    }
     

    代码不尽完善 只是提供一个思路

    http://blog.csdn.net/hufengvip/article/details/5974814

  • 相关阅读:
    [IOI2013]Dreaming
    Lost Cows
    Mobile Service
    [POI2005]Bank notes
    [CTSC2007]动物园zoo
    [CF1093F]Vasya and Array
    [雅礼集训 2017 Day1]市场
    [APIO2014]序列分割
    [CEOI2004]锯木厂选址
    [APIO2010]特别行动队
  • 原文地址:https://www.cnblogs.com/findumars/p/5317880.html
Copyright © 2011-2022 走看看