BOOL MyRealButton::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
// 捕获鼠标左键按下和松开状态,上报父窗口
switch(message)
{
case WM_LBUTTONDOWN:
::SendMessage(this->GetParent()->m_hWnd, 9999, 1, 0);
break;
case WM_LBUTTONUP:
::SendMessage(this->GetParent()->m_hWnd, 9999, 0, 0);
break;
default:
break;
}
return CButton::OnWndMsg(message, wParam, lParam, pResult);
}(3)重写主对话框(按钮的父窗口)的OnMsg()虚函数,捕获按钮发出的消息进行处理。BOOL CRealButtonDlg::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
switch(message)
{
case 9999:
if(wParam == 1) // 按钮被鼠标左键按下
{
GetDlgItem(IDC_STATIC)->SetWindowTextW(L"按钮被按下了");
}
else if(wParam==0) // 按钮被鼠标左键抬起
{
GetDlgItem(IDC_STATIC)->SetWindowTextW(L"按钮抬起了");
}
break;
default:
break;
}
return CDialog::OnWndMsg(message, wParam, lParam, pResult);
}(4)把m_btn变量的类型改为MyRealButton。如下所示:
//CButton m_btn; MyRealButton m_btn;这样,这个按钮控件的行为就被我们新建的MyRealButton类控制了。
原文:http://blog.csdn.net/smstong/article/details/19094921