尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

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

mfc最简单自定义消息投递实例
📅 发布时间:2026/6/17 23:33:57

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

#defineWM_MY_MESSAGE(WM_USER+200)afx_msg LRESULTOnMyMessage(WPARAM wParam,LPARAM lParam);

实现文件

ON_MESSAGE(WM_MY_MESSAGE,OnMyMessage)PostMessage(WM_MY_MESSAGE,60,0);LRESULTCMyDlg::OnMyMessage(WPARAM wParam,LPARAM lParam){intnValue=(int)wParam;CString strInfo;strInfo.Format("%d",nValue);MessageBox(strInfo);return0;}

完整代码
Simple_PostMessageDlg.h

// Simple_PostMessageDlg.h : header file//#defineWM_MY_MESSAGE(WM_USER+200)#if!defined(AFX_SIMPLE_POSTMESSAGEDLG_H__F9AE712A_93C4_41DB_9DBF_3F981ECB8C6E__INCLUDED_)#defineAFX_SIMPLE_POSTMESSAGEDLG_H__F9AE712A_93C4_41DB_9DBF_3F981ECB8C6E__INCLUDED_#if_MSC_VER>1000#pragmaonce#endif// _MSC_VER > 1000/////////////////////////////////////////////////////////////////////////////// CSimple_PostMessageDlg dialogclassCSimple_PostMessageDlg:publicCDialog{// Constructionpublic: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:virtualvoidDoDataExchange(CDataExchange*pDX);// DDX/DDV support//}}AFX_VIRTUAL// Implementationprotected:HICON m_hIcon;// Generated message map functions//{{AFX_MSG(CSimple_PostMessageDlg)virtualBOOLOnInitDialog();afx_msgvoidOnSysCommand(UINT nID,LPARAM lParam);afx_msgvoidOnPaint();afx_msg HCURSOROnQueryDragIcon();afx_msgvoidOnButton1();afx_msg LRESULTOnMyMessage(WPARAM wParam,LPARAM lParam);//}}AFX_MSGDECLARE_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

// Simple_PostMessageDlg.cpp : implementation file//#include"stdafx.h"#include"Simple_PostMessage.h"#include"Simple_PostMessageDlg.h"#ifdef_DEBUG#definenewDEBUG_NEW#undefTHIS_FILEstaticcharTHIS_FILE[]=__FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CAboutDlg dialog used for App AboutclassCAboutDlg:publicCDialog{public:CAboutDlg();// Dialog Data//{{AFX_DATA(CAboutDlg)enum{IDD=IDD_ABOUTBOX};//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CAboutDlg)protected:virtualvoidDoDataExchange(CDataExchange*pDX);// DDX/DDV support//}}AFX_VIRTUAL// Implementationprotected://{{AFX_MSG(CAboutDlg)//}}AFX_MSGDECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg():CDialog(CAboutDlg::IDD){//{{AFX_DATA_INIT(CAboutDlg)//}}AFX_DATA_INIT}voidCAboutDlg::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_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CSimple_PostMessageDlg dialogCSimple_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 Win32m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);}voidCSimple_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_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CSimple_PostMessageDlg message handlersBOOLCSimple_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 dialogSetIcon(m_hIcon,TRUE);// Set big iconSetIcon(m_hIcon,FALSE);// Set small icon// TODO: Add extra initialization herereturnTRUE;// return TRUE unless you set the focus to a control}voidCSimple_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.voidCSimple_PostMessageDlg::OnPaint(){if(IsIconic()){CPaintDCdc(this);// device context for paintingSendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);// Center icon in client rectangleintcxIcon=GetSystemMetrics(SM_CXICON);intcyIcon=GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);intx=(rect.Width()-cxIcon+1)/2;inty=(rect.Height()-cyIcon+1)/2;// Draw the icondc.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.HCURSORCSimple_PostMessageDlg::OnQueryDragIcon(){return(HCURSOR)m_hIcon;}voidCSimple_PostMessageDlg::OnButton1(){PostMessage(WM_MY_MESSAGE,60,0);}LRESULTCSimple_PostMessageDlg::OnMyMessage(WPARAM wParam,LPARAM lParam){intnValue=(int)wParam;CString strInfo;strInfo.Format("%d",nValue);MessageBox(strInfo);return0;}

运行程序

相关新闻

  • 数据可视化工具,助你打造好看图表
  • 一文了解图神经网络研究背景基本概念
  • D.二分查找-二分答案-求最小——1283. 使结果不超过阈值的最小除数

最新新闻

  • 2026年河南食品软包装定制与种子袋生产厂家完全指南:从源头工厂到全国覆盖的深度选型 - 精选优质企业推荐官
  • 等离子处理清洗机主流厂家技术实力实测解析 - 起跑123
  • CNAS实验室认证咨询机构实力排行:五家头部机构盘点 - 起跑123
  • 涿州老王匠全屋定制|全系ENF级高端板材硬核解析,高端家装健康选材首选 - GrowthUME
  • TensorFlow图模式实战:@tf.function性能优化与AutoGraph避坑指南
  • 2026上海破坏计算机信息系统罪律师推荐|网络攻击、数据篡改辩护 - 法律资讯

日新闻

  • 2026年不锈钢卷板厂家推荐排行榜:冷轧热轧/304/201不锈钢卷板,高颜值耐腐蚀源头厂家实力精选 - 企业推荐官【官方】
  • FLUX.1-dev FP8模型实战指南:24GB以下显卡高效部署方案
  • 2026佛山长途搬家价目表:跨省跨市搬家费用完整计算指南 - 从来都是英雄出少年

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号