mfc最简单自定义消息投递实例

点击按钮将一个int数据用PostMessage消息投递给出去,弹出MessagBox显示这个数据。核心代码如下。

头文件

cpp 复制代码
    #define WM_MY_MESSAGE (WM_USER + 200)
    
    afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam);

实现文件

cpp 复制代码
ON_MESSAGE(WM_MY_MESSAGE, OnMyMessage)

PostMessage(WM_MY_MESSAGE, 60, 0);	

LRESULT CMyDlg::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
    int nValue = (int)wParam;
    CString strInfo;
	strInfo.Format("%d",nValue);
    MessageBox(strInfo); 
    return 0;
}

完整代码

Simple_PostMessageDlg.h

cpp 复制代码
// Simple_PostMessageDlg.h : header file
//
#define WM_MY_MESSAGE (WM_USER + 200)
#if !defined(AFX_SIMPLE_POSTMESSAGEDLG_H__F9AE712A_93C4_41DB_9DBF_3F981ECB8C6E__INCLUDED_)
#define AFX_SIMPLE_POSTMESSAGEDLG_H__F9AE712A_93C4_41DB_9DBF_3F981ECB8C6E__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/////////////////////////////////////////////////////////////////////////////
// CSimple_PostMessageDlg dialog

class CSimple_PostMessageDlg : public CDialog
{
// Construction
public:
	CSimple_PostMessageDlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
	//{{AFX_DATA(CSimple_PostMessageDlg)
	enum { IDD = IDD_SIMPLE_POSTMESSAGE_DIALOG };
		// NOTE: the ClassWizard will add data members here
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CSimple_PostMessageDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	HICON m_hIcon;

	// Generated message map functions
	//{{AFX_MSG(CSimple_PostMessageDlg)
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	afx_msg void OnButton1();
    afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam);

	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_SIMPLE_POSTMESSAGEDLG_H__F9AE712A_93C4_41DB_9DBF_3F981ECB8C6E__INCLUDED_)

Simple_PostMessageDlg.cpp

cpp 复制代码
// Simple_PostMessageDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Simple_PostMessage.h"
#include "Simple_PostMessageDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSimple_PostMessageDlg dialog

CSimple_PostMessageDlg::CSimple_PostMessageDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSimple_PostMessageDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSimple_PostMessageDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSimple_PostMessageDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSimple_PostMessageDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSimple_PostMessageDlg, CDialog)
	//{{AFX_MSG_MAP(CSimple_PostMessageDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
    ON_MESSAGE(WM_MY_MESSAGE, OnMyMessage)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSimple_PostMessageDlg message handlers

BOOL CSimple_PostMessageDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	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);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSimple_PostMessageDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CSimple_PostMessageDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		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;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSimple_PostMessageDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSimple_PostMessageDlg::OnButton1() 
{
PostMessage(WM_MY_MESSAGE, 60, 0);	
	
}

LRESULT CSimple_PostMessageDlg::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
    int nValue = (int)wParam;
    CString strInfo;
	strInfo.Format("%d",nValue);
    MessageBox(strInfo); 
    return 0;
}

运行程序

相关推荐
hetao173383714 小时前
2025-12-12~14 hetao1733837的刷题笔记
数据结构·c++·笔记·算法
椰子今天很可爱14 小时前
五种I/O模型与多路转接
linux·c语言·c++
程序员zgh15 小时前
C++ 互斥锁、读写锁、原子操作、条件变量
c语言·开发语言·jvm·c++
獭.獭.17 小时前
C++ -- STL【unordered_set和unordered_map的使用】
c++·stl·unordered_map·unordered_set
star _chen18 小时前
C++ std::move()详解:从小白到高手
开发语言·c++
福尔摩斯张18 小时前
C++核心特性精讲:从C语言痛点出发,掌握现代C++编程精髓(超详细)
java·linux·c语言·数据结构·c++·驱动开发·算法
charlie11451419118 小时前
如何快速在 VS2026 上使用 C++ 模块 — 完整上手指南
开发语言·c++·笔记·学习·现代c++
报错小能手19 小时前
STL_unordered_map
开发语言·c++·哈希算法
历程里程碑19 小时前
C++ 9 stack_queue:数据结构的核心奥秘
java·开发语言·数据结构·c++·windows·笔记·算法