MFC关于句柄消息

MFC about handle message

本文关键字:句柄 消息 于句柄 MFC      更新时间:2023-10-16

我引用了about handle消息,并练习实现它

我想发送用户定义的消息,如WM_MESSAGE,从关于对话框使用底部点击发送消息(SendMessage)主对话框接收。在主对话框(HandleMessageDlg.cpp和HandleMessage.h),我创建按钮来显示对话框,在about对话框(aboutdgh .cpp和aboutdgh .h)中,我创建了发送WM_MESSAGE的按钮

实现如下:HandleMessageDlg.cpp

// HandleMessageDlg.cpp : 實作檔
//
#include "stdafx.h"
#include "HandleMessage.h"
#include "HandleMessageDlg.h"
#include "AboutDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// 對 App About 使用 CAboutDlg 對話方塊
class CAboutDlg : public CDialog
{
public:
    CAboutDlg();
// 對話方塊資料
    enum { IDD = IDD_ABOUTBOX };
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支援
// 程式碼實作
protected:
    DECLARE_MESSAGE_MAP()
public:
    afx_msg void OnBnClickedButton1();
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    ON_BN_CLICKED(IDC_BUTTON1, &CAboutDlg::OnBnClickedButton1)
END_MESSAGE_MAP()

// CHandleMessageDlg 對話方塊


CHandleMessageDlg::CHandleMessageDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CHandleMessageDlg::IDD, pParent)
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CHandleMessageDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CHandleMessageDlg, CDialog)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_MESSAGE (WM_MESSAGE, OnHandleMessage)
    //}}AFX_MSG_MAP
    ON_BN_CLICKED(IDC_BUTTON1, &CHandleMessageDlg::OnBnClickedButton1)
END_MESSAGE_MAP()

// CHandleMessageDlg 訊息處理常式
BOOL CHandleMessageDlg::OnInitDialog()
{
    CDialog::OnInitDialog();
    // 將 [關於...] 功能表加入系統功能表。
    // IDM_ABOUTBOX 必須在系統命令範圍之中。
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);
    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        CString strAboutMenu;
        strAboutMenu.LoadString(IDS_ABOUTBOX);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }
    // 設定此對話方塊的圖示。當應用程式的主視窗不是對話方塊時,
    // 框架會自動從事此作業
    SetIcon(m_hIcon, TRUE);         // 設定大圖示
    SetIcon(m_hIcon, FALSE);        // 設定小圖示
    // TODO: 在此加入額外的初始設定
    return TRUE;  // 傳回 TRUE,除非您對控制項設定焦點
}
void CHandleMessageDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
        CAboutDlg dlgAbout;
        dlgAbout.DoModal();
    }
    else
    {
        CDialog::OnSysCommand(nID, lParam);
    }
}
// 如果將最小化按鈕加入您的對話方塊,您需要下列的程式碼,
// 以便繪製圖示。對於使用文件/檢視模式的 MFC 應用程式,
// 框架會自動完成此作業。
void CHandleMessageDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // 繪製的裝置內容
        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
        // 將圖示置中於用戶端矩形
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;
        // 描繪圖示
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialog::OnPaint();
    }
}
// 當使用者拖曳最小化視窗時,
// 系統呼叫這個功能取得游標顯示。
HCURSOR CHandleMessageDlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
}
LRESULT CHandleMessageDlg::OnHandleMessage(WPARAM wParam, LPARAM lParam) {
    TRACE(L"wParam %d, lParam %d", wParam, lParam);
    return 0;
}
void CAboutDlg::OnBnClickedButton1()
{
    // TODO: 在此加入控制項告知處理常式程式碼
    GetParent()->SendMessage(WM_MESSAGE, 0, 0);
}
void CHandleMessageDlg::OnBnClickedButton1()
{
    // TODO: 在此加入控制項告知處理常式程式碼
    CAboutDlg AboutDlg;
    AboutDlg.DoModal();
}

HandleMessageDlg.cpp
HandleMessage.h

// HandleMessageDlg.h : 標頭檔
//
#pragma once
// CHandleMessageDlg 對話方塊
class CHandleMessageDlg : public CDialog
{
// 建構
public:
    CHandleMessageDlg(CWnd* pParent = NULL);    // 標準建構函式
// 對話方塊資料
    enum { IDD = IDD_HANDLEMESSAGE_DIALOG };
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支援
    afx_msg LRESULT OnHandleMessage(WPARAM wParam, LPARAM lParam);
// 程式碼實作
protected:
    HICON m_hIcon;
    // 產生的訊息對應函式
    virtual BOOL OnInitDialog();
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    DECLARE_MESSAGE_MAP()
public:
    afx_msg void OnBnClickedButton1();
};

HandleMessage.h
AboutDlg.cpp

// AboutDlg.cpp : 實作檔
//
#include "stdafx.h"
#include "HandleMessage.h"
#include "HandleMessageDlg.h"
#include "AboutDlg.h"

// AboutDlg 對話方塊
IMPLEMENT_DYNAMIC(AboutDlg, CDialog)
AboutDlg::AboutDlg(CWnd* pParent /*=NULL*/)
    : CDialog(AboutDlg::IDD, pParent)
{
}
AboutDlg::~AboutDlg()
{
}
void AboutDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(AboutDlg, CDialog)    
END_MESSAGE_MAP()
// AboutDlg 訊息處理常式

AboutDlg.cpp
AboutDlg.h

#pragma once
#define  WM_MESSAGE WM_USER+0x100
// AboutDlg 對話方塊
class AboutDlg : public CDialog
{
    DECLARE_DYNAMIC(AboutDlg)
public:
    AboutDlg(CWnd* pParent = NULL);   // 標準建構函式
    virtual ~AboutDlg();
// 對話方塊資料
    enum { IDD = IDD_ABOUTBOX };

protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支援
    DECLARE_MESSAGE_MAP()
};

AboutDlg.h
现在

有问题了

与WM_MESSAGE相关的参数是wParam还是lParamif(lParam == WM_MESSAGE) or if(wParam == WM_MESSAGE)

谢谢

试着把CHandleMessageDlg::放在OnHandleMessage()的定义前面,如下所示:

//      v v v v v v v v v v  Notice the addition of "CHandleMessageDlg::" here
LRESULT CHandleMessageDlg::OnHandleMessage(WPARAM wParam, LPARAM lParam) {
    TRACE(L"wParam %d, lParam %d", wParam, lParam);
    return 0;
}

可以吗?