Visual C ++ programming skills

zhaozj2021-02-11  254

Visual C Program Design Skills Shandong University of Science and Technology Intelligent Engineering Research Institute Yang in Chun, He Mingxiang

Microsoft Visual C is a visual programming language, which is favored by the majority of programming staff due to powerful function. However, since the application framework structure of VC is very complicated, many beginners are expected. This article reveals some essential characteristics and related programming skills when the VC program code is executed by setting the view background color and changing dialog box, revealing some of the essential features and related programming skills, understanding the structure of the MFC library and Windows operating system. The internal work mode provides a certain help.

Set view background color

For VC documents, views in the view structure, from the user's perspective, you can change the size, location of the normal window, which is the same as other Windows-based windows; from the perspective of programmers, the view is not Ordinary windows, but from the CVIEW class object from the MFC library. Like any VC object, the behavior of the view object is determined by the member function (data member) of the class, including functions defined in the derived class and from the base class inherited.

submit questions

The background of the view is generally white, and in the default, it is consistent with the color color_window defined by the system. Designers generally want their own programs to make users easily change the window background color, or with a beautiful picture to fill background. We can use the Windows function setsyscolors to re-specify the actual color corresponding to the Color_Window to change the purpose of changing the background color. But this will change the view window background of other applications at the same time, so that the color settings of the entire Windows system are confusing. In addition, we may use the following methods to set the background color of the view, that is, add the following program code in the CView's onDraw function:

Void CtestView :: OnDraw (CDC * PDC)

{

Ctestdoc * pdoc = getDocument ();

Ask_VALID (PDOC);

CRECT RECLIENT;

Cbrush brushbkcolor;

GetClientRect (RectClient);

Brushbkcolor.createsolidbrush (RGB (255, 0, 0));

PDC-> DPTOLP (RectClient);

PDC-> FillRect (RectClient, & Brushbkcolor);

...

}

This can achieve the purpose of changing the view background of the current application, but also has some adverse effects, making the program operational effect.

analyse problem

We know that in VC documents, view structures, CVIEW's OnDRAW function is used to achieve most of the graphics drawing. If the user changes the window size, or the hidden area is displayed, the onDraw function will call the window. Also, when the data in the program document changes, it is generally necessary to inform the change of Windows by calling the INVALIDATE (or InValidateRect) member function of the view, and the call to the InvalIDate will also trigger the call to the onDraw function. Because the onDraw function is frequently called, it will make the screen unstable and flicker each time it is implemented.

Through careful research of the VC application framework structure and the Windows message mapping system, the author finds another way to change the background of the view, which is better than the above two methods. In fact, a Windows message is triggered before the program calls the onDraw function: WM_ERASEBKGND to erase the view refreshed the area. By default, the Windows system uses the brush described by the member hbrbackground in the window class when registering the window class, which generally refreshes the screen into the color corresponding to the Color_Window. Therefore, the execution process of setting the background color in the onDraw function is this: first refresh the screen into the color corresponding to the color_window, and then fill other colors in the onDRAW function, which is the root cause of the screen flashing. Solve the problem

Through the above analysis, we should move the view background color to the Windows message: WM_ERASEBKGND The message mapping function corresponding to the WM_ERASEBKGnd, not in the onDRAW function. We can implement this process in the following steps: adding a member variable m_viewbkcolor in the document class to save the current background color while adding two member functions GetViewBkcolor and SetViewBkcolor to read and write. The advantage of this is to serialize M_ViewBkcolor members, link them with documents, when a document is opened, and its background will be consistent with the background of the previous program to operate this document. In the view class, the Windows message WM_ERASEBKGND increase the message mapping function onERaseBkGnd, the code is as follows:

Bool ctestview :: OneRaseBkGnd (CDC * PDC)

{

CRECT RECT;

CBRUSH brush;

Brush.createsolidbrush (getDocument () -> getViewBkcolor ());

PDC-> getClipbox (Rect);

PDC-> FillRect (Rect, & Brush);

Return True;

}

In this function, it is not necessary to convert the client area rectangle to the conversion of the logical coordinate, and Windows automatically calculates the cropping area when calling the function, making it minimized for the screen area that needs to be refreshed. This allows us to easily change the color of the view background by designing the following menu functions, and running is quite satisfactory.

Void ctestview :: OnChangeViewBkcolor ()

{

CcolorDialog CDLG;

IF (cdlg.domodal () == iDok)

{

GetDocument () -> setViewBkcolor

(cdlg.getcolor ());

INVALIDATERECT (NULL);

}

}

Change dialog title

submit questions

This is often encountered in the VC programming process: Multiple places in executing programs need to call the same dialog, but in different cases, they want to add different titles to the dialog box. Starting we may use the following programs to achieve this:

CTestDialog DLG;

Dlg.SetWindowText ("Title-1");

Dlg.domodal ();

With the above approach, we hope that in different places in the program, through setting the function setWindowText, to achieve the purpose of making the same dialog with different titles, but this is not good.

analyse problem

With this method, when the program is executed, the title of the dialog does not change any changes after an overlooked error. This is because most forms are dynamically created in the VC programming. For example, the above dialog box, although the VC objects of the dialog are constructed, the form object has not been created, but the form object is not yet created, clearly setting the title of a dialog box without a form of form. . In addition, when the call of DLG.Domodal ends, the dialog form object will be immediately released, so the setup dialog title is not possible after the function. Solve the problem

Through the analysis of the call sequence of the function in the VC frame structure, we found that the program will automatically call a series of initialization functions of the dialog box when DLG.Domodal execution, including the call of the dialog member function OnInitDialog, start from here Will find a way to change the title of the dialog box. To this end, first introduce a public member variable m_strcaption of a type CString to the dialog, and change the above block to:

CTestDialog DLG;

DLG.M_STRCAPTION = "Title -1";

Dlg.domodal ();

Then the virtual member function on the dialog box is onInitdialog as follows:

Bool ctestdialog :: oninitdialog ()

{

CDIALOG :: OnInitdialog ();

SetWindowText (M_STRCAPTION);

...

Return True;

}

With this approach, each time you open the dialog box, just set the dialog public member variable m_strcaption to a different value, you can make the dialog boxes have different titles.

Small knot

The two techniques introduced in this article have a similarity, which is basically the same as the process of solving the problem, just being placed different from the program process. This is a very important aspect of learning and proficient in VC , which is the key to influence the efficiency and performance of its procedures.

转载请注明原文地址:https://www.9cbs.com/read-3882.html

New Post(0)