如何在 MFC 中的 CListCtrl
中禁用多个项目的工具栏按钮。当前的实现是,如果没有元素存在,那么该按钮将被禁用。现在,如果选择了多个项目,则需要扩展该功能,然后需要禁用该按钮。
Void CMainFrame::OnUpdate( CCmdUI* pCmdUI)
{
if(I_count==0)//if no items are present
{
pCmdUI->Enable(false);
return;
}
}
请建议如何禁用多选按钮
最佳答案
只需使用:CListCtrl::GetSelectedCount()
即可检索 ListView 控件中选定项的数量。
所以你的实现看起来像这样:
void CMainFrame::OnUpdate(CCmdUI* pCmdUI)
{
CMyListView* pView = (CMyListView*) ((CFrameWnd*) AfxGetMainWnd ())->GetActiveView ();
int nSel = pView->GetListCtrl().GetSelectedCount();
if(nSel == 0 || nSel > 1)
pCmdUI->Enable(FALSE);
else
pCmdUI->Enable(TRUE);
}
当然你应该添加一些错误处理来确保窗口被初始化:
if (pWnd != NULL && pWnd->GetSafeHwnd() != NULL)
{
// TODO: safe to call HWND methods
}
如 Constantine Georgiou 所建议的更好的设计,如果将所有与 View 相关的代码移动到 View 类(包括 OnUpdateUI
处理程序),将会更加清晰。
关于c++ - 在 MFC 的 CListCtrl 中禁用多个项目的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40202156/