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

运行程序

相关推荐
MC皮蛋侠客36 分钟前
TDengine C++ 系列(11):性能模型与调优——从测量到优化
c++·php·tdengine
兔兔兔兔11 小时前
记录C++ 3
开发语言·c++
代码地平线1 小时前
C++类与对象(中):默认成员函数与日期类实战
c++·笔记·算法
hansang_IR1 小时前
【题解】可持久化区间仿射区间和(Persistent Range Affine Range Sum)
c++·算法·线段树
瑞码空间2 小时前
01背包:动态规划的Hello World
c++·算法·0/1背包问题
鱼子星_2 小时前
【C++】反向迭代器:反向迭代器的底层认识与模拟实现
开发语言·c++·笔记·stl
郝学胜-神的一滴2 小时前
[简化版 GAMES 104] 现代游戏引擎 05:游戏引擎世界构建核心机制深度解析
c++·程序人生·unity·游戏引擎·计算机图形学·opengl
Darkwanderor3 小时前
C++的流简介和简单使用
开发语言·c++
乐观勇敢坚强的老彭3 小时前
C++ STL 常用容器的速查表
java·c++·算法
lingran__3 小时前
C++ STL map 与 set 底层剖析与模拟实现万字详解|基于红黑树,复刻 SGI-STL 泛型复用架构
开发语言·c++·stl·set·map·泛型编程·sgi-stl