找回密码
 加入
搜索
查看: 24313|回复: 40

[原创] 使用图片创建闪烁图标按钮

 火... [复制链接]
发表于 2010-5-9 02:41:49 | 显示全部楼层 |阅读模式
本帖最后由 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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×

评分

参与人数 1威望 +2 金钱 +40 贡献 +5 收起 理由
afan + 2 + 40 + 5

查看全部评分

发表于 2010-5-9 11:19:57 | 显示全部楼层
O(∩_∩)O谢谢分享,回去学习一下
发表于 2010-5-9 13:04:18 | 显示全部楼层
留名收藏~~~~这个可是好东西
发表于 2010-5-9 13:29:05 | 显示全部楼层
汗。。 啥玩意么。 。没反应。。。很死板
发表于 2010-5-9 19:17:25 | 显示全部楼层
我也来一个
#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
#Include <GuiButton.au3>
GUICreate("Buttons", 510, 400)
$hImage = _GUIImageList_Create(141, 56, 5, 3, 6)
    
_GUIImageList_AddBitmap($hImage, "1.bmp")
_GUIImageList_AddBitmap($hImage, "2.bmp")
_GUIImageList_AddBitmap($hImage, "3.bmp")
_GUIImageList_AddBitmap($hImage, "1.bmp")
_GUIImageList_AddBitmap($hImage, "1.bmp")



    $btn = GUICtrlCreateButton("Button1", 10, 10, 141, 56)
    _GUICtrlButton_SetImageList($btn, $hImage,4)

 GUISetState()
    
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
    WEnd

    Exit

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2010-5-11 11:03:21 | 显示全部楼层
回复 1# 298311657

是否很消耗资源?
发表于 2011-2-13 07:48:05 | 显示全部楼层
       有点意思 漂亮
发表于 2011-2-19 18:00:03 | 显示全部楼层
这个很实用么
发表于 2011-4-15 09:31:44 | 显示全部楼层
很实用谢谢楼主
发表于 2011-4-15 09:31:49 | 显示全部楼层
很实用谢谢楼主
发表于 2011-4-15 16:30:23 | 显示全部楼层
太谢谢啦啊
发表于 2011-5-4 18:55:32 | 显示全部楼层
好贵哦  才米有烟鬼啊
发表于 2011-5-4 18:55:42 | 显示全部楼层
好贵哦  才米有烟鬼啊
发表于 2011-5-4 19:06:47 | 显示全部楼层
下了  感觉不是很强大
发表于 2011-6-2 22:03:44 | 显示全部楼层
很实用谢谢楼主
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-4-26 21:27 , Processed in 0.082571 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表