在CMFCMenuButton控件上有条件地设置鼠标指针。

huangapple go评论66阅读模式
英文:

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);
    }
}

我想要模仿一个超链接的效果:

  1. 当鼠标悬停在文本上时,它会显示手型光标。
  2. 当鼠标悬停在下拉箭头上以显示菜单时,它会变成普通光标。

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);
	}
}

在CMFCMenuButton控件上有条件地设置鼠标指针。

I would like to mimic a hyperlink:

  1. When the mouse is over the text it will show a hand cursor.
  2. 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();

它有效:

在CMFCMenuButton控件上有条件地设置鼠标指针。

现在我有了手形光标,无需派生新类。但即使鼠标位于下拉箭头上方,它仍然保持为手形光标。

英文:

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:

在CMFCMenuButton控件上有条件地设置鼠标指针。

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.

huangapple
  • 本文由 发表于 2023年8月4日 01:57:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定