MFC controls self-painted (vii)

Source: Internet
Author: User
Tags constructor message queue
The difference between CDC,CCLIENTDC,CPAINTDC,CWINDOWDC in MFC CDC is the base class for Windows drawing devices. CCLIENTDC: (1) (Customer area device context) is used for the output of the client area, associated with a particular window, which allows the developer to access the client area in the target window, whose constructor contains the GetDC, and the destructor contains ReleaseDC.
CPaintDC: (1) is used to respond to the window redraw message (WM_PAINT) is the drawing output. (2) CPaintDC calls BeginPaint () in the constructor to get the device context, and in the destructor, call EndPaint () to release the device context. EndPaint () is responsible for purging the WM_PAINT message from the message queue in addition to freeing the device context. Therefore, when processing a window redraw, you must use CPAINTDC, otherwise the WM_PAINT message cannot be purged from the message queue, causing a continuous window redraw. (3) CPaintDC can only be used in WM_PAINT message processing.
CWINDOWDC: (1) drawings can be drawn in the non-client area, while CCLIENTDC,CPAINTDC can only draw graphics in the customer area.  (2) The coordinates origin is in the upper left corner of the screen, and the CCLIENTDC,CPAINTDC coordinates origin is in the upper left corner of the client area. (3) Associate a specific window that allows the developer to draw on any part of the target window, including boundaries and headings, which are sent along with the Wm_ncpaint message. ===========

When the system sends a WM_PAINT message. The system sends WM_PAINT messages at various times: when a window is first created, when the window is resized, when the window is moved from behind another window, when the window is maximized or minimized, and so on, the actions are managed by the system, and the application simply receives the message passively. Draw operations in the message handler, and most of the time the application needs to be able to actively trigger the drawing operations in the window, such as when the data of the window display changes, this is usually done by the invalidaterect and INVALIDATERGN functions.  InvalidateRect and INVALIDATERGN Add the specified area to the window's update region, and the system automatically generates a WM_PAINT message if the window's update zone is not empty when there is no other message for the application's message queue. Why is the system not sending WM_PAINT messages when calling invalidate? Why not send a WM_PAINT message when the app message queue is empty. This is because the system treats the drawing operations in the window as a low-priority operation, so as to be pushed backwards as much as possible. However, this also helps to improve the efficiency of drawing: Two WM_PAINT messages between the InvalidateRect and Invaliatergn to invalidate the area will be added up, and then in a WM_PAINT message is updated once, not only to avoid repeated updates
  The same area, also optimizes the app's update operations. such as this through InvalidateRect and invalidatergn to invalidate the window area, depending on the system at the right time to send the WM_PAINT message mechanism is actually an asynchronous way of working, that is, in the invalid window area and send Wm_ There is a delay between the paint messages, and sometimes this delay is not what we want, and we can certainly use SendMessage to send a WM_PAINT message after the invalid window area to force immediate redrawing, but instead of using Windows GDI provides us with more convenient and powerful functions: UpdateWindow and RedrawWindow. UpdateWindow checks the window's update region to send the WM_PAINT message when it is not empty; RedrawWindow gives us more control: whether to redraw non-client areas and backgrounds, whether to always send WM_PAINT messages regardless of the update Whether the region is empty or not. Description: It is recommended to use CCLIENTDC,CPAINTDC and CWINDOWDC objects when drawing, rather than using CDC objects directly.

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.