Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

This function prepares the specified window for painting and fills a PAINTSTRUCTstructure with information about the painting.

Syntax

HDC BeginPaint( 
  HWND 
hwnd,
  LPPAINTSTRUCT 
lpPaint
);

Parameters

hwnd

Handle to the window to be repainted.

lpPaint

Long pointer to the PAINTSTRUCTstructure that will receive painting information.

Return Value

The handle to a display device context for the specified window indicates success.

NULL indicates that no display device context is available.

To get extended error information, call GetLastError.

Code Example

The following code example shows a window procedure that processes a WM_PAINTmessage by using BeginPaintto prepare a window for painting, and then draws an icon or bitmap in the window.

Note:
To make the following code example easier to read, error checking is not included. Do not use this code example in a release configuration unless you have modified it to include secure error handling.
Copy Code
#include <windows.h>
HICON ghIcon = NULL;
HBITMAP ghbmMask = NULL;
HBITMAP ghbmColor = NULL;
LRESULT APIENTRY MainWndProc(
		HWND hWnd, 			// window handle
		UINT message, 		 // type of message
		WPARAM wParam, 		// additional information
		LPARAM lParam)			// additional information
{
	switch (message) {
		case WM_PAINT:
			PAINTSTRUCT ps;
			HDC hdc;
			COLORREF crTxt, crBk;
			hdc = BeginPaint(hWnd, &ps);
			if (ghIcon)
				DrawIcon(hdc, 10, 10, ghIcon);
			if (ghbmMask)
				DrawBitmap(hdc, 10, 50, ghbmMask);
			if (ghbmColor)
				DrawBitmap(hdc, 50, 10, ghbmColor);
			EndPaint(hWnd, &ps);
			break;  
// Include case statements for other messages here.
		default:  // Pass the message on if unproccessed.
			return (DefWindowProc(hWnd, message, wParam, lParam));
}
	return (0L);
}

Remarks

The BeginPaintfunction sets the clipping region of the device context to exclude any area outside the update region.

The update region is set by the InvalidateRectfunction and by the system after sizing, moving, creating, scrolling, or any other operation that affects the client area.

If the update region is marked for erasing, BeginPaintsends a WM_ERASEBKGNDmessage to the window.

An application should not call BeginPaintexcept in response to a WM_PAINT message.

Each call to BeginPaintmust have a corresponding call to the EndPaintfunction.

If the caret is in the area to be painted, BeginPainthides the caret to prevent it from being erased.

If the window's class has a background brush, BeginPaintuses that brush to erase the background of the update region before returning.

Requirements

Header winuser.h
Library coredll.lib, Winmgr.lib
Windows Embedded CE Windows CE 1.0 and later
Windows Mobile Windows Mobile Version 5.0 and later

See Also