更改 CMFCToolbar 中菜单按钮的图像
我在 CMFCToolbar 中有一个菜单按钮,每次在菜单中选择不同的条目时,我想替换按钮的位图,因为每个条目都有自己的图标。
我成功使用 CMFCToolBarMenuButton::SetImage 更改图标,但它也更改了菜单条目中的图标。 太糟糕了。
替代文本 http://www.freeimagehosting.net/uploads/137269b0f2.jpg < a href="http://www.freeimagehosting.net/uploads/879d03843a.jpg" rel="nofollow noreferrer">替代文本 http://www.freeimagehosting.net/uploads/879d03843a.jpg
这里是代码示例:
if ( (pToolbar != NULL) && (idBase != 0) )
{
int ixButtonToReplace = pToolbar->CommandToIndex(idBase);
CMFCToolBarMenuButton* pBtnToReplace = dynamic_cast<CMFCToolBarMenuButton*>
(pToolbar->GetButton(ixButtonToReplace));
if ( pBtnToReplace )
{
const CObList& listCommands = pBtnToReplace->GetCommands();
POSITION pos = listCommands.GetHeadPosition();
while ( pos != NULL )
{
CMFCToolBarMenuButton* pItem = (CMFCToolBarMenuButton*) listCommands.GetNext(pos);
if ( pItem && (pItem->m_nID == idButtonToReplaceWith) )
{
pBtnToReplace->SetImage(pItem->GetImage());
}
}
}
}
有什么想法吗? 谢谢。
I have a menu button inside a CMFCToolbar
and I would like to replace the bitmap of the button each time a different entry is selected in the menu as each entry has its own icon.
I succeed in changing the icon using CMFCToolBarMenuButton::SetImage
but it changes the icon in the menu entry too. Too bad.
alt text http://www.freeimagehosting.net/uploads/137269b0f2.jpg alt text http://www.freeimagehosting.net/uploads/879d03843a.jpg
Here is a sample of code:
if ( (pToolbar != NULL) && (idBase != 0) )
{
int ixButtonToReplace = pToolbar->CommandToIndex(idBase);
CMFCToolBarMenuButton* pBtnToReplace = dynamic_cast<CMFCToolBarMenuButton*>
(pToolbar->GetButton(ixButtonToReplace));
if ( pBtnToReplace )
{
const CObList& listCommands = pBtnToReplace->GetCommands();
POSITION pos = listCommands.GetHeadPosition();
while ( pos != NULL )
{
CMFCToolBarMenuButton* pItem = (CMFCToolBarMenuButton*) listCommands.GetNext(pos);
if ( pItem && (pItem->m_nID == idButtonToReplaceWith) )
{
pBtnToReplace->SetImage(pItem->GetImage());
}
}
}
}
Any ideas? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它开箱即用。 您唯一需要的是调用 CMFCToolBar::AddToolBarForImageCollection 以便 MFC 知道要使用哪些图像。
It works out-of-box. The only you need is to call
CMFCToolBar::AddToolBarForImageCollection
so MFC could know which images to use.不确定菜单按钮也发生了变化是什么意思?
如果通过单个 setImage 调用更改另一个按钮,则明显表明它们共享某种资源 ID,唯一的解决方案是确保它们具有不同的 ID(这需要确保两个资源单独处理)。
但我已经很久没有乱搞MFC资源文件来确认这一点了。
Not sure what you mean by the menu button is changed too?
If another button is changed with the single setImage call to the obvious indication is they share a resource ID of some sort, the only solution would be to ensure they have different ID's (which would require making sure both resources are handled separately).
But it's a long time since I messed in MFC resource files to confirm this.