The size of the MFC control varies with the size of the form.

Source: Internet
Author: User

The size of the MFC control varies with the size of the form.

This article will share with you the methods and sample code for using VC ++ to adjust the widget Size Based on the dialog box size. For more information, see.

I. The size and position change.

1. First, add the CRect m_rect for the form class. This member variable is used to record the current size of the form.

2. In the Class Wizard (Ctrl + W), add the response function OnSize () for the message WM_SIZE to the form ();

Note that if (nType = 1) return; this sentence must be added; otherwise, an error occurs when the restoration is minimized.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

Void CPaperManagementDlg: OnSize (UINT nType, int cx, int cy)

{

CDialog: OnSize (nType, cx, cy );

If (nType = 1) return; // if it is minimized, nothing will be done.

// TODO: Add your message handler code here

CWnd * pWnd;

PWnd = GetDlgItem (IDC_STATIC );

ChangeSize (pWnd, cx, cy );

PWnd = GetDlgItem (IDC_FILE_TREE );

ChangeSize (pWnd, cx, cy );

PWnd = GetDlgItem (IDC_EDIT_NAME );

ChangeSize (pWnd, cx, cy );

PWnd = GetDlgItem (IDC_EDIT_REFERENCE );

ChangeSize (pWnd, cx, cy );

PWnd = GetDlgItem (IDC_EDIT_SUMMARY );

ChangeSize (pWnd, cx, cy );

PWnd = GetDlgItem (IDC_EDIT_REMARK );

ChangeSize (pWnd, cx, cy );

PWnd = GetDlgItem (IDC_BUTTON_UPDATE );

ChangeSize (pWnd, cx, cy );

PWnd = GetDlgItem (IDC_BUTTON_SAVE );

ChangeSize (pWnd, cx, cy );

PWnd = GetDlgItem (IDC_STATIC_1 );

ChangeSize (pWnd, cx, cy );

PWnd = GetDlgItem (IDC_STATIC_2 );

ChangeSize (pWnd, cx, cy );

PWnd = GetDlgItem (IDC_STATIC_3 );

ChangeSize (pWnd, cx, cy );

PWnd = GetDlgItem (IDC_STATIC_4 );

ChangeSize (pWnd, cx, cy );

GetClientRect (& m_rect); // set the size of the changed dialog box to the old size.

}

The function ChangeSize is a manually added user function. The procedure is as follows:

3. Add a User Function, void ChangeSize (CWnd * pWnd, int cx, int cy), and write code.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

Void CPaperManagementDlg: ChangeSize (CWnd * pWnd, int cx, int cy)

{

If (pWnd) // determines whether it is null, because this function is called when the dialog box is created, and the control has not yet been created

{

CRect rect; // obtain the size before the control changes

PWnd-> GetWindowRect (& rect );

ScreenToClient (& rect); // converts the widget size to the Region coordinates in the dialog box.

 

// Cx/m_rect.Width () is the ratio of horizontal changes in the dialog box

Rect. left = rect. left * cx/m_rect.Width (); // adjust the widget size

Rect. right = rect. right * cx/m_rect.Width ();

Rect. top = rect. top * cy/m_rect.Height ();

Rect. bottom = rect. bottom * cy/m_rect.Height ();

PWnd-> MoveWindow (rect); // you can specify the widget size.

}

}

The function that actually changes the widget size is the ChangeSize function, which continuously calls the ChangeSize function in OnSize to change the size of all widgets in the form.

------------------------------------------------------

The following content comes from the network:

Ii. VC ++: Adjust the widget location only according to the dialog box size

1. Add the member variable CRect m_rect to the dialog box class to save the size before the dialog box size change;

2. In the OnInitDialog () function of the dialog box, obtain the size when the dialog box is created:

GetClientRect (& m_rect );

3. Add the following code to the response function OnSize () of WM_SIZE:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

CWnd * pWnd;

PWnd = GetDlgItem (IDC_BUTTON1); // get the control handle

If (nType = 1) return; // if the form is minimized, nothing will be done.

If (pWnd) // determines whether it is null, because this function is called when the dialog box is created, and the control has not yet been created

{

CRect rect; // obtain the size before the control changes

LONG cWidth, cHeight; // The distance from the right of the control to the right of the form, and the distance from the bottom of the control to the bottom of the form

PWnd-> GetWindowRect (& rect );

ScreenToClient (& rect); // converts the widget size to the Region coordinates in the dialog box.

CWidth = m_rect.Width ()-rect. right;

CHeight = m_rect.Height ()-rect. bottom;

Rect. left = cx-rect.Width ()-cWidth;

Rect. right = cx-cWidth;

Rect. top = cy-rect.Height ()-cHeight;

Rect. bottom = cy-cHeight;

PWnd-> MoveWindow (rect); // you can specify the widget size.

}

GetClientRect (& m_rect); // set the size of the changed dialog box to the old size.

The above is all the content of this article. I hope you will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.