在运行时设置CMenu项目提示 [英] Set CMenu item prompt at runtime
问题描述
如何在运行时设置CMenu项提示?我知道可以在VS中的资源编辑器中完成此操作,但是我没有这样的资源,无法创建菜单,而且菜单项是动态的.
How can I set a CMenu item prompt at runtime? I know that it can be done in resource editor in VS, but I don't have such resource and create menu and it's items dynamically.
推荐答案
如果使用的是MFC Feature Pack,则需要覆盖MainFrame类的OnMenuButtonToolHitTest
:
If you are using MFC Feature Pack, you will need to override the OnMenuButtonToolHitTest
of your MainFrame class:
BOOL CMainFrame::OnMenuButtonToolHitTest(CMFCToolBarButton* pButton, TOOLINFO* pTI)
{
if(!pButton)
return FALSE;
if(!pTI)
return FALSE;
if (pButton->m_nID == UINT(-1)) //not a menu-item, but an opener menu for a sub-menu
return FALSE;
// Stolen from CMFCToolBar::OnToolHitTest on file afxtoolbar.cpp
// It is not needed to do the GetMessageString part, because it already done
// on function CMFCPopupMenuBar::OnToolHitTest of afxpopupmenubar.cpp file, which
// supplies the two parts to the Tooltip Manager
CString strTipText;
TCHAR szFullText[256];
AfxLoadString(pButton->m_nID, szFullText);
AfxExtractSubString(strTipText, szFullText, 1, '\n');
pTI->lpszText = _tcsdup(strTipText);
return TRUE;
}
您将必须在资源文件中定义与菜单完全相同的ID的字符串.其格式为Prompt text\nPrompt title
.我不确定,但是我认为您唯一可以换行的是将标题和文本分开的行.
You will have to define in your resource file strings with the EXACT same ID as your menus; and their format is Prompt text\nPrompt title
. I am not sure but I think the only new line you can have is the one that separates title from text.
除了使用鼠标悬停菜单时仅显示提示之外,您可能还需要做其他事情.您可以通过覆盖MainFrame
类的OnMenuSelect
来做到这一点:
You may also want to do things beyond simply displaying prompts when hovering menus using the mouse. You can do it by overriding OnMenuSelect
of your MainFrame
class:
void CMainFrame::OnMenuSelect(UINT nItemID, UINT nFlags, HMENU hSysMenu)
{
if (nItemID == ID_MENU_I_WANT_TO_PROCESS)
{
DoThings();
}
__super::OnMenuSelect(nItemID, nFlags, hSysMenu);
}
我建议您对MainFrame类上的GetMessageString
函数进行覆盖,并在其中放置一个断点,以了解流程的进行.
I recommend you to make an override to GetMessageString
function on your MainFrame class and put a breakpoint there for you see how the flow goes.
这篇关于在运行时设置CMenu项目提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!