当前位置: 首页 > 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.cnnetsun.cn/news/94778.html

相关文章:

  • 手机端AIDE安卓音乐播放器软件代码
  • 如何在 Windows 11/10 中启用IE浏览器?恢复 Internet Explorer 一键搞定,就这么简单!
  • Scholar Inbox 订阅最新学术进展至邮箱
  • D.二分查找-二分答案-求最小——1283. 使结果不超过阈值的最小除数
  • A.每日一题——3562. 折扣价交易股票的最大利润
  • 圣默思 Teledyne DalsaFilr SWIR相机
  • Go 语言结构
  • JavaScript for 循环详解
  • 5步搞定SillyTavern版本升级:告别烦恼的完整指南
  • 猫头虎AI开源分享:如何批量获取稀土掘金社区文章阅读量暨文章阅读量数据批量提取解决方案
  • DBO-RBF多变量回归预测 优化宽度+中心值+连接权值 (多输入单输出)Matlab代码
  • 亲测!WordPress网站接入聚合登录实践
  • 15、Mozilla模板系统:功能、构建与应用实践
  • Ofd2Pdf完整使用教程:5分钟掌握OFD转PDF的终极技巧
  • 毕业论文操作全流程:以营销类选题为例
  • 20、Mozilla 开发中的脚本、数据结构与数据库支持
  • 小学生学C++编程 (一维数组精讲)
  • 研发绩效评估的关键指标
  • [CISCN2019 华北赛区 Day1 Web2]ikun
  • LobeChat投诉处理建议生成引擎
  • 杨建允:AI搜索优化赋能全链路营销的全流程
  • AI原生应用中的长尾用户意图理解解决方案
  • 23、Vim 多文件查找替换与全局命令使用技巧
  • 如何避免MySQL死锁?资深DBA的9条黄金法则
  • arcpy导出excel表
  • 视频硬字幕AI去除终极方案:本地化无损修复技术详解
  • BetterNCM插件完整教程:从零开始打造你的专属音乐工作站
  • 大模型注意力机制全解析:从MHA到MoBA,一文掌握七种核心算法
  • LobeChat能否实现AI调酒师?饮品配方创意与口味偏好匹配
  • 如何快速绕过iOS激活锁:AppleRa1n完整解决方案指南