蜘蛛抱蛋 发表于 2011-2-5 10:31:08

png+label闪烁[已解决]

本帖最后由 蜘蛛抱蛋 于 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, $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
各位有更巧的办法吗?
========================
算了,大家别回复了,关帖

xowen 发表于 2011-3-5 00:11:11

好东西,收藏了

devilma 发表于 2011-3-5 09:48:28

嗯,不错,收藏了

wsfda 发表于 2011-3-5 11:17:50

美化的功夫不错哦

harrye9016 发表于 2011-3-9 06:38:37

不错的代码。。做个记号

5i5wei 发表于 2011-3-9 09:46:11

确实不错哦值得学习

yyyltyyyy 发表于 2011-3-9 15:55:27

学习了。。

guang19831217 发表于 2011-3-10 19:37:48

还没用到,支持下。

80ren 发表于 2011-11-24 16:01:11

学习了,很好的代码

xyhqqaa 发表于 2012-1-10 20:55:37

谢谢分享。。。。谢谢

penguinl 发表于 2012-5-3 00:11:33

怎么没效果???

juneqin2011 发表于 2012-7-29 11:22:22

有点深奥 看不懂 慢慢研究之~~~~

sdycn2012 发表于 2016-9-2 20:18:36

能做出这个效果,说明作者对GDI+比较了解,赞一个!

我现在也面临GDI+的问题,初学者,还不熟

heroxianf 发表于 2016-9-7 12:01:31

运行失败了,108行出错。
页: [1]
查看完整版本: png+label闪烁[已解决]