BEGINPAINT&&GETDC Difference

Source: Internet
Author: User
Tags textout

This is a problem with Windows programming.
The first kind of case shows the word is normal.
Case WM_PAINT:
GDC = BeginPaint (hwnd, &PS);
TextOut (GDC, 0, 0, S, strlen (s));
EndPaint (hwnd, &PS);
Break
The second situation shows the word flashing continuously.
Case WM_PAINT:
GDC = GetDC (hwnd);
TextOut (GDC, 0, 0, S, strlen (s));
ReleaseDC (hwnd, GDC);
Break
Ask about the role of two functions.

BeginPaint () and EndPaint () can delete WM_PAINT messages in message queues and make invalid zones valid.
GetDC () and ReleaseDC () do not delete or invalidate the invalid zone, so the invalid zone still exists when the program jumps out of the WM_PAINT. The system continues to send WM_PAINT messages, so the program continues to process WM_PAINT messages.

The equivalent of BeginPaint, EndPaint will tell GDI inside, this window needs to redraw the place has been repaint, so that the WM_PAINT after the return to the system, the system will not be again WM_PAINT, And GetDC didn't tell the system that the window needed to be repaint, after you return the program to the system, the system has been thought to inform you of the redraw command you have not obediently executed or executed error, so in the message idle, it will continue to send wm_paint urge you to draw, causing the program to die.

Invalid zone:

An invalid area is an area that needs to be repaint, which means that the current content is old and obsolete.
Let's say that A is a new pop-up dialog box or an existing dialog box that is activated. The A dialog box is placed in front of the original active dialog box B, causing some or all of the dialog box B to be overwritten, and when dialog box A is moved or closed, the place where dialog B is originally overwritten is again visible. That part of the covered area is called the Void Zone.
Only when a window message is idle does the system take time to check to see if the invalid area of the window is Non-null (WM_PAINT is the lowest priority.) This is why the system is busy when the window and desktop often appear white, refreshing, drag and drop traces, and so on the cause of the phenomenon, if not empty, the system will send WM_PAINT. So be sure to use BeginPaint, EndPaint invalid area set to empty, otherwise WM_PAINT will always be sent.

Why Windows wants to present the concept of an invalid zone.

This is to accelerate.
Because the device descriptors used by BeginPaint and EndPaint will only be painted in the current invalid area, the painting in the effective area will be filtered automatically, as we all know, WIN GDI's painting speed is relatively slow, so can save a pixel to save one, not stingy, This can effectively speed up the painting.
Visible BeginPaint, EndPaint is relatively "passive", only when the window is new and destroyed only to repaint.
And GetDC is used for active drawing, so wherever you point to it, it hits. It's all painted without judgment, and the void area doesn't matter. dialog box is not covered without being destroyed, it is very healthy, the system does not require it to repaint, but the developer in some cases need it to actively repaint: such as a timed-change window, this time to Wm_timer processing code with GETDC. Then use BeginPaint, EndPaint, because the invalid area is empty, all painting operations will be filtered out.

eg

Paintstruct PS;
HDC HDC = BeginPaint (HWND,&PS);
Create a DC that matches the device
HDC Hdcmem = CreateCompatibleDC (HDC);
Load the bitmap
HANDLE hbmp= LoadImage (G_hinst_mainwnd,makeintresource (Idb_mainwnd), image_bitmap,0,0,0);
Select the bitmap into the compatible device context
Hgdiobj Holdsel = SelectObject (hdcmem,hbmp);
Get the bitmap dimensions from the bitmap
BITMAP bmp;
GetObject (Hbmp,sizeof (BITMAP), &bmp);
Get the window area
RECT RC;
GetClientRect (HWND,&RC);
Copy the bitmap image from the ' Memory DC to the ' screen DC
BitBlt (hdc,rc.left,rc.top,bmp.bmwidth,bmp.bmheight,hdcmem,0,0,srccopy);
Restore original bitmap selection and destroy the memory DC
SelectObject (Hdcmem,holdsel);
DeleteDC (HDCMEM);
EndPaint (HWND,&PS);
return 0;

/////////////////////////

The following is a more detailed introduction

//========================================================================
TITLE:
eVC the difference between a bitmap--beginpaint () and GetDC ()
AUTHOR:
Norains
DATE:
Tuesday 29-august-2006
//========================================================================
1.BeginPaint () and GetDC ()
It is convenient to draw a bitmap in eVC, there are a lot of ready-made functions to call, all we have to pay attention to is the use of beginpaint () or GetDC ().
Because the code is simpler, there is no more explanation.

This is the message loop function:
Lresult CALLBACK Mainwndproc (HWND hwnd,uint wmsg,wparam Wparam,lparam)
{
......

Switch (WMSG)
{
Case WM_PAINT:
Onpaintmainwnd (Hwnd,wmsg,wparam,lparam);
Break

......

}
return DefWindowProc (Hwnd,wmsg,wparam,lparam);

......

}

A function that responds to the WM_PAINT message, where the bitmap is drawn:
Lresult Onpaintmainwnd (HWND hwnd,uint wmsg,wparam wparam,lparam LPARAM)
{
Paintstruct PS;
HDC HDC = BeginPaint (HWND,&PS);
Create a DC that matches the device
HDC Hdcmem = CreateCompatibleDC (HDC);
Load the bitmap
HANDLE hbmp= LoadImage (G_hinst_mainwnd,makeintresource (Idb_mainwnd), image_bitmap,0,0,0);
Select the bitmap into the compatible device context
Hgdiobj Holdsel = SelectObject (hdcmem,hbmp);
Get the bitmap dimensions from the bitmap
BITMAP bmp;
GetObject (Hbmp,sizeof (BITMAP), &bmp);
Get the window area
RECT RC;
GetClientRect (HWND,&RC);
Copy the bitmap image from the ' Memory DC to the ' screen DC
BitBlt (hdc,rc.left,rc.top,bmp.bmwidth,bmp.bmheight,hdcmem,0,0,srccopy);
Restore original bitmap selection and destroy the memory DC
SelectObject (Hdcmem,holdsel);
DeleteDC (HDCMEM);
EndPaint (HWND,&PS);
return 0;
}

We all know that BeginPaint () and endpaint () need to be used together, and that these two functions can only be used in the corresponding functions of the WM_PAINT message. If we replace the two drawing functions in the WM_PAINT response function with the GETDC () and ReleaseDC () what will be the result?
That
HDC HDC = BeginPaint (HWND,&PS); --> HDC HDC = GetDC (hWnd);
EndPaint (HWND,&PS); --> ReleaseDC (HWND,HDC);

Compile and run the program, we found that the window was blank, as if there were no bitmaps drawn. But not really, we use single step debugging, we can find that the bitmap has been mapped out, but also by the background color erased. So, if you need to use GETDC (), we have to add the wm_ to the message loop function ERASEBKGND Processing:
Lresult CALLBACK Mainwndproc (HWND hwnd,uint wmsg,wparam Wparam,lparam)
{
Switch (WMSG)
{
Case WM_PAINT:
Onpaintmainwnd (Hwnd,wmsg,wparam,lparam);
Break
Case WM_ERASEBKGND
return 0;
}
return DefWindowProc (Hwnd,wmsg,wparam,lparam);
}
//
At this point we can see the difference between BeginPaint (), EndPaint () and GetDC (), ReleaseDC (). The first pair can only be used in the WM_PAINT response function and will not be erased when the background is drawn; the latter pair is available everywhere, but if used in wm_ In the paint response function, the next step is to erase the background of the response function of the WM_ERASEBKGND message.

2. Drawing Flicker Problem
Sometimes when we draw a large number of screens, there may be a screen flicker problem, and double buffering is possible at this time. The step is to create a memory DC, then draw to the memory DC, and finally copy the contents of the memory DC to the display DC to complete the drawing. The specific process is not complex, combined with the code to explain.
PS: This piece of code is also the corresponding WM_PAINT message.

Paintstruct PS;
HDC HDC;
Get Screen display DC
HDC = BeginPaint (hWnd, &ps);

Create memory DC
HDC Hdcmem = CreateCompatibleDC (HDC);
Create a BMP memory space
Hbitmap hbmp = CreateCompatibleBitmap (hdc,screen_width,screen_height);
Allocating BMP memory space to a memory DC
Hgdiobj Holdsel = SelectObject (hdcmem,hbmp);

This is the user needs to draw the picture, all to the memory DC drawing
Rectangle (Hdcmem,0,0,screen_width,screen_height);
Drawmenubutton (HDCMEM);

Copy the contents of the memory DC to the screen display DC to complete the display
BitBlt (hdc,0,0,screen_width,screen_height,hdcmem,0,0,srccopy);
Clear Resources
SelectObject (Hdcmem,holdsel);
DeleteDC (HDCMEM);
EndPaint (HWND,&PS);

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.