Cpaintdc and CDC

Source: Internet
Author: User

Today, when drawing a graph in the dialog box, I encountered a problem:

The onpaint function that is reloaded in the dialog box is as follows:

Crect rect; CDC * PDC = getdc (); CDC memdc; getclientrect (& rect); cbitmap membitmap; memdc. createcompatibledc (null); membitmap. createcompatiblebitmap (PDC, rect. width (), rect. height (); memdc. setbkmode (transparent); memdc. selectObject (& membitmap); colorref bkcolor =: getsyscolor (color_3dface); // obtain the system color memdc. fillsolidrect (rect. left, rect. top, rect. width (), rect. height (), bkcolor); // draw the background memdc. fillsolidrect (rect. left, rect. bottom-40, rect. width (), rect. height (), RGB (80, 80, 80); PDC-> bitblt (rect. left, rect. top, rect. width (), rect. height (), & memdc, 0, 0, srccopy); cdialog: onpaint ();

The purpose of this Code is to draw a rectangle box, and then call cdialog: onpaint () to display the controls in the original dialog box.

However, at the beginning, the problem was confusing: click "show desktop" to minimize all windows. When the dialog box is displayed, the rectangle will disappear, but the dialog box will be blocked by the screen, only a part of the dialog box is displayed. When the dialog box is dragged back to the center of the screen for full display, all controls except the rectangular box on the dialog box will disappear.

Then let's look at the normal code after the change:

Cpaintdcdc (this); crect rect; CDC * PDC = & DC; CDC memdc; getclientrect (& rect); cbitmap membitmap; memdc. createcompatibledc (null); membitmap. createcompatiblebitmap (PDC, rect. width (), rect. height (); memdc. setbkmode (transparent); memdc. selectObject (& membitmap); colorref bkcolor =: getsyscolor (color_3dface); // obtain the system color memdc. fillsolidrect (rect. left, rect. top, rect. width (), rect. height (), bkcolor); // draw the background memdc. fillsolidrect (rect. left, rect. bottom-40, rect. width (), rect. height (), RGB (80, 80, 80); PDC-> bitblt (rect. left, rect. top, rect. width (), rect. height (), & memdc, 0, 0, srccopy); releasedc (PDC );

The change is to change the method for obtaining DC: CDC * PDC = getdc () to cpaintdc DC (this), and then cancel calling the cdialog: onpaint () function. Why?


The unique feature of cpaintdc is that it calls cwnd: beginpaint In the constructor to call cwnd: endpaint In the destructor, and it can only respond to wm_onpaint messages. This beginpaint will send the wm_onerasebkgnd message. Therefore, it only erases the specified background and redraws it without affecting other controls in the dialog box. It is worth noting that after this operation, do not call the cdialog: onpaint function.


The previous method failed because:

Beginpaint () and endpaint () can delete the wm_paint message in the message queue and make the invalid region valid.
Getdc () and releasedc () are neither deleted nor valid. Therefore, when the program jumps out of wm_paint, the invalid region still exists. The system continuously sends the wm_paint message, so the program continuously processes the wm_paint message. Invalid region refers to the area to be re-painted. Invalid indicates that the current content is old and outdated.

Only when a window message is idle will the system take the time to check whether the invalid area of the window is non-empty (wm_paint has the lowest priority. This is why windows and desktops tend to become white, fail to refresh, and leave drag marks when the system is busy). If it is not empty, the system sends wm_paint. Therefore, you must use beginpaint and endpaint to set the invalid region to null. Otherwise, wm_paint will be sent all the time.

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.