news 2026/3/26 22:25:46

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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;}

运行程序

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/13 1:48:36

Swagger2Word完整指南:快速将API文档转为专业Word格式

Swagger2Word完整指南&#xff1a;快速将API文档转为专业Word格式 【免费下载链接】swagger2word 项目地址: https://gitcode.com/gh_mirrors/swa/swagger2word Swagger2Word是一款功能强大的开源工具&#xff0c;专门用于将Swagger/OpenAPI接口文档转换为格式规范的Wo…

作者头像 李华
网站建设 2026/3/20 18:24:14

FEMM软件下载与安装

FEMM软件下载与安装 官网下载地址 Finite Element Method Magnetics:Finite Element Method Magnetics Finite Element Method Magnetics / Wiki / Download 下载 安装包非常小, 只有7.5MB. 安装 双击启动可执行程序;点击我接受; 选择安装路径; 选择开始菜单, 保持默认; 开…

作者头像 李华
网站建设 2026/3/24 19:25:16

LobeChat支持Markdown渲染:技术文档输出利器

LobeChat支持Markdown渲染&#xff1a;技术文档输出利器 在今天&#xff0c;一个工程师与AI助手的日常对话可能不再是简单的问答&#xff0c;而是这样一幕&#xff1a;你输入“请帮我写一份关于微服务鉴权方案的技术文档”&#xff0c;几秒钟后&#xff0c;屏幕上跳出一篇结构清…

作者头像 李华
网站建设 2026/3/24 23:53:40

关于数组和指针的一些问题

#include <stdio.h> #include <string.h> int main() {//指针和数组笔试题解析int a[] { 1,2,3,4 };printf("%d\n", sizeof(a));//16 a&a[0]//sizeof(数组名)&#xff0c;计算的是整个数组的大小单位是字节printf("%d\n", sizeof(a0));/…

作者头像 李华
网站建设 2026/3/21 11:49:23

移动端AI图像生成的性能突围:从算力瓶颈到流畅体验

移动端AI图像生成的性能突围&#xff1a;从算力瓶颈到流畅体验 【免费下载链接】denoising-diffusion-pytorch Implementation of Denoising Diffusion Probabilistic Model in Pytorch 项目地址: https://gitcode.com/gh_mirrors/de/denoising-diffusion-pytorch 你是否…

作者头像 李华