函数参考


_WinAPI_BeginPaint

准备绘制指定的窗口.

#Include <WinAPIEx.au3>
_WinAPI_BeginPaint ( $hWnd, ByRef $tPAINTSTRUCT )

参数

$hWnd 重新绘制的窗口句柄.
$tPAINTSTRUCT 将接受绘制信息的 $tagPAINTSTRUCT 结构.When the function call, this
parameter should be any valid variable, the function creates this structure itself.

返回值

成功: 返回指定窗口显示设备环境的句柄.
失败: 返回 0,并设置@error标志为非 0 值.

注意/说明

本函数自动设置该设备环境的剪辑区域,并排除更新区域以外的任何区域.
更新区域则由 _WinAPI_InvalidateRect() 或 _WinAPI_InvalidateRgn() 函数设置.
系统在调整大小、移动、创建、滚动或或任何其他操作以后更新客户区域.
如果更新区域标记为擦除, 本函数将发送 WM_ERASEBKGND 消息到窗口.

应用程序有可能不调用本函数, 除非响应一个 WM_PAINT 消息.
本函数的每次调用必须有一个对应的 _WinAPI_EndPaint() 函数调用.

相关

详情参考

在MSDN中搜索


示例/演示


#Include <APIConstants.au3>
#include <GUIConstantsEx.au3>
#Include <WinAPIEx.au3>

Opt('MouseCoordMode', 2)
Opt('MustDeclareVars', 1)

Global Const $STM_SETIMAGE = 0x0172
Global Const $STM_GETIMAGE = 0x0173

Global $hForm, $Msg, $Pic, $hPic, $Label, $hLabel, $Count = 0, $Pos, $hObj, $hBitmap, $hIcon, $hDll, $pDll, $hProc, $hDC, $hDestDC, $hDestSv
Global $aVertex[2][3] = [[0, 0, 0xAA00FF], [400, 400, 0x33004D]]

OnAutoItExitRegister('OnAutoItExit')

; 创建 GUI
$hForm = GUICreate('MyGUI', 400, 400)
$Pic = GUICtrlCreatePic('', 0, 0, 400, 400)
GUICtrlSetCursor(-1, 0)
$hPic = GUICtrlGetHandle($Pic)
$Label = GUICtrlCreateLabel('', 176, 176, 48, 48)
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
$hLabel = GUICtrlGetHandle($Label)

; 提取图标
$hIcon = _WinAPI_ShellExtractIcon(@SystemDir & '\shell32.dll', 130, 48, 48)

; 注册标签窗口过程
$hDll = DllCallbackRegister('_WinProc', 'ptr', 'hwnd;uint;wparam;lparam')
$pDll = DllCallbackGetPtr($hDll)
$hProc = _WinAPI_SetWindowLongEx($hLabel, $GWL_WNDPROC, $pDll)

; 创建渐变
$hDC = _WinAPI_GetDC($hPic)
$hDestDC = _WinAPI_CreateCompatibleDC($hDC)
$hBitmap = _WinAPI_CreateCompatibleBitmap($hDC, 400, 400)
$hDestSv = _WinAPI_SelectObject($hDestDC, $hBitmap)
_WinAPI_GradientFill($hDestDC, $aVertex)

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

; 设置渐变到控件
_SendMessage($hPic, $STM_SETIMAGE, 0, $hBitmap)
$hObj = _SendMessage($hPic, $STM_GETIMAGE)
If $hObj <> $hBitmap Then
    _WinAPI_DeleteObject($hBitmap)
EndIf

GUISetState()

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

Func _WinProc($hWnd, $iMsg, $wParam, $lParam)
    Switch $iMsg
        Case $WM_PAINT
            If $Count = 0 Then

                Local $tPAINTSTRUCT, $hDC

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

Func OnAutoItExit()
    _WinAPI_SetWindowLongEx($hLabel, $GWL_WNDPROC, $hProc)
    DllCallbackFree($hDll)
EndFunc   ;==>OnAutoItExit