同樣使用 SendMessage 來傳遞,所以直接拿上一篇的程式碼做一點修改。
同樣假設有兩個對話框 Send_Dlg 和 Receive_Dlg。
Send_Dlg: Send_Dlg.h 和 Send_Dlg.cpp
Receive_Dlg: Receive_Dlg.h 和 Receive_Dlg.cpp
Send_Dlg.h
// 定義 訊息編號 #define WM_MYMESSAGE_USE_COSTOMIZE_MESSAGE (WM_USER + 2) struct DataStruct { int i; double d; CString m_string; CEdit m_edit; }; class Send_Dlg : public CDialogEx { public: Send_Dlg(CWnd* pParent = NULL); // 要傳遞的結構體 DataStruct dataStruct; // 傳遞訊息的 function afx_msg void sender(); }Send_Dlg.cpp
#include "Send_Dlg.h" Send_Dlg::Send_Dlg(CWnd* pParent /*=NULL*/) : CDialogEx(Send_Dlg::IDD) { m_Icon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void Send_Dlg::sender() { // struct 的值 dataStruct.i = 1; dataStruct.d = 1.0; dataStruct.m_string = L""; dataStruct.m_edit = ""; // 尋找對話框名稱為 Receive_Dlg 的對話框 CWnd* pWnd = CWnd::FindWindowW(NULL, L"Receive_Dlg"); // 傳送訊息 pWnd->SendMessageW(WM_MYMESSAGE_USE_COSTOMIZE_MESSAGE, (WPARAM)&dataStruct); }Receive_Dlg.h
#include "Send_Dlg.h" class Receive_Dlg : public CDialogEx { Receive_Dlg(CWnd* pParent = NULL); // 接收訊息用的 Function afx_msg LRESULT receiver(WPARAM wParam, LPARAM lParam); }Receive_Dlg.cpp
#include "Receive_Dlg.h" Receive_Dlg::Receive_Dlg(CWnd* pParent /*=NULL*/) : CDialogEx(Receive_Dlg::IDD) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } // 在 MESSAGE MAP 中加入關聯 BEGIN_MESSAGE_MAP(Receive_Dlg, CDialogEx) // 以 ON_MESSAGE 將 訊息ID 和 receiver 串起來 ON_MESSAGE(WM_MYMESSAGE_USE_COSTOMIZE_MESSAGE, &Receive_Dlg::receiver) END_MESSAGE_MAP() LRESULT Receive_Dlg::receiver(WPARAM wParam, LPARAM lParam) { // 接受到訊息後的工作 // do something // 接收傳過來的結構體 DataStruct* pDs; pDs = (DataStruct*)wParam; // 取值 pDs->i; pDs->d; pDs->m_string; pDs->m_edit; AfxMessageBox("接收到訊息"); return LRESULT(); }
0 件のコメント:
コメントを投稿