假设现在在某一对话框上有一id为IDC_BUTTON_DELETEMAP的按钮资源;并且已经创建了该对话框的对话框类;现在要实现的效果是:当地图列表中有某一项被选中时,删除按钮有效,否则按钮无效;
界面如图1所示:
图1
此时通过菜单View->ClassWizard打开MFC Wizard,选中其中的member variables标签,选择class为该对话框的类,然后选中IDC_BUTTON_DELETEMAP,点击AddVariable 按钮,为该id添加一个名为m_DeleteButton,category为Control,类型为CButton的变量;
剩下的工作较为简单,就是在地图列表NM_SETFOCUS事件的响应函数中添加如下代码:
1void CMapViewerDialog::OnSetfocusTreeMaptree(NMHDR* pNMHDR, LRESULT* pResult)
2{
3 // TODO: Add your control notification handler code here
4 if(m_MapTree.GetSelectedItem()!=NULL)
5 m_DeleteButton.EnableWindow();
6 else
7 m_DeleteButton.EnableWindow(FALSE);
8 *pResult = 0;
9}
这样就ok了。
2{
3 // TODO: Add your control notification handler code here
4 if(m_MapTree.GetSelectedItem()!=NULL)
5 m_DeleteButton.EnableWindow();
6 else
7 m_DeleteButton.EnableWindow(FALSE);
8 *pResult = 0;
9}