Conditionally set the mouse cursor on a CMFCMenuButon control

53 Views Asked by At

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

enter image description here

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)

2

There are 2 best solutions below

0
On

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:

enter image description here

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.

2
On

You have only to implement a WM_SETCURSOR handler. This should work for the button area.