禁用CMFCToolbar中的按钮 [英] disable buttons in CMFCToolbar
问题描述
我希望能够自定义"我的工具栏,具体取决于哪种类型的子框架在MDI应用程序中处于活动状态.我一直在使用CMFCToolbar,因为它们看起来不错,Visual Studio会朝着这个方向引导您.
我无法确定如何自动执行此操作(例如,当文档类型更改时菜单也会更改),因此我尝试为每种文档类型创建独立的工具栏-并在OnMDIActivate()中进行切换-但这导致CMFCToolbar出现了太多麻烦,包括:
-工具栏尺寸错误
-工具栏未显示
-重新加载应用程序并且工具栏格式错误(停靠/取消停靠),即使我禁用了LoadState/SaveState
-工具栏过去的空白-有时图像仍然存在,但工具栏不存在(不重新绘制框架)
..所以我放弃了. [< flame"恕我直言,很难自定义使用CMFCToolbar,因为它缺乏可预测的行为,易于阅读代码,文档等.]
我现在想做的只是简单地使用* one *工具栏,但是将某些文档类型的某些按钮变成灰色,例如:
I would like to be able to "customize" my toolbar depending on what type of child frame becomes active in my MDI application. I have been using CMFCToolbar because they look nice and Visual Studio steers you in that direction.
I could not determine how to do it automatically (like the menus change when the document type changes) so I tried creating independent toolbars for each document type - and switching them in OnMDIActivate() - but that caused too many hassles with CMFCToolbar, including:
- toolbars the wrong size
- toolbars not showing up
- reload app and the toolbar is in the wrong form (docked/undocked)even though I disabled LoadState/SaveState
- gaps where the toolbar used to be - sometimes the image is still there but the toolbar is not (no repaint of the frame)
.. so I gave up on that. [<flame>IMHO it is shockingly hard to make customized use of CMFCToolbar viz its lack of predictable behavior, ease of reading the code, documentation, etc.]
What I would like to do now is simply use *one* toolbar but gray-out certain buttons for certain document types, something like:
void CChildFrame::OnMDIActivate(BOOL bActivate, CWnd* pActivateWnd, CWnd* pDeactivateWnd)
{
CMDIChildWndEx::OnMDIActivate(bActivate, pActivateWnd, pDeactivateWnd);
// which Doc type are we now working with?
if (bActivate) {
CView *theView = ((CChildFrame *)pActivateWnd)->GetActiveView();
if (theView == NULL) return;
CMainFrame *MF = (CMainFrame *)theApp.m_pMainWnd;
CMFCToolBar *TB = &MF->m_wndToolBar;
if (theView->IsKindOf ( RUNTIME_CLASS (CDrumGenView))) {
// disable certain toolbar buttons
}
else if (theView->IsKindOf ( RUNTIME_CLASS (CDrumGenListingView))) {
// disable certain toolbar buttons
}
else if (theView->IsKindOf ( RUNTIME_CLASS (CDrumGenStaffView))) {
// disable certain toolbar buttons
//TB->GetToolBarCtrl().EnableButton(ID_FILE_OPEN, FALSE);
}
else if (theView->IsKindOf ( RUNTIME_CLASS (CDrumGenLogView))) {
// disable certain toolbar buttons
}
}
如您所见,我在那里有一个EnableButton(FALSE)调用...但是等等,在CMFCToolbar中不再存在.
任何人都可以向我指出禁用CMFCToolbar按钮的超级简单方法吗?
Thx
As you can see, I have a EnableButton(FALSE) call in there ... but wait, that doesn''t exist any more in CMFCToolbar.
Can anyone point me to a super simple way to disable a CMFCToolbar button?
Thx
推荐答案
Venkat和Anna,
感谢您的答复.以下代码似乎可以解决下面提到的一个警告(我决定坚持使用CMFCToolBar):
Venkat and Anna,
Thanks for the replies. The following code seems to work with one caveat mentioned below (I decided to stick with CMFCToolBar):
void CMyToolBar::EnableButton (int iCmd, BOOL bStatus)
{
// iCmd is not the index, but the command ID - this way the
// buttons can be re-arranged and this routine still works
int idx = CommandToIndex (iCmd);
if (idx == -1) return; // not in this toolbar
UINT iStyle = GetButtonStyle (idx);
CMFCToolBarButton *Button = GetButton (idx);
if (bStatus) { // enable
SetButtonStyle (idx, iStyle & !TBBS_DISABLED);
}
else {
SetButtonStyle (idx, iStyle | TBBS_DISABLED);
}
Button->EnableWindow (bStatus);
}
void CMyToolBar::OnUpdateCmdUI(CFrameWnd *pFrame, BOOL bDisableIfNoHndler)
{
CBasePane::OnUpdateCmdUI(pFrame, bDisableIfNoHndler);
CMainFrame *MF = (CMainFrame *)pFrame;
for (int i=0; i<NUMTOOLBUTTONS; i++)
EnableButton (MF->m_ToolBarStatus[i].iCmd, MF->m_ToolBarStatus[i].bEnabled);
}
我在ToolBarStatus []中保留每个按钮的启用/禁用状态.现在这些按钮已正确变灰..但是它们仍然处于活动状态(至少其中一些);换句话说,EnableWindow()调用似乎没有任何作用.有进一步的想法吗?
谢谢,
I keep the enabled/disabled status of each button in ToolBarStatus[]. The buttons are Grayed out correctly now .. however they are still active (at least some of them); in other words, the EnableWindow() call does not seem to have any effect. Further thoughts?
Thx,
请使用TBBS_DISABLED样式进行检查,
CMFCToolBar :: SetButtonStyle .但这将使用从零开始的按钮索引.
Check this with TBBS_DISABLED style,
CMFCToolBar::SetButtonStyle.But this will use zero based button index.
auto_ptr< HBITMAP>
auto_ptr< HBITMAP>
auto_ptr<HBITMAP>
auto_ptr<HBITMAP>
这篇关于禁用CMFCToolbar中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!