zghwelcome 发表于 2023-5-5 08:21:30

【已解决】请教GDI绘制更新文字时如何避免闪烁?

本帖最后由 zghwelcome 于 2023-5-5 09:12 编辑

请教各位大佬, GDI绘制、更新文字时,如何避免闪烁?





#include <StaticConstants.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#AutoIt3Wrapper_UseX64 = n

HotKeySet('{esc}','_Exit');退出
$Form1 = GUICreate("Form1", @DesktopWidth, @DesktopHeight)
GUICtrlCreatePic(@ScriptDir & "\1.jpg", 0, 0, @DesktopWidth, @DesktopHeight, BitOR($WS_CLIPSIBLINGS, $SS_NOTIFY))
GUICtrlSetState(-1, 128)
GUISetBkColor(0)
GUISetState(@SW_SHOW)
Global $iTime = 200

_GDIPlus_Startup()
Global $hBrush, $hFormat, $hFamily, $hFont, $tLayout, $hGraphic
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($Form1)
$hBrush = _GDIPlus_BrushCreateSolid(0xFFFF0000)
$hFormat = _GDIPlus_StringFormatCreate()
$hFamily = _GDIPlus_FontFamilyCreate("Segoe UI")
$hFont = _GDIPlus_FontCreate($hFamily, 40, 1, 2)
$tLayout = _GDIPlus_RectFCreate(@DesktopWidth / 2, @DesktopHeight / 2, 100, 70)

AdlibRegister('_time', 1000)
While 1
      $nMsg = GUIGetMsg()
      Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        _Exit()

      EndSwitch
WEnd

Func _time()
      If $iTime < 0 Then
                AdlibUnRegister('_time')
      Else
                ;// 更新数字
                $iTime -= 1
                ConsoleWrite('$iTime: ' & $iTime & @CRLF)
                GUISetState(@SW_DISABLE, $Form1)
                _WinAPI_InvalidateRect($Form1)
                GUISetState(@SW_ENABLE, $Form1)
                _GDIPlus_GraphicsDrawStringEx($hGraphic, $iTime, $hFont, $tLayout, $hFormat, $hBrush)
      EndIf
EndFunc   ;==>_time
Func _Exit()
      _GDIPlus_FontDispose($hFont)
      _GDIPlus_FontFamilyDispose($hFamily)
      _GDIPlus_StringFormatDispose($hFormat)
      _GDIPlus_BrushDispose($hBrush)
      _GDIPlus_GraphicsDispose($hGraphic)
      _GDIPlus_Shutdown()
      Exit
EndFunc   ;==>_Exit




haijie1223 发表于 2023-5-5 09:02:04

如此,这样?
#NoTrayIcon
#include <StaticConstants.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#AutoIt3Wrapper_UseX64 = n

HotKeySet('{esc}', '_Exit');退出

$Form1 = GUICreate("Form1", @DesktopWidth, @DesktopHeight)
GUICtrlCreatePic('', 0, 0, @DesktopWidth, @DesktopHeight, BitOR($WS_CLIPSIBLINGS, $SS_NOTIFY))
GUICtrlSetState(-1, 128)
GUISetBkColor(0)
GUISetState(@SW_SHOW)
Global $iTime = 200

_GDIPlus_Startup()
Global $hBrush, $hFormat, $hFamily, $hFont, $tLayout, $hGraphic
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($Form1)

Global $hBitmap = _GDIPlus_BitmapCreateFromGraphics(@DesktopWidth, @DesktopHeight, $hGraphic)
Global $hBackBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)

Global $hImage = _GDIPlus_BitmapCreateFromFile(@ScriptDir & "\1.jpg")
_GDIPlus_GraphicsDrawImageRect($hBackBuffer, $hImage, 0, 0, @DesktopWidth, @DesktopHeight)
_GDIPlus_GraphicsDrawImage($hGraphic, $hBitmap, 0, 0)

$hBrush = _GDIPlus_BrushCreateSolid(0xFFFF0000)
$hFormat = _GDIPlus_StringFormatCreate()
$hFamily = _GDIPlus_FontFamilyCreate("Segoe UI")
$hFont = _GDIPlus_FontCreate($hFamily, 40, 1, 2)
$tLayout = _GDIPlus_RectFCreate(@DesktopWidth / 2, @DesktopHeight / 2, 100, 70)

AdlibRegister('_time', 1000)
While 1
      $nMsg = GUIGetMsg()
      Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        _Exit()

      EndSwitch
WEnd

Func _time()
      If $iTime < 0 Then
                AdlibUnRegister('_time')
      Else
                ;// 更新数字
                $iTime -= 1
                ConsoleWrite('$iTime: ' & $iTime & @CRLF)
                GUISetState(@SW_DISABLE, $Form1)
                _GDIPlus_GraphicsClear($hBackBuffer ,0xFF000000 )
                _GDIPlus_GraphicsDrawImageRect($hBackBuffer, $hImage, 0, 0, @DesktopWidth, @DesktopHeight)
                _GDIPlus_GraphicsDrawStringEx($hBackBuffer, $iTime, $hFont, $tLayout, $hFormat, $hBrush)
                _GDIPlus_GraphicsDrawImage($hGraphic, $hBitmap, 0, 0)
      EndIf
EndFunc   ;==>_time
Func _Exit()
      _GDIPlus_FontDispose($hFont)
      _GDIPlus_FontFamilyDispose($hFamily)
      _GDIPlus_StringFormatDispose($hFormat)
      _GDIPlus_BrushDispose($hBrush)
      _GDIPlus_GraphicsDispose($hGraphic)
      _GDIPlus_Shutdown()
      Exit
EndFunc   ;==>_Exit


yohoboy 发表于 2023-5-6 21:12:50

win 7- 64bit

autoit 3.3.14.2
經測試會有如圖狀況,數字影像重疊,在找原因中

haijie1223 发表于 2023-5-7 08:44:32

yohoboy 发表于 2023-5-6 21:12
win 7- 64bit

autoit 3.3.14.2


因为你没有1.jpg

haijie1223 发表于 2023-5-7 10:52:32

yohoboy 发表于 2023-5-6 21:12
win 7- 64bit

autoit 3.3.14.2


代码更新了,再试试吧

yohoboy 发表于 2023-5-7 12:23:35

抱歉挽回覆,目前測試正常,不管是否有背景圖作背景,還沒透徹代碼,感謝回覆。
页: [1]
查看完整版本: 【已解决】请教GDI绘制更新文字时如何避免闪烁?