How do I specify the size of a CDialogBar in an MFC / MDI project?

I wonder how to specify the default size for the CDialogBar when it is created in the MainFrame of an MFC / MDI project. Here is the code to create the dialog.

    // add Dialog bar window
if (m_wndDlgBar.Create(this, IDD_ADDLGBAR,
    CBRS_RIGHT|CBRS_SIZE_DYNAMIC|CBRS_FLYBY, IDD_ADDLGBAR))
{
    TRACE0("Failed to create DlgBar\n");
    return -1;      // fail to create
}

m_wndDlgBar.EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndDlgBar);

      

I tried calling MoveWindow () or SetWindowPos () , but they don't work. The goal I want to achieve is when the dialog is created, it has a fixed size (like 200x300) no matter what DPI setting. As you know, the size of the dialog box drawn in the resource will change with the DPI settings. So I want the dialog to have a fixed size.

Thanks in advance!

-bc

+2


source to share


2 answers


You can use the overridden CalcFixedLayout method if you subclass CDialogBar with a custom one. For example:



class CSizingDialogBar : public CDialogBar {
    CSize m_size;
    bool m_forceSize;
public:
    CSizingDialogBar(CWnd* pParentWnd, UINT nID, CSize initialSize) 
    : CDialogBar(
        pParentWnd, nID,
        CBRS_RIGHT|CBRS_SIZE_DYNAMIC|CBRS_FLYBY, nID)
    , m_size(initialSize)
    , m_forceSize(true) {
    }
    ~CSizingDialogBar() {}

    virtual CSize CalcFixedLayout(BOOL bStretch, BOOL bHorz) {
        if (m_forceSize) {
            return m_size;
        }
        else {
            return CDialogBar::CalcFixedLayout( bStretch, bHorz );
        }
    }
};

      

+2


source


CalcFixedLayout works just fine, but if you can't override the method:

Resize the m_sizeDefault CDialogBar member to the size you need before calling MoveWindow () or SetWindowPos (), after which it should resize correctly. In fact, you will also need to add borders to the size (they should also fit into the window), so I used something like this:



int nEdgeThickness = GetSystemMetrics(SM_CXSIZEFRAME);
pContrBar->m_sizeDefault = CSize(rc->right+nEdgeThickness*2, rc->bottom+nEdgeThickness);

      

I needed this to dynamically resize the CDialogBar, so I knew when it would resize.

0


source







All Articles