注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

沙漠里de烟雨

原创分享,禁止转载

 
 
 

日志

 
 

qt下CopyData--不同语言写的程序进程间消息通信  

2017-05-11 03:22:08|  分类: QT5.x与QML |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
QtCopyDate工程:
main.cpp =>
#include "qtcopydate.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QtCopyDate w;
w.show();
return a.exec();
}

qtcopydate.h =>
#ifndef QTCOPYDATE_H
#define QTCOPYDATE_H

#include <QtWidgets/QDialog>
class QPushButton;
class QLabel;

class QtCopyDate : public QDialog
{
Q_OBJECT

public:
QtCopyDate(QWidget *parent = 0);
~QtCopyDate();
protected: //Qt5将Qt4的winEvent改成nativeEvent;
bool nativeEvent(const QByteArray &eventType, void *message, long *result);

private slots:
void SendMessageToWTLSlot();

private:
QPushButton* m_SendToWtlBtn;
QLabel* m_FromWtlLabel;
};

#endif // QTCOPYDATE_H


qtcopydate.cpp =>
#include "qtcopydate.h"

#include <QPushButton>
#include <QLabel>
#include <QLayout>
#include <QByteArray>
#include <string>

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



QtCopyDate::QtCopyDate(QWidget *parent) : QDialog(parent)
{
m_FromWtlLabel = new QLabel("From WTL:",this);
m_FromWtlLabel->setAlignment(Qt::AlignCenter);
m_SendToWtlBtn = new QPushButton("Send \"Qt:hello,WTL!\" to WTL");
QVBoxLayout* mainLayout = new QVBoxLayout;
mainLayout->addWidget(m_FromWtlLabel);
mainLayout->addWidget(m_SendToWtlBtn);
this->setLayout(mainLayout);
this->resize(200,100);

connect(m_SendToWtlBtn,SIGNAL(clicked()),this,SLOT(SendMessageToWTLSlot()));
}

QtCopyDate::~QtCopyDate()
{

}

bool QtCopyDate::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
Q_UNUSED(eventType);
if(eventType=="windows_generic_MSG")
{
MSG* msg = static_cast<MSG*>(message);
if(msg->message==WM_COPYDATA)
{
COPYDATASTRUCT* data = reinterpret_cast<COPYDATASTRUCT*>(msg->lParam);
m_FromWtlLabel->setText(QString(static_cast<char*>(data->lpData)));

return true;
}
}
return false;
}

void QtCopyDate::SendMessageToWTLSlot()
{
HWND hWndRcv = ::FindWindowW(NULL,L"WtlCopyData");
if(hWndRcv)
{
std::string str("Qt:hello,WTL!");
COPYDATASTRUCT cpd;
cpd.dwData = 0;
cpd.cbData = str.length()+1;
cpd.lpData = (void*)(str.c_str());
::SendMessage(hWndRcv,WM_COPYDATA,(WPARAM)winId(),(LPARAM)&cpd);
}
}


WtlCopyData工程:(WTL)

mainDlg.h =>
// MainDlg.h : interface of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////

#pragma once
#include <string>

class CMainDlg : public CDialogImpl<CMainDlg>, public CUpdateUI<CMainDlg>,
public CMessageFilter, public CIdleHandler
{
public:
enum { IDD = IDD_MAINDLG };

virtual BOOL PreTranslateMessage(MSG* pMsg)
{
return CWindow::IsDialogMessage(pMsg);
}

virtual BOOL OnIdle()
{
return FALSE;
}

BEGIN_UPDATE_UI_MAP(CMainDlg)
END_UPDATE_UI_MAP()

BEGIN_MSG_MAP(CMainDlg)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
MESSAGE_HANDLER(WM_COPYDATA, OnCopyData)
COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
COMMAND_ID_HANDLER(IDOK, OnOK)
COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
COMMAND_ID_HANDLER(IDC_BUTTON_SEND_TO_QT, OnSendToQtMsg)
END_MSG_MAP()

// Handler prototypes (uncomment arguments if needed):
// LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
// LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
// LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
// center the dialog on the screen
CenterWindow();

// set icons
HICON hIcon = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON));
SetIcon(hIcon, TRUE);
HICON hIconSmall = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON));
SetIcon(hIconSmall, FALSE);

// register object for message filtering and idle updates
CMessageLoop* pLoop = _Module.GetMessageLoop();
ATLASSERT(pLoop != NULL);
pLoop->AddMessageFilter(this);
pLoop->AddIdleHandler(this);

UIAddChildWindowContainer(m_hWnd);


return TRUE;
}

LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
// unregister message filtering and idle updates
CMessageLoop* pLoop = _Module.GetMessageLoop();
ATLASSERT(pLoop != NULL);
pLoop->RemoveMessageFilter(this);
pLoop->RemoveIdleHandler(this);

return 0;
}
LRESULT OnCopyData(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
{
COPYDATASTRUCT* data = (COPYDATASTRUCT*)lParam;
LPWSTR text = ConvertCharToLPWSTR((const char*)data->lpData);
::SetWindowText(GetDlgItem(IDC_STATIC_FROM_QT),text);
return 0;
}
LRESULT OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
CAboutDlg dlg;
dlg.DoModal();
return 0;
}

LRESULT OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
// TODO: Add validation code 
CloseDialog(wID);
return 0;
}

LRESULT OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
CloseDialog(wID);
return 0;
}
LRESULT OnSendToQtMsg(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
HWND hWndRcv = ::FindWindow(NULL,_T("QtCopyDate"));
 
COPYDATASTRUCT data;
std::string str("WTL:hello,Qt!");
data.dwData = 0;
data.cbData = str.length()+1;
data.lpData = (void*)str.c_str();
::SendMessage(hWndRcv,WM_COPYDATA,(WPARAM)m_hWnd,(LPARAM)&data);
return 0;
}
void CloseDialog(int nVal)
{
DestroyWindow();
::PostQuitMessage(nVal);
}
LPWSTR ConvertCharToLPWSTR(const char * szString)
{
int dwLen = strlen(szString) + 1;
int nwLen = MultiByteToWideChar(CP_ACP, 0, szString, dwLen, NULL, 0);//算出合适的长度;
LPWSTR lpszPath = new WCHAR[dwLen];
MultiByteToWideChar(CP_ACP, 0, szString, dwLen, lpszPath, nwLen);
return lpszPath;
}
};

WtlCopyData.cpp =>
// WtlCopyData.cpp : main source file for WtlCopyData.exe
//

#include "stdafx.h"

#include <atlframe.h>
#include <atlctrls.h>
#include <atldlgs.h>

#include "resource.h"

#include "aboutdlg.h"
#include "MainDlg.h"

CAppModule _Module;

int Run(LPTSTR /*lpstrCmdLine*/ = NULL, int nCmdShow = SW_SHOWDEFAULT)
{
CMessageLoop theLoop;
_Module.AddMessageLoop(&theLoop);

CMainDlg dlgMain;

if(dlgMain.Create(NULL) == NULL)
{
ATLTRACE(_T("Main dialog creation failed!\n"));
return 0;
}

dlgMain.ShowWindow(nCmdShow);

int nRet = theLoop.Run();

_Module.RemoveMessageLoop();
return nRet;
}

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
{
HRESULT hRes = ::CoInitialize(NULL);
// If you are running on NT 4.0 or higher you can use the following call instead to 
// make the EXE free threaded. This means that calls come in on a random RPC thread.
// HRESULT hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
ATLASSERT(SUCCEEDED(hRes));

// this resolves ATL window thunking problem when Microsoft Layer for Unicode (MSLU) is used
::DefWindowProc(NULL, 0, 0, 0L);

AtlInitCommonControls(ICC_BAR_CLASSES); // add flags to support other controls

hRes = _Module.Init(NULL, hInstance);
ATLASSERT(SUCCEEDED(hRes));

int nRet = Run(lpstrCmdLine, nCmdShow);

_Module.Term();
::CoUninitialize();

return nRet;
}
  评论这张
 
阅读(9)| 评论(0)
推荐

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017