BeginPaint/EndPaint or GetDC/ReleaseDC?

No.of Views2589
Bookmarked0 times
By  Geming Leader   On  02 Sep 2010 12:09:34
Tag : Windows SDK , General
Which is better, to use BeginPaint/EndPaint, or to use GetDC/ReleaseDC?
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at


This article is also available in my blog, Just Like a Magic.


Which is better, to use BeginPaint/EndPaint, or to use GetDC/ReleaseDC?Actually, it depends! If you are handling WM_PAINT, you should use BeginPaint/EndPaint. Otherwise, you should use GetDC/ReleaseDC.You already know that Windows sends WM_PAINT to your message queue as soon as a new area of the window's client area becomes invalidated.

If Windows finds an invalidated area, it sets a flag in the message pump indicating that a new WM_PAINT is waiting for processing. If no messages are waiting in the queue, it sends the WM_PAINT to the window procedure.

An area of the client area of the window becomes invalidated in many ways. For example, when a portion of the window covered by another window, Windows combines the area covered by the other window with the currently invalidated area of the window. In addition, you can €œinvalidate€ an area of the window using functions like InvalidateRect (to invalidate a rectangular area.) Those functions add the area specified to the currently invalidated area (i.e. combine the new area with the currently invalidated area of the window.)

Remember that, Windows continues sending WM_PAINT messages to your message queue as long as there's an invalidated area. Therefore, you should validate the client area before leaving the WM_PAINT handler block. That's why it is recommended using BeginPaint/EndPaint in WM_PAINT message handler because EndPaint does validate the entire client area of the window.

The following is a pseudo-code for EndPaint:

BOOL EndPaint(...)
	. . .

	validate client area
	e.g. call ValidateRect()

	release the DC

	do the necessary finalization
	. . .

Therefore, using GetDC/ReleaseDC in WM_PAINT would clog the message pump with a sequence of WM_PAINT messages that would divert your application from continuing its work, unless you validate the client area before jumping out of WM_PAINT handler.

On the other hand, using BeginPaint/EndPaint outside the WM_PAINT handler would validates the client area each time you call EndPaint. And that would prevent WM_PAINT from arriving to your message queue.

Another interesting point to consider is the following block of code inside the window procedure:

switch (uMsg)
		. . .

		case WM_PAINT:

			return 0;

		. . .

Why the previous code is considered wrong? Yes, you are right. It leaves the WM_PAINT with neither validating the client area nor passing the message to the default window procedure.

The default window procedure actually did nothing interesting inside the WM_PAINT. However, it is required to pass the WM_PAINT to the default window procedure if you are not going to handle WM_PAINT or you're not validating the client area inside the WM_PAINT handler. That's because Windows simply calls BeginPaint and EndPaint in pair. Thus, validates the client area.

case WM_PAINT:
			BeginPaint(hWnd, &ps);

			EndPaint(hWnd, &ps);
			return 0;

Thus, you should use BeginPaint/EndPaint in WM_PAINT only and GetDC/ReleaseDC in all other places in your code.

Sign Up to vote for this article
About Author
Geming Leader
Occupation-Software Engineer
Company-Just Like a Magic
Member Type-Expert
Joined date-30 Jul 2009
Home Page-
Blog Page-
Independent software developer, trainer, and technical writer from Egypt born in 1991
Other popularSectionarticles
    This writing talks about hard links and soft links; two of the nice features of NTFS file system. You can divide file links into two categories: 1) Normal Links (shortcuts) 2) Symbolic Links
    Published Date : 23/May/2010
    A union is a memory location that is shared by two or more different types of variables. A union provides a way for interpreting the same bit pattern in two or more different ways (or forms.) In fact, unions share structures lots of characteristics, like the way they defined and marshaled. It might be helpful to know that, like structures, unions can be defined inside a structure or even as a single entity. In addition, unions can define complex types inside, like structures too.
    Published Date : 02/Sep/2010
    This lesson focuses on how to change the screen resolution and color system programmatically via DirectX. It starts by an overview about how the Windows satisfies user’s need through the Display Settings window. Then, it digs into discussing how to retrieve these settings and to change these programmatically in the .NET environment.
    Published Date : 16/Feb/2010
There is no comments for this articles.
Leave a Reply
Display Name:
(not display in page for the security purphase)
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top