Partager via


BeginPaint

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

This function prepares the specified window for painting and fills a PAINTSTRUCT structure 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 PAINTSTRUCT structure 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_PAINT message by using BeginPaint to 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.

#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 BeginPaint function sets the clipping region of the device context to exclude any area outside the update region.

The update region is set by the InvalidateRect function 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, BeginPaint sends a WM_ERASEBKGND message to the window.

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

Each call to BeginPaint must have a corresponding call to the EndPaint function.

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

If the window's class has a background brush, BeginPaint uses 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

Reference

EndPaint
InvalidateRect
ValidateRect
WM_ERASEBKGND
WM_PAINT
PAINTSTRUCT