Creating Dynamic controls on dialog boxes, displaying pictures on the picture control

Source: Internet
Author: User

1 creation of dynamic controls on top of the MFC dialog box

 The controls on the dialog box are a specific object of the MFC class.

When you use static controls on top of a dialog box, you can add messages, response functions, and variables to each control according to the Class wizard.

When you need to create a control dynamically in a dialog box, you need to manually add messages , response functions , and variables to the dynamic control instead of the Class Wizard .


When you create a control dynamically, you can follow these steps:

(1) Define the class to which the control belongs

Each control corresponds to a class (for example, a button that belongs to a class of CButton).

For example, in the program we need to create a specific button object with the CButton class, and use the function to indicate its position and size on the dialog box.

Because of the many benefits of pointers, you can use a CButton * type variable to manage the buttons you create. But be cautious about using pointer variables to prevent memory leaks, wild pointers.

(2) Determine the ID number of the control

  When you use a control class to create a specific control object, you need a unique ID to be identified. The ID number is actually a macro, for the order of the control ID, you can define the ID value for each control under the string table under Explorer VS2012. These ID numbers are automatically saved in the Resource.h file to form a macro.

(3) The general process of dynamically creating controls

After you have defined the control class and its ID number, you can write code that dynamically creates the control within the appropriate function. The flow of controls such as dynamically generated buttons (button), text boxes (staic text), edit control, Union boxes (Combo box) can be described as follows (for example, creating a text box):

To create a text box function:

    // Create a static text  box dynamically    CStatic *cmfcdialogdlg::createonestatic (lpctstr str, CRect Coord, UINT IDC)      {              *pstatic = null;< c11/>new             assert_valid (pstatic);              Pstatic->create (str, this  , IDC);                     return pstatic;      }  

Ps:

First, the explicit text box class is cstatic; Then, assign a text box object with the dynamic allocation function of C + +; This object's create function is then used to display the object in the dialog box according to the specified style (title str, text box display appearance, coordinates, ID number) . Finally, the created control address is returned to the calling function to manage.

The core of a dynamically created control is the CREATE function in the object of each control class, and the CREATE function for different control classes may require different parameters. When you need to release the dynamic button you created, use the DELETE statement to release the address of the dynamic button (which is also part of the New-delete dynamic Assignment), and the controls created on top of the frame will disappear .

(4) Dynamic control size varies with dialog box scale

You can use the CREATE function within a control to dynamically create a control on a dialog box, so you do not use a constant value for the coordinates. Because the dialog window can follow the user's needs to change.

MFC specifies that the dialog box size changes with the message on_wm_size (), as long as the on_wm_size () message, the response function onsize () should be obtained to the size of the dialog box after resizing , you can follow the dynamic Control and dialog box scale to determine the size of the current control. In this way, dynamic controls can also be scaled to follow the dialog box.


2 Picture control controls display pictures

In MFC, the CImage class contains a member function that loads a picture and draws a picture to an area. The picture control control provides a platform for the CImage object. This means that images loaded into memory by the CImage object can be drawn on top of the picture control control. Therefore, the CImage class and the picture control control class can be used together.

(1) Preparing the picture control control

Switch directly to the dialog file and drag a Picturecontrol control into the dialog box. Remember its ID number.

(2) The Picture control control displays the image's code

Only from the perspective of the display picture. The picture control control only provides a platform for image display, loading pictures and drawing pictures is done by the member functions of the CImage object, and some other commonly used functions are used.

    int CX, CY;      CImage  image;      CRect   rect;                Loads the picture image according to the path      . Load (_t ("e:\\mylibrayphotoshop\\log.jpg"));      Gets the wide height of the picture,      cx  = image. GetWidth ();      Cy  = image. GetHeight ();                Gets the size of the picture control control      GetDlgItem (idc_picture)->getwindowrect (&rect);      Select the client area to      screentoclient (&rect) within the rectangular area represented by the control;      The window moves to the area represented by the control      GetDlgItem (idc_picture)->movewindow (Rect.left, Rect.top, CX, CY, TRUE);      CWnd *pwnd = NULL;      PWnd    = GetDlgItem (idc_picture);//Get control handle      pwnd->getclientrect (&rect);//Get handle point to size of control area                CDC * PDc = NULL;      PDc = PWND->GETDC ();//Gets the DC image of the picture                . Draw (PDC->M_HDC, rect);//Draws the picture to the area                ReleaseDC (pDc) in the image representation;  

Ps:

[1] the Load method within the CImage loads the picture under the specified path, and then obtains the width and height of the image using its getwidth and GetHeight methods.

[2] GetDlgItem gets its handle through the ID of the picturecontrol control, and then calls the Getwindrect in the picture control method to get the range it occupies in the dialog box itself. The screentoclient method is used to transfer the client area to the Picturecontrol, with the aim of using the MoveWindow method within the Picturecontrol to reset The position that the control occupies on the dialog box is the size of the picture that it adapts to.

[3] re-set to get the area occupied by Picturecontrol in the dialog box. Use the pDc to manage the DCs in the Picturecontrol. The CImage draw method can then draw the picture in the Rect area using the obtained parameters.

Reference articles

1. most wussy , dialog box Dynamic controls are created to display pictures on the picture control control

Creating Dynamic controls on dialog boxes, displaying pictures on the picture control

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.