Programming Skills for Visual C + +

Source: Internet
Author: User

Microsoft Visual C + + is a visual programming language that is popular with programmers because of its powerful features. However, because of the complexity of VC + + application Framework structure, many beginners are discouraged. Through the analysis and research of several realization methods of setting the view background color and changing the title of the dialog box, this paper reveals some essential features of vc+ + program code execution and relevant programming skills, and provides some help to understand the structure of MFC library and the internal working mode of Windows operating system.

Set the view background color

For vc+ + documents, the view in the structure, from the user's point of view, can only change the size, position of the normal window, and other windows-based application windows are the same; from a programmer's perspective, the view is not a normal window, but rather 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 the application-defined functions in the derived class and the functions inherited from the base class.

Ask a question

The background of the view is generally white, and by default it is the same as the system-defined color Color_window. Designers generally want their programs to make it easy for users to change the window background color, or to fill the background with beautiful pictures. We can use the Windows function setsyscolors to reassign the actual color of the Color_window to change the background color of the view. However, this will change the view window background of other applications at the same time, causing confusion in the color settings of the entire Windows system. In addition, we might set the background color of the view by adding the following program code in the CView OnDraw function:

void CTestView::OnDraw(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 can achieve the purpose of changing the view background of the current application, but it also has some bad effects, which makes the program run less effective.

Analyze problems

We know that in VC + + documents, visual structure, CView OnDraw function is used to achieve most of the work of drawing. If the user changes the window size or displays the hidden area, the OnDraw function will be called to repaint the window. Also, when data in a program document changes, it is generally necessary to notify windows of changes by invoking the invalidate (or InvalidateRect) member function of the view, and a call to the invalidate will trigger a call to the OnDraw function. Because the OnDraw function is called frequently, it can cause the screen to become unstable and flicker when it is executed, each time a refresh fills a view client area.

Through a careful study of VC + + application framework structure and Windows message mapping system, the author finds another way to change the view background, which is better than both of the above methods. In fact, before the program calls the OnDraw function, a Windows message is triggered: WM_ERASEBKGND to erase the view refresh area. By default, the Windows system erases the screen by using the brush described by the member Hbrbackground in the window class when the view window is registered, which typically refreshes the screen to the color that corresponds to the Color_window. Therefore, setting the background color in the OnDraw function is done by refreshing the screen to the corresponding color of the Color_window, and then populating the OnDraw function with other colors, which is the root cause of the screen flicker.

Solve the problem

With the above analysis, we should move the view background color to the Windows message: WM_ERASEBKGND's corresponding message mapping function, not in the OnDraw function. We can implement this process by adding a member variable to the document class M_viewbkcolor save the current background color while adding two member functions Getviewbkcolor and Setviewbkcolor to read and write to it. The advantage of this is that you can serialize M_viewbkcolor members, associate them with the document, and when you open a document, the background will match the background of the previous program when the document was manipulated. Add the message mapping function OnEraseBkgnd to the view's Windows message WM_ERASEBKGND in the view class, 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;
}

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.