本帖最后由 298311657 于 2015-12-13 19:56 编辑
四张图片创建一个按钮,图片分别为按钮常规状态、激活状态、点击状态、禁用状态
使用我写的这个函数建立的按钮没有文字,所以可以在创建图片的时候带上文字
废话不多说,具体使用看代码。
包括图片的源码压缩包:
$GUI = GUICreate('使用图片创建动态按钮', 560, 385)
$Btn = _FlashButton(10, 10, 80, 25, "Bitmap_181.bmp", "Bitmap_186.bmp", "Bitmap_187.bmp")
GUICtrlSetCursor(-1, 0)
GUISetState(@SW_SHOW, $GUI)
Do
If GUIGetMsg() = -3 Then ExitLoop
Until GUIGetMsg() = -3
#include <GDIPlus.au3>
;~ By Crossdoor
;~ 创建一个闪烁图标的按钮
;~ _FlashButton( 标题, 左侧, 上方 , 宽度 , 高度 , 常规图片名 , 激活图片名, 点击图片名[[[, 禁用图片名], 字体大小], 字体样式] )
;~ 图片文件不存在,设置@error为0,并且返回-1
;~ 成功返回按钮控件标识符(控件ID)
Func _FlashButton($sText, $Left, $Top, $Width, $Height, $mImage, $hImage, $cImage, $dImage = "", $iSize = 13, $iStyle = 0)
Local $Btn
Local $ImageList_Create = DllCall("ComCtl32.dll", "hwnd", "ImageList_Create", "int", $Width, "int", $Height, "int", 8225, "int", 6, "int", 4)
If $dImage = "" Then $dImage = $mImage
Dim $aImage[6] = [$mImage, $hImage, $cImage, $dImage, $mImage, $mImage]
_GDIPlus_Startup()
For $i = 0 To 3
If FileExists($aImage[$i]) Then
If StringLen($sText) Then
$aImage[$i] = _SetText($aImage[$i], @TempDir & "\ex" & $i & ".bmp", $sText, $Width, $Height, $iSize, $iStyle)
EndIf
EndIf
Next
_GDIPlus_Shutdown()
$aImage[4] = $aImage[0]
$aImage[5] = $aImage[0]
Local $tPoint = DllStructCreate("int X;int Y")
Local $pPointX = DllStructGetPtr($tPoint, "X")
Local $pPointY = DllStructGetPtr($tPoint, "Y")
For $i = 0 To 5
Local $aResult = DllCall("ComCtl32.dll", "int", "ImageList_GetIconSize", "hwnd", $ImageList_Create[0], "ptr", $pPointX, "ptr", $pPointY)
Local $SizeX = DllStructGetData($tPoint, "X")
Local $SizeY = DllStructGetData($tPoint, "Y")
DllCall("ComCtl32.dll", "int", "ImageList_Add", "hwnd", $ImageList_Create[0], "hwnd", $aImage[$i], "hwnd", 0)
Next
Local $Btn = GUICtrlCreateButton("", $Left, $Top, $Width, $Height)
If @error Then Return SetError(@error, @extended, -2)
Local $tBUTTON_IMAGELIST = DllStructCreate("hwnd ImageList;int Left;int Top;int Right;int Bottom;uint Align")
DllStructSetData($tBUTTON_IMAGELIST, "ImageList", $ImageList_Create[0])
DllStructSetData($tBUTTON_IMAGELIST, "Left", 1)
DllStructSetData($tBUTTON_IMAGELIST, "Top", 1)
DllStructSetData($tBUTTON_IMAGELIST, "Right", 1)
DllStructSetData($tBUTTON_IMAGELIST, "Bottom", 1)
DllStructSetData($tBUTTON_IMAGELIST, "Align", 4)
DllCall("user32.dll", "lparam", "SendMessage", "hwnd", GUICtrlGetHandle($Btn), "int", 0x1602, "wparam", 0, "lparam", DllStructGetPtr($tBUTTON_IMAGELIST))
If @error Then Return SetError(@error, @extended, -3)
Return $Btn
EndFunc ;==>_FlashButton
Func _SetText($pic, $savepic, $str, $iWidth=0, $iHeight=0, $iSize = 11, $iStyle = 0)
$hImage = _GDIPlus_ImageLoadFromFile($pic)
;~ If @error Then MsgBox(0,1,$pic)
If $iWidth=0 Then $iWidth = _GDIPlus_ImageGetWidth($hImage)
If $iHeight=0 Then $iHeight = _GDIPlus_ImageGetHeight($hImage)
Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
Local $hGraphic = _GDIPlus_ImageGetGraphicsContext($hBitmap);画布
_GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage, 0, 0, $iWidth, $iHeight);在画布上按指定大小画$hImage
Local $hBrush = _GDIPlus_BrushCreateSolid(0xffffff7f) ;文字颜色
Local $hFormat = _GDIPlus_StringFormatCreate()
Local $hFamily = _GDIPlus_FontFamilyCreate("Arial") ;字体
Local $hFont = _GDIPlus_FontCreate($hFamily, $iSize, $iStyle, 2) ;大小
Local $tLayout = _GDIPlus_RectFCreate(0, 0, $iWidth, $iHeight)
Local $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $str, $hFont, $tLayout, $hFormat)
If Not @error Then
DllStructSetData($aInfo[0], "X", ($iWidth-DllStructGetData($aInfo[0], "Width"))/2);设置字符串X坐标
DllStructSetData($aInfo[0], "Y", ($iHeight-DllStructGetData($aInfo[0], "Height"))/2);设置字符串Y坐标
EndIf
_GDIPlus_GraphicsDrawStringEx($hGraphic, $str, $hFont, $aInfo[0], $hFormat, $hBrush)
_GDIPlus_FontDispose($hFont)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_BrushDispose($hBrush)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_ImageDispose($hImage)
Return _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
EndFunc
|