Detecting when a CControlBar's docking state has changed

假如想象 提交于 2019-12-31 04:07:09

问题


I'm using a CControlBar-derived class and I would like to detect when the docking state of the CControlBar has changed (i.e., docking from vertical to horizontal or when it goes into floating mode).

Of course, I could handle WM_SIZE but it seems to be a waste of ressources doing the task every time a WM_SIZE message is fired instead of only when the docking state has changed.

Can anyone please point me in the right direction?


回答1:


You can override the CControlBar::OnBarStyleChange virtual function to detect changes in the control bar style (CBRS_XXX values - look in the <afxres.h> header file for details).

To determine whether the control bar is floating/docked, check the CBRS_FLOATING style. To check for horizontal/vertical orientation, use the CBRS_ORIENT_HORZ and CBRS_ORIENT_VERT styles.

So, using CToolBar (which is derived from CControlBar) as an example:

class CMyToolBar : public CToolBar {
public:
    virtual void OnBarStyleChange(DWORD dwOldStyle, DWORD dwNewStyle);
};

void CMyToolBar::OnBarStyleChange(DWORD dwOldStyle, DWORD dwNewStyle)
{
    // Call base class implementation.
    CToolBar::OnBarStyleChange(dwOldStyle, dwNewStyle);

    // Use exclusive-or to detect changes in style bits.
    DWORD changed = dwOldStyle ^ dwNewStyle;

    if (changed & CBRS_FLOATING) {
        if (dwNewStyle & CBRS_FLOATING) {
            // ToolBar now floating
        }
        else {
            // ToolBar now docked
        }
    }

    if (changed & CBRS_ORIENT_ANY) {
        if (dwNewStyle & CBRS_ORIENT_HORZ) {
            // ToolBar now horizontal
        }
        else if (dwNewStyle & CBRS_ORIENT_VERT) {
            // ToolBar now vertical            
        }
    }
}

I hope this helps!



来源:https://stackoverflow.com/questions/523942/detecting-when-a-ccontrolbars-docking-state-has-changed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!