- Offizieller Beitrag
Am Ende deines Code werden viele Sachen freigegeben. Wenn ich das in meinem Programm übernehme, muss ich dann am Ende des Programms noch etwas freigeben, oder ist schon alles freigegeben?
Nee, ist alles schon wieder freigegeben. Wird nur temporär gebraucht.
Die TABs musst Du natürlich selbst festlegen. Wenn z.B. ein TAB vier Leerzeichen entsprechen soll, dann musst Du die entsprechend ersetzen:
AutoIt
#include <AutoItConstants.au3>
#include <GDIPlus.au3>
#include <SendMessage.au3>
#include <StaticConstants.au3>
#include <WinAPIHObj.au3>
#include <WinAPIInternals.au3>
#include <WinAPISysInternals.au3>
#include <GUIConstantsEx.au3> ; Für $GUI_EVENT_CLOSE.
Example1()
Func Example1()
Local $hGui = GUICreate("My GUI", 400, 800)
Local $iTop = 0
; Labels mit verschiedener Schriftgöße erstellen.
For $i = 4 To 27
$iTop += 15 + $i
GUICtrlCreateLabel("fontsize " & $i, 10, $iTop, 0, 0, $SS_BITMAP) ; Wichtig! $SS_BITMAP als Style
_GUICtrlSetFont(-1, "fontsize" & $i, $i, 0, "Courier New", 0xFF00007F)
GUICtrlSetBkColor(-1, 0xCCCCFF) ; <== Um zu erkennen, wenn sich die Größe des Labels ändert.
Next
GUISetState(@SW_SHOW)
Local $idBtn_1 = GUICtrlCreateButton("Change Label 7 Text", 200, 130, 120)
Local $sNewText = " This is a sample text to fill the label."
While 1
Switch GUIGetMsg()
Case $idBtn_1
$sNewText = StringReplace($sNewText, @TAB, ' ') ; TABs in 4 Leerzeichen umwandeln
_GUICtrlSetFont(7, $sNewText, 8, 0, "Courier New", 0xFF00007F)
GUICtrlSetBkColor(7, 0xCCCCAA) ; <== Nur um zu sehen, ob das hier einen Einfluss hat.
Case $GUI_EVENT_CLOSE
ExitLoop
EndSwitch
WEnd
EndFunc ;==>Example1
Func _GUICtrlSetFont($idCtrl, $sString, $iSize, $iStyle, $sFontname, $iColor = 0xFF000000)
Local $hWnd, $hBitmap, $hGfxCtx, $hBrush, $hFormat, $hFamily, $hFont, $tLayout, $aInfo
Local $hBMP, $hPrevImage
_GDIPlus_Startup()
If $idCtrl = Default Or $idCtrl <= 0 Then $idCtrl = _WinAPI_GetDlgCtrlID(GUICtrlGetHandle($idCtrl))
If $iStyle = Default Or $iStyle <= 0 Then $iStyle = 0
$hWnd = GUICtrlGetHandle($idCtrl)
If Not IsHWnd($hWnd) Then Return SetError(1)
Local $iW = 1000, $iH = 100
$hBitmap = _GDIPlus_BitmapCreateFromScan0($iW, $iH)
$hGfxCtx = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsSetTextRenderingHint($hGfxCtx, 4)
$hBrush = _GDIPlus_BrushCreateSolid($iColor)
$hFormat = _GDIPlus_StringFormatCreate()
$hFamily = _GDIPlus_FontFamilyCreate($sFontname)
$hFont = _GDIPlus_FontCreate($hFamily, $iSize, $iStyle)
$tLayout = _GDIPlus_RectFCreate(0, 0, $iW, $iH)
$aInfo = _GDIPlus_GraphicsMeasureString($hGfxCtx, $sString, $hFont, $tLayout, $hFormat)
_GDIPlus_GraphicsDispose($hGfxCtx)
_GDIPlus_BitmapDispose($hBitmap)
$iW = Ceiling($aInfo[0].Width)
$iH = Ceiling($aInfo[0].Height)
;~ ConsoleWrite(StringFormat('Width x Height = %i x %i\r\n', $iW, $iH))
$hBitmap = _GDIPlus_BitmapCreateFromScan0($iW, $iH)
$hGfxCtx = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsSetTextRenderingHint($hGfxCtx, 4)
$tLayout = _GDIPlus_RectFCreate(0, 0, $iW, $iH)
$aInfo = _GDIPlus_GraphicsMeasureString($hGfxCtx, $sString, $hFont, $tLayout, $hFormat)
_GDIPlus_GraphicsDrawStringEx($hGfxCtx, $sString, $hFont, $aInfo[0], $hFormat, $hBrush)
$hBMP = _GDIPlus_BitmapCreateDIBFromBitmap($hBitmap)
_WinAPI_SetWindowPos($hWnd, $HWND_NOTOPMOST, 0, 0, $iW, $iH, BitOR($SWP_FRAMECHANGED, $SWP_NOMOVE))
$hPrevImage = _SendMessage($hWnd, $STM_SETIMAGE, $IMAGE_BITMAP, $hBMP)
If $hPrevImage Then _WinAPI_DeleteObject($hPrevImage)
_GDIPlus_FontDispose($hFont)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_BrushDispose($hBrush)
_GDIPlus_GraphicsDispose($hGfxCtx)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_Shutdown()
EndFunc ;==>_GUICtrlSetFont
Alles anzeigen