Chapter I: ExcludeClipRect exclude a rectangle in a cut area. As a result, this rectangle is not drawn when the cut area is drawn. for details about the ExcludeClipRect function, see MSDN. This function is used to exclude a part of a region and is often used to draw pictures. For example, you can draw an image in the customer area of a window, as shown in the following code:
- Case WM_PAINT:
- {
- Hdc = BeginPaint (hWnd, & ps );
- RECT rc = {0 };
- GetClientRect (hWnd, & rc );
- HDC hMemDC = CreateCompatibleDC (ps. hdc );
- SelectObject (hMemDC, g_hBmpAllWstDskWallpaper); // select a bitmap
-
- // ExcludeClipRect (ps. hdc, 200,100,); // exclude some regions. A line
- BitBlt (ps. hdc, 300,200, hMemDC, SRCCOPY );
- DeleteDC (hMemDC );
- EndPaint (hWnd, & ps );
- }
This Code creates a bitmap in the client area of the window. but pay attention to line A, which is commented out, and then removed from the comment, run the program again, you can see that although this bitmap is still drawn in the customer area of the window, however, the rectangle area {0, 0,200,100} still displays the background color of the window. In other words, the bitmap is missing. this is because ExcludeClipRect removes the {0, 0,200,100} rectangle from the cut area of the window, so that GDI will not draw this rectangular area. the Chaper II: ExcludeClipRect function does not release the excluded rectangular area. Change Row A in Chapter I code:
- BOOL g_bCall = TRUE; // global variable.
- If (g_bCall)
- {
- G_bCall = FALSE;
- ExcludeClipRect (ps. hdc, 200,100,); // exclude some regions and call. A line only once.
- }
Run the program again and change the window size. We can find that the rectangle area {0, 0,200,100} of the customer area is never drawn. it can be seen that once this function is called, the rectangular area excluded by this function will never be drawn on the DC. Is there any way to restore the area? In fact, it is very simple. Just "retrieve" the rectangular area that has been "Exclude. the method is that the program creates another cut area, so that its size and location are the same as the previous "Exclude", and then calls ExtSelectClipRgn. Note that the last parameter must use RGN_OR, this indicates merging two cut regions. this is equivalent to retrieving the rectangular area of the "Exclude. the following code is used:
- # Define RECT_WIDTH (rt) (rt. right-rt.left)
- # Define RECT_HEIGHT (rt) (rt. bottom-rt.top)
- RECT g_rcExclud = {0 };
- BOOL g_bCancelExcludeRect = FALSE;
- Case WM_PAINT:
- {
- Hdc = BeginPaint (hWnd, & ps );
- RECT rc = {0 };
- GetClientRect (hWnd, & rc );
- HDC hMemDC = CreateCompatibleDC (ps. hdc );
- SelectObject (hMemDC, g_hBmpAllWstDskWallpaper); // select a bitmap
-
- // The "exclud" drop area is in the lower right corner.
- G_rcExclud.left = rc. right-200;
- G_rcExclud.top = rc. bottomtom-100;
- G_rcExclud.right = rc. right;
- G_rcExclud.bottom = rc. bottom;
-
- HRGN hrgn = NULL; // fill the last "exclud" drop area back.
- If (g_bCancelExcludeRect & RECT_WIDTH (g_rcExclud) & RECT_HEIGHT (g_rcExclud ))
- {
- Hrgn = CreateRectRgn (g_rcExclud.left, g_rcExclud.top, g_rcExclud.right, g_rcExclud.bottom );
- ExtSelectClipRgn (ps. hdc, hrgn, RGN_OR );
- FillRect (ps. hdc, & g_rcExclud, (HBRUSH) GetStockObject (COLOR_WINDOW); // erase the area of the last drawn image.
- }
-
- (! G_bCancelExcludeRect) // "exclud" Drop the area in the lower right corner of the window.
- ExcludeClipRect (ps. hdc, g_rcExclud.left, g_rcExclud.top, RECT_WIDTH (g_rcExclud), RECT_HEIGHT (g_rcExclud ));
- BitBlt (ps. hdc, 400,300, hMemDC, SRCCOPY); // draw a bitmap in the lower right corner.
-
- DeleteDC (hMemDC );
- If (hrgn! = NULL) DeleteObject (hrgn );
- EndPaint (hWnd, & ps );
- }
Chapter III: Using ExcludeClipRect to achieve non-blinking images some netizens wrote related articles: http://dev.10086.cn/cmdn/wiki/index.php? Edition-view-6349-1.html this article some good, but there is still a problem, that is, Chapter II mentioned ExcludeClipRect will not release the "exclude" drop area, in this way, if the location of the bitmap changes due to the size of the window, the "exclude" area will not be erased by the background. in addition, this article still does not clarify clearly, that is, the solution to image flickering is that the background of the window does not need to be erased, while the foreground view of the window is drawn, and the image area is drawn using a bitmap, other areas are drawn with the background color of the window, which is equivalent to sticking the canvas with a hole (this part is the image, and the other part is drawn with the background color) to the window, so that no overlap is drawn, the modified code is as follows:
- Case WM_PAINT:
- {
- Hdc = BeginPaint (hWnd, & ps );
- RECT rc = {0 };
- GetClientRect (hWnd, & rc );
-
- HDC hMemDC = CreateCompatibleDC (ps. hdc );
- SelectObject (hMemDC, g_hBmpAllWstDskWallpaper );
-
- HRGN hrgn = NULL;
- If (RECT_WIDTH (g_rcExclud) & RECT_HEIGHT (g_rcExclud ))
- {
- Hrgn = CreateRectRgn (g_rcExclud.left, g_rcExclud.top, g_rcExclud.right, g_rcExclud.bottom );
- ExtSelectClipRgn (ps. hdc, hrgn, RGN_OR); // restore the region that was "excude" last time. required. Otherwise, this part will not be drawn.
- FillRect (ps. hdc, & g_rcExclud, (HBRUSH) GetStockObject (COLOR_WINDOW); // erase the area of the last drawn image.
- }
-
- G_rcExclud.left = rc. right-300;
- G_rcExclud.top = rc. bottomtom-200;
- G_rcExclud.right = rc. right;
- G_rcExclud.bottom = rc. bottom;
-
- BitBlt (ps. hdc, g_rcExclud.left, g_rcExclud.top, g_rcExclud.right-g_rcExclud.left, g_rcExclud.bottom-g_rcExclud.top, hMemDC, SRCCOPY );
- ExcludeClipRect (ps. hdc, g_rcExclud.left, g_rcExclud.top, g_rcExclud.right, g_rcExclud.bottom); // exclude the region occupied by the image
- FillRect (ps. hdc, & rc, (HBRUSH) GetStockObject (COLOR_WINDOW); // draw other regions with the background color of the window.
-
- DeleteObject (hrgn );
- DeleteDC (hMemDC); // releases the memory device Environment
- EndPaint (hWnd, & ps );
- Return 0;
- }
- Break;
- Case WM_SIZE:
- InvalidateRect (hWnd, NULL, FALSE); // The last parameter is FALSE, indicating that the background is not erased.
- Break;
Finally, change the window size and observe the image, which is always in the lower right corner of the window, and the image does not flash any more.
This article from the "zero one small building" blog, please be sure to keep this source http://jetyi.blog.51cto.com/1460128/642401