Visual C programming skills
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 the background color for the VC document, view the view, from the user's perspective, only can change the size, position of the normal window, the same, other Windows-based windows, is the same; from the programmer's perspective The view is not a normal window, but a class object derived from the CView class in 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.
The background of the problem view is generally white, which is consistent with the color color_window defined by the system default. 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 onDraw function of the cView: void ctestview :: OctRaw (CDC * PDC) {ctestdoc * PDOC = getDocument (); assert_valid pDoc); CRect rectClient; CBrush brushBkColor; GetClientRect (rectClient); brushBkColor.CreateSolidBrush (RGB (255,0,0)); pDC-> DPtoLP (rectClient); pDC-> FillRect (rectClient, & brushBkColor); ...} this The purpose of changing the view background of the current application, but also has some adverse effects, making the program operational effect.
Analysis issues We know that in the documentation of VC , in the view structure, the 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. Solving 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_RASEBKGnd, 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. WM_ERASEBKGND Windows message is a view in the view class add message mapping function OnEraseBkgnd, the following code: BOOL CTestView :: OnEraseBkgnd (CDC * pDC) {CRect rect; CBrush brush; brush.CreateSolidBrush (GetDocument () -> GetViewBkColor ()); PDC-> getClipbox (Rect); PDC-> FillRect (Rect, & Brush); Return True;} Do not need to perform device coordinates to logical coordinates in this function, and Windows automatically The calculation of the cropping area makes it possible to achieve the smallest screen area. 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 ()); invalidateect (null);}}
Change dialog title
Summary The problem is often encountered during the VC programming process: Multiple parts of the executing program need to call the same dialog, but in different cases, it is desired to add different titles to the dialog box. Start we may use the following programs to achieve this: CTestDialog Dlg; Dlg.SetWindowText ("Title -1"); DLG.Domodal (); Using the above method, we hope that we hope to be different from the program, through setting Function setWindowText Different parameters to achieve the purpose of making the same dialog with different titles, but doing this is not good. Analysis of the problem uses this method, when executing this program, after a negligible error, the title of the dialog does not change any changes. 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.
Solving the problem by analyzing the call sequence of the function in the VC framework structure, we found that the program will automatically call a series of initialization functions of the dialog box at the beginning of DLG.Domodal, including the call of the dialog member function OnInitDialog, from Here, you will find a way to change the title of changing dialog. To this end, first introduce a public member variable m_strcaption type CString to: CTestDialog Dlg; dlg.m_strcaption = "Title -1"; DLG.Domodal (); DLG.Domodal (); then overload dialog The virtual member function onInitdialog is as follows: BOOL CTESTDIALOG :: OnNitDialog () {cdialog :: OnItDialog (); setWindowText (m_strcaption); ... return true;} In this way, each time you open the dialog box, as long as the dialog is public The member variable m_strcaption is set to a different value, allowing the dialog to have different titles.
The two techniques introduced in small junctions have a similar, 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.