Resizing Controls in MFC

前端 未结 8 1921
萌比男神i
萌比男神i 2021-01-11 10:26

I am writing a program which has two panes (via CSplitter), however I am having problems figuring out out to resize the controls in each frame. For simplicity,

相关标签:
8条回答
  • 2021-01-11 10:31

    GetDlgItem(IDC_your_slidebar)->SetWindowPos(...) // actually you can move ,resize...etc

    0 讨论(0)
  • 2021-01-11 10:34

    A window receives WM_SIZE message (which is processed by OnSize handler in MFC) immediately after it was resized, so CEdit::OnSize is not what you are looking for.

    You should add OnSize handler in your frame class and inside this handler as Rob pointed out you'll get width and height of the client area of your frame, then you should add the code which adjusts size and position of your control.

    Something like this

    void MyFrame::OnSize(UINT nType, int w, int h)
    {
        // w and h parameters are new width and height of your frame
        // suppose you have member variable CEdit myEdit which you need to resize/move
        myEdit.MoveWindow(w/5, h/5, w/2, h/2);
    }
    
    0 讨论(0)
  • 2021-01-11 10:35

    It is better to use the Dynamic Layout capabilities of each control at the Property section.

    Let's say you want to have a specific control, like a heading, always at the center of the view/dialog, then you just choose the properties of Dynamic Layout of the control, Moving Type as Horizontal and Moving X as 50 but you keep sizing to None. This way, when you resize the view, the header remains always at the center. You have to keep in mind that the minimum of the resizing/moving is the size/position of the control within the dialog/view, when you designed it at the Resource View.

    This way, you save the burden of geometry and the transformations.

    0 讨论(0)
  • 2021-01-11 10:37

    SetWindowPos is a little heavy duty for this purpose. MoveWindow has just what is needed.

    0 讨论(0)
  • 2021-01-11 10:44

    I use CResize class from CodeGuru to resize all controls automatically. You tell how you want each control to be resized and it does the job for you.

    The resize paradigm is to specify how much each side of a control will move when the dialog is resized.

    SetResize(IDC_EDIT1, 0,   0,   0.5, 1);
    SetResize(IDC_EDIT2, 0.5, 0,   1,   1);
    

    Very handy when you have a large number of dialog controls.

    Source code

    0 讨论(0)
  • 2021-01-11 10:46

    When your frame receives an OnSize message it will give you the new width and height - you can simply call the CEdit SetWindowPos method passing it these values.

    Assume CMyPane is your splitter pane and it contains a CEdit you created in OnCreate called m_wndEdit:

    void CMyPane::OnSize(UINT nType, int cx, int cy)
    {
        m_wndEdit.SetWindowPos(NULL, 0, 0, cx, cy, SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
    }
    
    0 讨论(0)
提交回复
热议问题