Mfc 当面板折叠时,获取按钮的矩形不再有效

Mfc 当面板折叠时,获取按钮的矩形不再有效,mfc,ribbon,Mfc,Ribbon,我试过几种方法。从CBCGPRibbonButton(与CMFCRibbonButton相同)派生对象,并在类中使用GetRect(),并使用click事件在功能区中查找按钮并获取rect 发生的情况是,rect相对于它所在的窗口。但是,如果面板已折叠,则它所在的窗口不是功能区栏,因此它会得到错误的位置 我需要一种方法来获取相对于功能区栏的位置。有什么想法吗?在屏幕坐标中计算。 从功能区中获取按钮矩形。使用ClientToScreen,您现在就有了屏幕坐标。使用ScreenToClient的按钮

我试过几种方法。从CBCGPRibbonButton(与CMFCRibbonButton相同)派生对象,并在类中使用GetRect(),并使用click事件在功能区中查找按钮并获取rect

发生的情况是,rect相对于它所在的窗口。但是,如果面板已折叠,则它所在的窗口不是功能区栏,因此它会得到错误的位置


我需要一种方法来获取相对于功能区栏的位置。有什么想法吗?

在屏幕坐标中计算。 从功能区中获取按钮矩形。使用ClientToScreen,您现在就有了屏幕坐标。使用ScreenToClient的按钮父句柄,您就有了相对于功能区栏的跳线


PS:甚至我都不知道为什么在功能区折叠时显示按钮。

好的,所以我试图找出按钮的矩形是什么:

当面板倒塌时:

这是我的解决方案:

class CMyButton : public CBCGPRibbonButton
{
    DECLARE_DYNCREATE(CHeaderFooter)

public:
    CMyButton()
    {
    };

    CMyButton(
        UINT    nID,
        LPCTSTR lpszText,
        int     nSmallImageIndex = -1,
        int     nLargeImageIndex = -1,
        BOOL    bAlwaysShowDescription = FALSE)
        : CBCGPRibbonButton(nID, lpszText, nSmallImageIndex, nLargeImageIndex, bAlwaysShowDescription)
    {
    }

    BOOL HasMenu() const override
    {
        return true;
    }

    CWnd* GetButtonWnd() const
    {
        CBCGPBaseRibbonElement* pDroppedDown = m_pParent->GetDroppedDown();
        CWnd* pWnd;
        if (pDroppedDown)  // Was popup opened from a collapsed panel from the ribbon?
        {
            pWnd = pDroppedDown->GetPopupMenu()->GetMenuBar();
        }
        else
        {
            pWnd = m_pParent->GetParentRibbonBar();
        }
        return pWnd;
    }

    void OnShowPopupMenu() override
    {
        CRect rect = GetRect();
        // pt is the bottom left corner of button relative to the window that
        // it is contained in.
        CPoint pt(rect.left, rect.bottom);
        GetButtonWnd()->ClientToScreen(&pt); // convert pt to screen coordinates

        ... // do other stuff with that point
    }
};

IMPLEMENT_DYNCREATE(CHeaderFooter, CBCGPRibbonButton)

它确定按钮所属的CWnd,以便将rect正确转换为屏幕坐标。

我已经知道如何在屏幕坐标中计算。我需要靠窗户才能这样做。不是功能区窗口,而是下拉列表。反正我现在已经弄明白了。谢谢。顺便说一句,我不明白你的PS是什么意思。