英文:
Conditionally set the mouse cursor on a CMFCMenuButon control
问题
我在我的对话框上有一个标准的CMFCMenuButton
控件:
CONTROL "Congregation Link",IDC_MFCMENUBUTTON_CONGREGATION_LINK,
"MfcMenuButton",WS_TABSTOP,257,60,159,14
这是菜单:
IDR_MENU_HYPERLINK_POPUP MENU
BEGIN
POPUP "__HYPERLINK__"
BEGIN
MENUITEM "Add Hyperlink", ID_ADD_HYPERLINK
MENUITEM "Edit Hyperlink", ID_EDIT_HYPERLINK
MENUITEM "Remove Hyperlink", ID_REMOVE_HYPERLINK
END
END
我使用标准方法来初始化这个控件:
void CCongregationDlg::InitCongregationLinkMenuButton()
{
m_menuCongregationLink.LoadMenu(IDR_MENU_HYPERLINK_POPUP);
m_menuBtnCongregationLink.m_hMenu = m_menuCongregationLink.GetSubMenu(0)->GetSafeHmenu();
m_menuBtnCongregationLink.m_nFlatStyle = CMFCButton::BUTTONSTYLE_SEMIFLAT;
// 调整大小(如果需要的话)
CRect rctButton;
m_menuBtnCongregationLink.GetWindowRect(&rctButton);
const auto nOrigWidth = rctButton.Width(); // 存储原始宽度
const auto sizNewButton = m_menuBtnCongregationLink.SizeToContent(true); // 这会调整控件的大小!!!
if (sizNewButton.cx > nOrigWidth) // 与新宽度相比较而不是原始宽度
{
m_menuBtnCongregationLink.SizeToContent();
}
else // 恢复原始宽度
{
m_menuBtnCongregationLink.SetWindowPos(nullptr, -1, -1,
nOrigWidth, sizNewButton.cy, SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
}
}
我想要模仿一个超链接的效果:
- 当鼠标悬停在文本上时,它会显示手型光标。
- 当鼠标悬停在下拉箭头上以显示菜单时,它会变成普通光标。
在CMFCMenuButton
控件中是否有可能实现这个效果?
PS. 理想情况下,它会显示带下划线的链接(类似于按钮超链接控件)。
英文:
I have a standard CMFCMenuButton
control on my dialog:
CONTROL "Congregation Link",IDC_MFCMENUBUTTON_CONGREGATION_LINK,
"MfcMenuButton",WS_TABSTOP,257,60,159,14
This is the menu:
IDR_MENU_HYPERLINK_POPUP MENU
BEGIN
POPUP "__HYPERLINK__"
BEGIN
MENUITEM "Add Hyperlink", ID_ADD_HYPERLINK
MENUITEM "Edit Hyperlink", ID_EDIT_HYPERLINK
MENUITEM "Remove Hyperlink", ID_REMOVE_HYPERLINK
END
END
I use standard approach to initialize the control:
void CCongregationDlg::InitCongregationLinkMenuButton()
{
m_menuCongregationLink.LoadMenu(IDR_MENU_HYPERLINK_POPUP);
m_menuBtnCongregationLink.m_hMenu = m_menuCongregationLink.GetSubMenu(0)->GetSafeHmenu();
m_menuBtnCongregationLink.m_nFlatStyle = CMFCButton::BUTTONSTYLE_SEMIFLAT;
// Resize (if required)
CRect rctButton;
m_menuBtnCongregationLink.GetWindowRect(&rctButton);
const auto nOrigWidth = rctButton.Width(); // Store the original width
const auto sizNewButton = m_menuBtnCongregationLink.SizeToContent(true); // This resizes the control!!!
if (sizNewButton.cx > nOrigWidth) // Compare to the original width rather than the new one
{
m_menuBtnCongregationLink.SizeToContent();
}
else // Restore original width
{
m_menuBtnCongregationLink.SetWindowPos(nullptr, -1, -1,
nOrigWidth, sizNewButton.cy, SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
}
}
I would like to mimic a hyperlink:
- When the mouse is over the text it will show a hand cursor.
- When the mouse is over the drop-down arrow to display the menu it turns into a normal cursor.
Is this possible with the CMFCMenuButton
control?
PS. Ideally it would show the link with an underline (much like the button hyperlink control)
答案1
得分: 1
只需实现一个WM_SETCURSOR处理程序。这应该适用于按钮区域。
英文:
You have only to implement a WM_SETCURSOR handler. This should work for the button area.
答案2
得分: 1
我在查找 CMFCMenuButton
的文档时,发现它是从 CMFCButton
派生的。
而且,这个类有这个方法:CMFCButton::SetMouseCursorHand
。引用文档:
使用此方法将手形光标与按钮关联起来。光标从应用程序资源中加载。
所以我更新了我的 InitCongregationLinkMenuButton
方法,包括:
m_menuBtnCongregationLink.SetMouseCursorHand();
它有效:
现在我有了手形光标,无需派生新类。但即使鼠标位于下拉箭头上方,它仍然保持为手形光标。
英文:
I was looking up the documentation for CMFCMenuButton
and I realised that it is derived from CMFCButton
.
And, that class have this method: CMFCButton::SetMouseCursorHand
. To quote the documentation:
> Use this method to associate the cursor image of a hand with the button. The cursor is loaded from the application resources.
So I updated my InitCongregationLinkMenuButton
method to include:
m_menuBtnCongregationLink.SetMouseCursorHand();
And it works:
I now have the hand cursor without the need to derive a new class. But it stays as a hand cursor even when the mouse is over the drop-arrow.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论