CDC dual-buffer rendering and InvalidateRect

Source: Internet
Author: User

When drawing a graph using the Double Buffer technology of MFC, we often ignore a detail ----> support for the InvalidateRect function.


We know that to improve the rendering efficiency, we often only need to re-paint the area to be drawn, that is, call InvalidateRect.


Generally, the OnPaint processing code is:

CWnd: OnPaint ()

{

CPaintDC dc (this );

CMemoryDC memDC (& dc );


// Draw with memDC

DrawByMemoryDC (& memDC );

// Copy to CPaintDC

Dc. Bilt (memDC );

}


Here, CPaintDC will process the Rect parameters passed in by the InvalidateRect function. pixel painting and copying operations are not performed in the Rect region, but our CMemoryDC does not, therefore, dual buffering is very inefficient.


To solve this problem, you can use the following method to crop areas that do not need to be drawn:

CRect invalidateRect = dc. m_ps.rcPaint; // dc is CPaintDC


If (invalidateRect. left> m_rtClient.left) // m_rtClient indicates the size of the customer zone.

{

CRect leftClipRect (m_rtClient.left, m_rtClient.top, invalidateRect. left, m_rtClient.bottom );

MemDC. ExcludeClipRect (leftClipRect );

}


If (invalidateRect. top> m_rtClient.top)

{

CRect topClipRect (invalidateRect. left, m_rtClient.top, invalidateRect. right, invalidateRect. top );

MemDC. ExcludeClipRect (topClipRect );

}


If (invalidateRect. bottom <m_rtClient.bottom)

{

CRect bottomClipRect (invalidateRect. left, invalidateRect. bottom, invalidateRect. right, m_rtClient.bottom );

MemDC. ExcludeClipRect (bottomClipRect );

}


If (invalidateRect. right <m_rtClient.right)

{

CRect rightClipRect (invalidateRect. right, m_rtClient.top, m_rtClient.right, m_rtClient.bottom );

MemDC. ExcludeClipRect (rightClipRect );

}


This article from "fall in love with the floor" blog, please be sure to keep this source http://mmmn143.blog.51cto.com/2541616/1266181

Related Article

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.