当前位置: 首页 > news >正文

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

点击按钮将一个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;}

运行程序

http://www.jsqmd.com/news/101621/

相关文章:

  • 【Hadoop+Spark+python毕设】网络安全入侵数据可视化分析系统、计算机毕业设计、包括数据爬取、数据分析、数据可视化、实战教学
  • LobeChat能否支持播客生成?音频内容创作新模式
  • 数据可视化工具,助你打造好看图表
  • Python 列表(List)详解
  • 一文了解图神经网络研究背景基本概念
  • D.二分查找-二分答案-求最小——1283. 使结果不超过阈值的最小除数
  • 巴菲特的投资时间管理
  • 文本消息发送:构造请求体、API 调用流程及 Go 语言的 Struct 映射实现
  • Motrix浏览器扩展:如何让你的下载速度提升300%?
  • 毕设 stm32与深度学习口罩佩戴检测系统(源码+硬件+论文)
  • 13、Linux文件系统挂载与检查全攻略
  • R 基础语法
  • A.每日一题——3562. 折扣价交易股票的最大利润
  • Obsidian Style Settings 终极指南:如何快速自定义你的笔记界面
  • TradingView图表库深度解析:实时数据流与K线生成实战指南
  • YOLOv11改进 - C3k2融合 | C3k2融合HMHA分层多头注意力机制(CVPR 2025):优化模型在复杂场景下的目标感知能力
  • 百度网盘解析:2025年最实用的下载限速终极解决方案
  • 大数据领域 Eureka 服务的性能瓶颈分析与突破
  • win11灵活控制Python版本,使用pyenv-win
  • 14、Linux 系统中光盘刻录与文件系统创建指南
  • 同样是PPT模板网站,为啥使用PPT模板 大家都选择LFPPT
  • 用户投诉处理指南:LobeChat建议妥善回应
  • Token 缓存策略对比:探讨本地内存、Redis 和数据库缓存的优缺点及适用场景
  • JavaScript for 循环详解
  • 应用页:专为电视与车机优化的轻量级应用管理解决方案
  • 15、Linux系统存储管理与RAID配置指南
  • 20、Mozilla 开发中的脚本、数据结构与数据库支持
  • LobeChat支持哪些大模型?一文看懂全兼容列表
  • 终极指南:免费部署Llama-2-7b-chat-hf打造企业级AI助手
  • Dolby Atmos Lite:轻量级全景声音效模拟工具,多设备音效增强方案