Share via


BeginPaint (Compact 2013)

3/28/2014

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

For readability, the following code example does not contain security checking or error handling. Do not use the following code in a production environment.

#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

See Also

Reference

GDI Functions
EndPaint
InvalidateRect
ValidateRect
WM_ERASEBKGND
WM_PAINT
PAINTSTRUCT