C++ 自定义消息两种方式

#define WM_MY_DEFINED_MSG (WM_USER + 101)

#define MY_REGISTERED_MSG_STR _T("MY_MSG_STRING")
CDemoDlg::CDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CDemoDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	WM_MY_REGISTERED_MSG = RegisterWindowMessage(MY_REGISTERED_MSG_STR);
}
ON_MESSAGE(WM_MY_DEFINED_MSG, &CDemoDlg::OnMyDefinedMsg)
	ON_REGISTERED_MESSAGE(WM_MY_REGISTERED_MSG,&CDemoDlg::OnMyRegisteredMsg)
void CDemoDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	PostMessage(WM_MY_DEFINED_MSG,0,0);
}

LRESULT CDemoDlg::OnMyDefinedMsg(WPARAM wParam, LPARAM lParam)
{
	TCHAR szBuf[128] = {0};
	_stprintf(szBuf,_T("收到自定义消息: WM_MY_DEFINED_MSG,ID=0x%X."),WM_MY_DEFINED_MSG);
	SetDlgItemText(IDC_OUTPUT,szBuf);
	return 0;
}

LRESULT CDemoDlg::OnMyRegisteredMsg(WPARAM wParam, LPARAM lParam)
{
	TCHAR szBuf[128] = {0};
	_stprintf(szBuf,_T("收到Registered消息: WM_MY_REGISTERED_MSG,ID=0x%X."),WM_MY_REGISTERED_MSG);
	SetDlgItemText(IDC_OUTPUT,szBuf);
	return 0;
}


void CDemoDlg::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	PostMessage(WM_MY_REGISTERED_MSG,0,0);
}

 

上一篇:Charles使用30分钟自动关闭解决方法


下一篇:URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs)