找回密码
 加入
搜索
查看: 6893|回复: 13

[GUI管理] png+label闪烁[已解决]

 火.. [复制链接]
发表于 2011-2-5 10:31:08 | 显示全部楼层 |阅读模式
本帖最后由 蜘蛛抱蛋 于 2011-3-9 16:49 编辑

目的是解决这篇帖子提到的闪烁问题。这是官网上找到的一段代码
#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#Include <GDIPlus.au3>
#include <WinAPI.au3>

Global $sFile = @ScriptDir & "\Splash.png"
Global $hGui, $iW, $iH, $hBmp

_GDIPlus_Startup()

$hImage1 = _GDIPlus_ImageLoadFromFile($sFile)
$iW = _GDIPlus_ImageGetWidth($hImage1)
$iH = _GDIPlus_ImageGetHeight($hImage1)

$hGUI = GUICreate("", $iW, $iH, -1, -1, $WS_POPUP, BitOr($WS_EX_LAYERED, $WS_EX_TOPMOST))
GUISetState()

;Add the text to a new image
$hBmp = _ImageDrawText($hImage1, "Text drawn on image", 100, 200, 0xFFFFFF)

;fade in png image
For $i = 0 To 255 Step 1
    SetBitmap($hGUI, $hBmp, $i)
Next
;Finished with the new text written image delete it
_WinAPI_DeleteObject($hBmp)
Sleep(2000)

;Since the original png is still open lets dribble some more text..
$hBmp = _ImageDrawText($hImage1, "Once upon a time....", 0, 0, 0xFF0000, 16, 2, "Impact")
SetBitmap($hGUI, $hBmp, 255)
_WinAPI_DeleteObject($hBmp)
Sleep(2000)

$hBmp = _ImageDrawText($hImage1, "Long long ago....", 0, 60, 0x00FF00, 16, 1, "Tahoma")
SetBitmap($hGUI, $hBmp, 255)
_WinAPI_DeleteObject($hBmp)
Sleep(2000)

$hBmp = _ImageDrawText($hImage1, "When I was young and in my prime....", 0, 80, 0x0000FF, 9, 3, "Arial")
SetBitmap($hGUI, $hBmp, 255)
_WinAPI_DeleteObject($hBmp)
Sleep(2000)

$hBmp = _ImageDrawText($hImage1, "Wait....", 60, 120, 0x00FFFF, 20, 0, "Arial")
SetBitmap($hGUI, $hBmp, 255)
_WinAPI_DeleteObject($hBmp)
Sleep(2000)

$hBmp = _ImageDrawText($hImage1, "Scratch that part....", 20, 130, 0x00FF00, 12, 8, "Comic Sans MS")
SetBitmap($hGUI, $hBmp, 255)
_WinAPI_DeleteObject($hBmp)
Sleep(2000)

$hBmp = _ImageDrawText($hImage1, "To Be Continued...." & @LF & "NOT!", 50, 160, 0xFFFF00, 12, 4, "Impact")
SetBitmap($hGUI, $hBmp, 255)
_WinAPI_DeleteObject($hBmp)
Sleep(2000)

;set the original non texted png image back to the window
SetBitmap($hGUI, $hImage1, 255)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE, $GUI_EVENT_PRIMARYDOWN, $GUI_EVENT_PRIMARYUP
            _GDIPlus_ImageDispose($hImage1)
            _GDIPlus_Shutdown()
            Exit
    EndSwitch
Wend

; $hImage = The handle to your open image file
; $sText = Sting of text to draw on the picture
; $iX = X postion the text will be drawn at
; $iY = Y position the text will be drawn at
; $iRGB = the RGB color to draw the text (0x000000 to 0xffffff)
; $iSize = The size of the font
; $iStyle = 0 None (Default)
;           1 Bold
;           2 Italic
;           4 Underline
;           8 Strikethrough
;           (Add together for combination, eg: 3 = Bold + Italic)
; $sFont = Name of font to use
Func _ImageDrawText($hImage, $sText, $iX = 0, $iY = 0, $iRGB = 0x000000, $iSize = 9, $iStyle = 0, $sFont = "Arial") 
    Local $w, $h, $hGraphic1, $hBitmap, $hGraphic2, $hBrush, $hFormat, $hFamily, $hFont, $tLayout, $aInfo
    $w = _GDIPlus_ImageGetWidth($hImage)
    $h = _GDIPlus_ImageGetHeight($hImage)
    
    ;Create a new bitmap, this way the original opened png is left unchanged
    $hGraphic1 = _GDIPlus_GraphicsCreateFromHWND(_WinAPI_GetDesktopWindow())
    $hBitmap = _GDIPlus_BitmapCreateFromGraphics($w, $h, $hGraphic1)
    $hGraphic2 = _GDIPlus_ImageGetGraphicsContext($hBitmap) 
    
    ; Draw the original opened png into my newly created bitmap
    _GDIPlus_GraphicsDrawImageRect($hGraphic2, $hImage, 0, 0, $w, $h)

    ;Create the font
    $hBrush = _GDIPlus_BrushCreateSolid ("0xFF" & Hex($iRGB, 6))
    $hFormat = _GDIPlus_StringFormatCreate()
    $hFamily = _GDIPlus_FontFamilyCreate ($sFont)
    $hFont = _GDIPlus_FontCreate ($hFamily, $iSize, $iStyle)
    $tLayout = _GDIPlus_RectFCreate ($iX, $iY, 0, 0)
    $aInfo = _GDIPlus_GraphicsMeasureString ($hGraphic2, $sText, $hFont, $tLayout, $hFormat)
    
    ;Draw the font onto the new bitmap
    _GDIPlus_GraphicsDrawStringEx ($hGraphic2, $sText, $hFont, $aInfo[0], $hFormat, $hBrush)
    
    ;Cleanup the no longer needed resources
    _GDIPlus_FontDispose ($hFont)
    _GDIPlus_FontFamilyDispose ($hFamily)
    _GDIPlus_StringFormatDispose ($hFormat)
    _GDIPlus_BrushDispose ($hBrush)
    _GDIPlus_GraphicsDispose ($hGraphic2)
    _GDIPlus_GraphicsDispose ($hGraphic1)

    ;Return the new bitmap
    Return $hBitmap
EndFunc 
    
Func SetBitmap($hGUI, $hImage, $iOpacity)
    Local Const $AC_SRC_ALPHA = 1
    Local Const $ULW_ALPHA = 2
    Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend
    
    $hScrDC = _WinAPI_GetDC(0)
    $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", _GDIPlus_ImageGetWidth($hImage))
    DllStructSetData($tSize, "Y", _GDIPlus_ImageGetHeight($hImage))
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend = DllStructCreate($tagBLENDFUNCTION)
    $pBlend = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha", $iOpacity)
    DllStructSetData($tBlend, "Format", $AC_SRC_ALPHA)
    
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteDC($hMemDC)
EndFunc ;==>SetBitmap

各位有更巧的办法吗?
========================
算了,大家别回复了,关帖
发表于 2011-3-5 00:11:11 | 显示全部楼层
好东西,收藏了
发表于 2011-3-5 09:48:28 | 显示全部楼层
嗯,不错,收藏了
发表于 2011-3-5 11:17:50 | 显示全部楼层
美化的功夫不错哦
发表于 2011-3-9 06:38:37 | 显示全部楼层
不错的代码。。做个记号
发表于 2011-3-9 09:46:11 | 显示全部楼层
确实不错哦  值得学习
发表于 2011-3-9 15:55:27 | 显示全部楼层
学习了。。
发表于 2011-3-10 19:37:48 | 显示全部楼层
还没用到,支持下。
发表于 2011-11-24 16:01:11 | 显示全部楼层
学习了,很好的代码
发表于 2012-1-10 20:55:37 | 显示全部楼层
谢谢分享。。。。谢谢
发表于 2012-5-3 00:11:33 | 显示全部楼层
怎么没效果???
发表于 2012-7-29 11:22:22 | 显示全部楼层
有点深奥 看不懂 慢慢研究之~~~~
发表于 2016-9-2 20:18:36 | 显示全部楼层
能做出这个效果,说明作者对GDI+比较了解,赞一个!

我现在也面临GDI+的问题,初学者,还不熟
发表于 2016-9-7 12:01:31 | 显示全部楼层
运行失败了,108行出错。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-11-17 00:17 , Processed in 0.073943 second(s), 19 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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