# Function Reference

# _WinAPI_BeginPaint

Prepares the specified window for painting

#include <WinAPIGdi.au3>
_WinAPI_BeginPaint ( $hWnd, ByRef $tPAINTSTRUCT )

## Parameters

$hWnd | Handle to the window to be repainted.
$tPAINTSTRUCT | $tagPAINTSTRUCT structure that will receive painting information. When the function call, this parameter should be any valid variable, the function creates this structure itself.

## Return Value

Success: | Handle to a display device context for the specified window.
Failure: | 0, call _WinAPI_GetLastError() to get extended error information.

## Remarks

This function automatically sets the clipping region of the device context to exclude any area outside the update region.
The update region is set by the _WinAPI_InvalidateRect() or _WinAPI_InvalidateRgn() 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, _WinAPI_BeginPaint() sends a WM_ERASEBKGND message to the window.
An application should not call _WinAPI_BeginPaint() except in response to a WM_PAINT message.
Each call to _WinAPI_BeginPaint() must have a corresponding call to the _WinAPI_EndPaint() function.

## Related

_WinAPI_InvalidateRect, _WinAPI_InvalidateRgn

## See Also

Search BeginPaint in MSDN Library.

## Example

    #include <GUIConstantsEx.au3>
    #include <SendMessage.au3>
    #include <StaticConstants.au3>
    #include <WinAPIGdi.au3>
    #include <WinAPIGdiDC.au3>
    #include <WinAPIHObj.au3>
    #include <WinAPIShellEx.au3>
    #include <WinAPISysWin.au3>
    #include <WindowsNotifsConstants.au3>

    Opt('MouseCoordMode', 2)

    Global $g_iCount = 0

    OnAutoItExitRegister('OnAutoItExit')

    ; Create GUI
    Local $hForm = GUICreate('Test ' & StringReplace(@ScriptName, '.au3', '()'), 400, 400)
    Local $idPic = GUICtrlCreatePic('', 0, 0, 400, 400)
    GUICtrlSetCursor(-1, 0)
    Local $hPic = GUICtrlGetHandle($idPic)
    Local $idLabel = GUICtrlCreateLabel('', 176, 176, 48, 48)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    Global $g_hLabel = GUICtrlGetHandle($idLabel)

    ; Extract icon
    Local $hIcon = _WinAPI_ShellExtractIcon(@SystemDir & '\shell32.dll', 130, 48, 48)

    ; Register label window proc
    Global $g_hDll = DllCallbackRegister('_WinProc', 'ptr', 'hwnd;uint;wparam;lparam')
    Local $pDll = DllCallbackGetPtr($g_hDll)
    Global $g_hProc = _WinAPI_SetWindowLong($g_hLabel, $GWL_WNDPROC, $pDll)

    ; Create gradient
    Local $hDC = _WinAPI_GetDC($hPic)
    Local $hDestDC = _WinAPI_CreateCompatibleDC($hDC)
    Local $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC, 400, 400)
    Local $hDestSv = _WinAPI_SelectObject($hDestDC, $hBitmap)
    Local $aVertex[2][3] = [[0, 0, 0xAA00FF], [400, 400, 0x33004D]]
    _WinAPI_GradientFill($hDestDC, $aVertex)

    _WinAPI_ReleaseDC($hPic, $hDC)
    _WinAPI_SelectObject($hDestDC, $hDestSv)
    _WinAPI_DeleteDC($hDestDC)

    ; Set gradient to control
    _SendMessage($hPic, $STM_SETIMAGE, 0, $hBitmap)
    Local $hObj = _SendMessage($hPic, $STM_GETIMAGE)
    If $hObj <> $hBitmap Then
        _WinAPI_DeleteObject($hBitmap)
    EndIf

    GUISetState(@SW_SHOW)

    Local $aPos
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $GUI_EVENT_PRIMARYDOWN
                $aPos = MouseGetPos()
                If _WinAPI_PtInRectEx($aPos[0], $aPos[1], 0, 0, 400, 400) Then
                    GUICtrlSetPos($idLabel, $aPos[0] - 24, $aPos[1] - 24)
                EndIf
        EndSwitch
    WEnd

    Func _WinProc($hWnd, $iMsg, $wParam, $lParam)
        Switch $iMsg
            Case $WM_PAINT
                If $g_iCount = 0 Then
                    Local $tPAINTSTRUCT, $hDC

                    $g_iCount += 1
                    $hDC = _WinAPI_BeginPaint($hWnd, $tPAINTSTRUCT)
                    _WinAPI_DrawIconEx($hDC, 0, 0, $hIcon, 48, 48)
                    _WinAPI_EndPaint($hWnd, $tPAINTSTRUCT)
                    $g_iCount -= 1
                    Return 0
                EndIf
        EndSwitch
        Return _WinAPI_CallWindowProc($g_hProc, $hWnd, $iMsg, $wParam, $lParam)
    EndFunc   ;==>_WinProc

    Func OnAutoItExit()
        _WinAPI_SetWindowLong($g_hLabel, $GWL_WNDPROC, $g_hProc)
        DllCallbackFree($g_hDll)
    EndFunc   ;==>OnAutoItExit