Text von selbstgemaltem Progress mittig setzen

  • Hallo.
    Ich nutze seit geraumer Zeit eine Funktion eines Freundes um Progresse selber zu malen. Das ganze passiert mit GDI+. Die Progresse sehen auch gut aus, allerdings ist der Text auf den Progressen nicht mittig, sondern man bestimmt die Position durch eine X-Koordinate.

    Zeile 45-50 sind die, wo der Text geschrieben wird. Ich weiß aber nicht, wie ich da soetwas wie einen Centerstyle einbauen könnte.
    In dem Beispiel läuft der Text nach rechts, wo sich der Text befindet, wird durch den letzten Parameter in der Funktion _ProgressCreate angegeben.
    Hier die der Code:

    Spoiler anzeigen
    [autoit]

    #include <GDIPlus.au3>

    [/autoit] [autoit][/autoit] [autoit]

    $hGUI = GUICreate("Progress", 200, 30)
    $hGUIProgress = GUICtrlCreatePic("", 10, 10, 180, 17)

    [/autoit] [autoit][/autoit] [autoit]

    GUISetState()
    Sleep(1000)

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_Startup()

    [/autoit] [autoit][/autoit] [autoit]

    For $i = 1 To 100
    $hBitmap = _WinAPI_CreateBitmap(180, 17, 1, 32)
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
    _ProgressCreate($hGraphic, 0x6FFF0000, "Procedure: " & $i & "%", $i * 1.8, 180, $i * 0.85)
    $hNewBitmapHandle = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    _SetBitmapToCtrl($hGUIProgress, $hNewBitmapHandle)
    Sleep(50)
    Next

    [/autoit] [autoit][/autoit] [autoit]

    Sleep(2000)
    _GDIPlus_Shutdown()

    [/autoit] [autoit][/autoit] [autoit]

    ;Creates progresses
    Func _ProgressCreate($hGraphic, $vColor, $vText, $vPercent, $vWidth, $vTextWidth)
    _GDIPlus_GraphicsClear($hGraphic, 0xFFFFFFFF)

    [/autoit] [autoit][/autoit] [autoit]

    Dim $hPenProgress[99]
    For $i = 0 To 8
    $vSWColor = "0xFF" & Hex($i * 2 * 10, 2) & Hex($i *2* 10, 2) & Hex($i * 2 * 10, 2)
    $hPenProgress[$i] = _GDIPlus_PenCreate($vSWColor)
    _GDIPlus_GraphicsDrawLine($hGraphic, 0, $i, $vWidth, $i, $hPenProgress[$i])
    _GDIPlus_PenDispose($hPenProgress[$i])
    Next
    For $i = 0 To 7
    $vSWColor = "0xFF" & Hex($i*2 * 10, 2) & Hex($i *2* 10, 2) & Hex($i *2* 10, 2)
    $hPenProgress[$i] = _GDIPlus_PenCreate($vSWColor)
    _GDIPlus_GraphicsDrawLine($hGraphic, 0, 16 - $i, $vWidth, 16 - $i, $hPenProgress[$i])
    _GDIPlus_PenDispose($hPenProgress[$i])
    Next

    $hBrushProgress = _GDIPlus_BrushCreateSolid($vColor)
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 0, $vPercent, 17, $hBrushProgress)

    $hFontFamilyProgress = _GDIPlus_FontFamilyCreate("Arial")
    $hFontProgress = _GDIPlus_FontCreate($hFontFamilyProgress, 8, 1)
    $hLayoutProgress = _GDIPlus_RectFCreate($vTextWidth, 1, $vWidth, 17)
    $hFormatProgress = _GDIPlus_StringFormatCreate()
    $hBrushTextProgress = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
    _GDIPlus_GraphicsDrawStringEx($hGraphic, $vText, $hFontProgress, $hLayoutProgress, $hFormatProgress, $hBrushTextProgress)

    $hPenOutLineTL = _GDIPlus_PenCreate(0xFF666666)
    $hPenOutLineBR = _GDIPlus_PenCreate(0xFFDDDDDD)
    _GDIPlus_GraphicsDrawLine($hGraphic, 0, 0, $vWidth, 0, $hPenOutLineTL)
    _GDIPlus_GraphicsDrawLine($hGraphic, 0, 0, 0, 16, $hPenOutLineTL)
    _GDIPlus_GraphicsDrawLine($hGraphic, 0, 16, $vWidth, 16, $hPenOutLineBR)
    _GDIPlus_GraphicsDrawLine($hGraphic, $vWidth - 1, 0, $vWidth - 1, 16, $hPenOutLineBR)

    _GDIPlus_PenDispose($hPenOutLineTL)
    _GDIPlus_PenDispose($hPenOutLineBR)
    _GDIPlus_BrushDispose($hBrushProgress)
    _GDIPlus_FontFamilyDispose($hFontFamilyProgress)
    _GDIPlus_FontDispose($hFontProgress)
    _GDIPlus_StringFormatDispose($hFormatProgress)
    _GDIPlus_BrushDispose($hBrushTextProgress)
    EndFunc ;==>_SM_ProgressCreate

    [/autoit] [autoit][/autoit] [autoit]

    ;Set a bitmap to a control - by Zedna
    Func _SetBitmapToCtrl($vCtrlID, $h_Bitmap)
    Local $hWnd = GUICtrlGetHandle($vCtrlID)
    If $hWnd = 0 Then Return SetError(1, 0, 0)

    [/autoit] [autoit][/autoit] [autoit]

    Local $vOldStyle = _WinAPI_GetWindowLong($hWnd, -16)
    _WinAPI_SetWindowLong($hWnd, -16, BitOR($vOldStyle, 0xE))

    [/autoit] [autoit][/autoit] [autoit]

    Local $vOldBmp = _SendMessage($hWnd, 0x0172, 0, $h_Bitmap)
    If $vOldBmp <> 0 Then _WinAPI_DeleteObject($vOldBmp)
    EndFunc ;==>_SM_SetBitmapToCtrl

    [/autoit]

    Wenn ihr Ideen habt, wie ich das bei mir einbauen könnte oder anders machen kann, dann her damit =)

    MfG NoName

    Einmal editiert, zuletzt von NoName (21. März 2010 um 18:14)

  • Hallo NoName,

    du musst die Zeile 47 wie folgt ändern:

    [autoit]

    $hLayoutProgress = _GDIPlus_RectFCreate(int(($vWidth-$vTextWidth)/2), 1, $vWidth, 17)

    [/autoit]

    und beim Aufruf übergibst du immer den gleichen Wert z.B. 80 also so:

    [autoit]

    _ProgressCreate($hGraphic, 0x6FFF0000, "Procedure: " & $i & "%", $i * 1.8, 180, 80)

    [/autoit]

    idealerweise erstellst du eine Formel für diesen Wert, fällt mir aber (noch) kein Ansatz dafür ein,

    mfg (Auto)Bert

  • Also grundsätzlich geht das schon, nur das Problem ist, dass der text, der auf den Progress geschrieben wird, unterschiedlich lang ist. Also der Text kann 15 oder auch 5 Zeichen lang sein, und soll immer mittig sein.

  • Das wäre natürlich ideal, denn ich aufgrund der unterschiedlichen Zeichenlängen wusste ich nicht, wie ich das anordnen könnte. Ich mache mich mal auf die Suche.

  • Hallo NoName,

    ich habe die func aus textmeter etwas abgeändert eingebaut (sollte passen):

    Spoiler anzeigen
    [autoit]

    #include <GDIPlus.au3>
    #include <array.au3>

    [/autoit] [autoit][/autoit] [autoit]

    $hGUI = GUICreate("Progress", 200, 30)
    $hGUIProgress = GUICtrlCreatePic("", 10, 10, 180, 17)

    [/autoit] [autoit][/autoit] [autoit]

    GUISetState()
    Sleep(1000)

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_Startup()

    [/autoit] [autoit][/autoit] [autoit]

    For $i = 1 To 100
    $hBitmap = _WinAPI_CreateBitmap(180, 17, 1, 32)
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
    $sText = "Procedure: " & $i & " % von 100 fertig"
    $vTextWidth = Measure($hGUI,$sText)
    _ProgressCreate($hGraphic, 0x6FFF0000, $sText, $i * 1.8, 180, $vTextWidth)
    $hNewBitmapHandle = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    _SetBitmapToCtrl($hGUIProgress, $hNewBitmapHandle)
    Sleep(50)
    Next

    [/autoit] [autoit][/autoit] [autoit]

    Sleep(2000)
    _GDIPlus_Shutdown()

    [/autoit] [autoit][/autoit] [autoit]

    ;Creates progresses
    Func _ProgressCreate($hGraphic, $vColor, $vText, $vPercent, $vWidth, $vTextWidth)
    _GDIPlus_GraphicsClear($hGraphic, 0xFFFFFFFF)

    [/autoit] [autoit][/autoit] [autoit]

    Dim $hPenProgress[99]
    For $i = 0 To 8
    $vSWColor = "0xFF" & Hex($i * 2 * 10, 2) & Hex($i *2* 10, 2) & Hex($i * 2 * 10, 2)
    $hPenProgress[$i] = _GDIPlus_PenCreate($vSWColor)
    _GDIPlus_GraphicsDrawLine($hGraphic, 0, $i, $vWidth, $i, $hPenProgress[$i])
    _GDIPlus_PenDispose($hPenProgress[$i])
    Next
    For $i = 0 To 7
    $vSWColor = "0xFF" & Hex($i*2 * 10, 2) & Hex($i *2* 10, 2) & Hex($i *2* 10, 2)
    $hPenProgress[$i] = _GDIPlus_PenCreate($vSWColor)
    _GDIPlus_GraphicsDrawLine($hGraphic, 0, 16 - $i, $vWidth, 16 - $i, $hPenProgress[$i])
    _GDIPlus_PenDispose($hPenProgress[$i])
    Next

    [/autoit] [autoit][/autoit] [autoit]

    $hBrushProgress = _GDIPlus_BrushCreateSolid($vColor)
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 0, $vPercent, 17, $hBrushProgress)

    [/autoit] [autoit][/autoit] [autoit]

    $hFontFamilyProgress = _GDIPlus_FontFamilyCreate("Arial")
    $hFontProgress = _GDIPlus_FontCreate($hFontFamilyProgress, 8, 1)
    ; $hLayoutProgress = _GDIPlus_RectFCreate($vTextWidth, 1, $vWidth, 17)
    $hLayoutProgress = _GDIPlus_RectFCreate(int(($vWidth-$vTextWidth)/2), 1, $vWidth, 17)
    $hFormatProgress = _GDIPlus_StringFormatCreate()
    $hBrushTextProgress = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
    _GDIPlus_GraphicsDrawStringEx($hGraphic, $vText, $hFontProgress, $hLayoutProgress, $hFormatProgress, $hBrushTextProgress)

    [/autoit] [autoit][/autoit] [autoit]

    $hPenOutLineTL = _GDIPlus_PenCreate(0xFF666666)
    $hPenOutLineBR = _GDIPlus_PenCreate(0xFFDDDDDD)
    _GDIPlus_GraphicsDrawLine($hGraphic, 0, 0, $vWidth, 0, $hPenOutLineTL)
    _GDIPlus_GraphicsDrawLine($hGraphic, 0, 0, 0, 16, $hPenOutLineTL)
    _GDIPlus_GraphicsDrawLine($hGraphic, 0, 16, $vWidth, 16, $hPenOutLineBR)
    _GDIPlus_GraphicsDrawLine($hGraphic, $vWidth - 1, 0, $vWidth - 1, 16, $hPenOutLineBR)

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_PenDispose($hPenOutLineTL)
    _GDIPlus_PenDispose($hPenOutLineBR)
    _GDIPlus_BrushDispose($hBrushProgress)
    _GDIPlus_FontFamilyDispose($hFontFamilyProgress)
    _GDIPlus_FontDispose($hFontProgress)
    _GDIPlus_StringFormatDispose($hFormatProgress)
    _GDIPlus_BrushDispose($hBrushTextProgress)
    EndFunc ;==>_SM_ProgressCreate

    [/autoit] [autoit][/autoit] [autoit]

    ;Set a bitmap to a control - by Zedna
    Func _SetBitmapToCtrl($vCtrlID, $h_Bitmap)
    Local $hWnd = GUICtrlGetHandle($vCtrlID)
    If $hWnd = 0 Then Return SetError(1, 0, 0)

    [/autoit] [autoit][/autoit] [autoit]

    Local $vOldStyle = _WinAPI_GetWindowLong($hWnd, -16)
    _WinAPI_SetWindowLong($hWnd, -16, BitOR($vOldStyle, 0xE))

    [/autoit] [autoit][/autoit] [autoit]

    Local $vOldBmp = _SendMessage($hWnd, 0x0172, 0, $h_Bitmap)
    If $vOldBmp <> 0 Then _WinAPI_DeleteObject($vOldBmp)
    EndFunc ;==>_SM_SetBitmapToCtrl

    [/autoit] [autoit][/autoit] [autoit]

    Func Measure($gui,$nText) ;Original von BugFix http://www.autoit.de/index.php?page…5795#post105795
    If $nText = '' Then Return
    ;_GDIPlus_Startup()
    Local $hFormat = _GDIPlus_StringFormatCreate(0)
    Local $hFamily = _GDIPlus_FontFamilyCreate("Arial"); $aFont[2])
    Local $hFont = _GDIPlus_FontCreate($hFamily, 10, 0, 3)
    ; Local $hFont = _GDIPlus_FontCreate($hFamily, $aFont[3], $aFont[1], 3)
    Local $tLayout = _GDIPlus_RectFCreate(15, 171, 0, 0)
    Local $hGraphic = _GDIPlus_GraphicsCreateFromHWND($gui)
    Local $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $nText, $hFont, $tLayout, $hFormat)
    ;_ArrayDisplay($aInfo)
    Local $iWidth = Ceiling(DllStructGetData($aInfo[0], "Width"))
    Local $iHeight = Ceiling(DllStructGetData($aInfo[0], "Height"))
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_GraphicsDispose($hGraphic)
    ;_GDIPlus_ShutDown()
    Return $iWidth *0.85 ;Rückgabe mit Faktor (Urspruungsskript NoName $vTextWidth)
    EndFunc

    [/autoit]

    mfg (Auto)Bert

  • Ich danke dir^^
    Jetzt hast du mir ja schon alles abgenommen. Musste aber noch eine Kleinigkeit ändern.

    Danke für deine Hilfe!

    MfG
    NoName

  • 1. Zeile 102: Das * 0.85 war nur dazu da, beim ersten Beispiel, damit der Text nicht nach rechts über den Rand geht, kann also restlos gestrichen werden.
    2. Zeile 89:
    Local $hFont = _GDIPlus_FontCreate($hFamily, 10, 0, 3)
    zu
    Local $hFont = _GDIPlus_FontCreate($hFamily, 8, 1, 3) ;Schriftgröße 8, 1 = Bold

    3. Zeile 51:
    $hLayoutProgress = _GDIPlus_RectFCreate(int(($vWidth-$vTextWidth)/2), 1, $vWidth, 17)
    zu
    $hLayoutProgress = _GDIPlus_RectFCreate($vWidth / 2 - $vTextWidth / 2), 1, $vWidth, 17)

    und den Displaytext habe ich noch wieder geändert.

    [autoit]

    #include <GDIPlus.au3>
    #include <array.au3>

    [/autoit][autoit][/autoit][autoit]

    $hGUI = GUICreate("Progress", 200, 30)
    $hGUIProgress = GUICtrlCreatePic("", 10, 10, 180, 17)

    [/autoit][autoit][/autoit][autoit]

    GUISetState()
    Sleep(1000)

    [/autoit][autoit][/autoit][autoit]

    _GDIPlus_Startup()

    [/autoit][autoit][/autoit][autoit]

    For $i = 1 To 100
    $hBitmap = _WinAPI_CreateBitmap(180, 17, 1, 32)
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
    $sText = "Procedure: " & $i & " %"
    $vTextWidth = Measure($hGUI,$sText)
    _ProgressCreate($hGraphic, 0x6FFF0000, $sText, $i * 1.8, 180, $vTextWidth)
    $hNewBitmapHandle = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    _SetBitmapToCtrl($hGUIProgress, $hNewBitmapHandle)
    Sleep(50)
    Next

    [/autoit][autoit][/autoit][autoit]

    Sleep(2000)
    _GDIPlus_Shutdown()

    [/autoit][autoit][/autoit][autoit]

    ;Creates progresses
    Func _ProgressCreate($hGraphic, $vColor, $vText, $vPercent, $vWidth, $vTextWidth)
    _GDIPlus_GraphicsClear($hGraphic, 0xFFFFFFFF)

    [/autoit][autoit][/autoit][autoit]

    Dim $hPenProgress[99]
    For $i = 0 To 8
    $vSWColor = "0xFF" & Hex($i * 2 * 10, 2) & Hex($i *2* 10, 2) & Hex($i * 2 * 10, 2)
    $hPenProgress[$i] = _GDIPlus_PenCreate($vSWColor)
    _GDIPlus_GraphicsDrawLine($hGraphic, 0, $i, $vWidth, $i, $hPenProgress[$i])
    _GDIPlus_PenDispose($hPenProgress[$i])
    Next
    For $i = 0 To 7
    $vSWColor = "0xFF" & Hex($i*2 * 10, 2) & Hex($i *2* 10, 2) & Hex($i *2* 10, 2)
    $hPenProgress[$i] = _GDIPlus_PenCreate($vSWColor)
    _GDIPlus_GraphicsDrawLine($hGraphic, 0, 16 - $i, $vWidth, 16 - $i, $hPenProgress[$i])
    _GDIPlus_PenDispose($hPenProgress[$i])
    Next

    [/autoit][autoit][/autoit][autoit]

    $hBrushProgress = _GDIPlus_BrushCreateSolid($vColor)
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 0, $vPercent, 17, $hBrushProgress)

    [/autoit][autoit][/autoit][autoit]

    $hFontFamilyProgress = _GDIPlus_FontFamilyCreate("Arial")
    $hFontProgress = _GDIPlus_FontCreate($hFontFamilyProgress, 8, 1)
    ; $hLayoutProgress = _GDIPlus_RectFCreate($vTextWidth, 1, $vWidth, 17)
    $hLayoutProgress = _GDIPlus_RectFCreate($vWidth / 2 - $vTextWidth / 2, 1, $vWidth, 17)
    $hFormatProgress = _GDIPlus_StringFormatCreate()
    $hBrushTextProgress = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
    _GDIPlus_GraphicsDrawStringEx($hGraphic, $vText, $hFontProgress, $hLayoutProgress, $hFormatProgress, $hBrushTextProgress)

    [/autoit][autoit][/autoit][autoit]

    $hPenOutLineTL = _GDIPlus_PenCreate(0xFF666666)
    $hPenOutLineBR = _GDIPlus_PenCreate(0xFFDDDDDD)
    _GDIPlus_GraphicsDrawLine($hGraphic, 0, 0, $vWidth, 0, $hPenOutLineTL)
    _GDIPlus_GraphicsDrawLine($hGraphic, 0, 0, 0, 16, $hPenOutLineTL)
    _GDIPlus_GraphicsDrawLine($hGraphic, 0, 16, $vWidth, 16, $hPenOutLineBR)
    _GDIPlus_GraphicsDrawLine($hGraphic, $vWidth - 1, 0, $vWidth - 1, 16, $hPenOutLineBR)

    [/autoit][autoit][/autoit][autoit]

    _GDIPlus_PenDispose($hPenOutLineTL)
    _GDIPlus_PenDispose($hPenOutLineBR)
    _GDIPlus_BrushDispose($hBrushProgress)
    _GDIPlus_FontFamilyDispose($hFontFamilyProgress)
    _GDIPlus_FontDispose($hFontProgress)
    _GDIPlus_StringFormatDispose($hFormatProgress)
    _GDIPlus_BrushDispose($hBrushTextProgress)
    EndFunc ;==>_SM_ProgressCreate

    [/autoit][autoit][/autoit][autoit]

    ;Set a bitmap to a control - by Zedna
    Func _SetBitmapToCtrl($vCtrlID, $h_Bitmap)
    Local $hWnd = GUICtrlGetHandle($vCtrlID)
    If $hWnd = 0 Then Return SetError(1, 0, 0)

    [/autoit][autoit][/autoit][autoit]

    Local $vOldStyle = _WinAPI_GetWindowLong($hWnd, -16)
    _WinAPI_SetWindowLong($hWnd, -16, BitOR($vOldStyle, 0xE))

    [/autoit][autoit][/autoit][autoit]

    Local $vOldBmp = _SendMessage($hWnd, 0x0172, 0, $h_Bitmap)
    If $vOldBmp <> 0 Then _WinAPI_DeleteObject($vOldBmp)
    EndFunc ;==>_SM_SetBitmapToCtrl

    [/autoit][autoit][/autoit][autoit]

    Func Measure($gui,$nText) ;Original von BugFix http://www.autoit.de/index.php?page…5795#post105795
    If $nText = '' Then Return
    ;_GDIPlus_Startup()
    Local $hFormat = _GDIPlus_StringFormatCreate(0)
    Local $hFamily = _GDIPlus_FontFamilyCreate("Arial"); $aFont[2])
    Local $hFont = _GDIPlus_FontCreate($hFamily, 10, 0, 3)
    ; Local $hFont = _GDIPlus_FontCreate($hFamily, $aFont[3], $aFont[1], 3)
    Local $tLayout = _GDIPlus_RectFCreate(15, 171, 0, 0)
    Local $hGraphic = _GDIPlus_GraphicsCreateFromHWND($gui)
    Local $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $nText, $hFont, $tLayout, $hFormat)
    ;_ArrayDisplay($aInfo)
    Local $iWidth = Ceiling(DllStructGetData($aInfo[0], "Width"))
    Local $iHeight = Ceiling(DllStructGetData($aInfo[0], "Height"))
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_GraphicsDispose($hGraphic)
    ;_GDIPlus_ShutDown()
    Return $iWidth ;Rückgabe mit Faktor (Urspruungsskript NoName $vTextWidth)
    EndFunc

    [/autoit]

    Text ist zwar nicht ganz mittig, aber das passt schon super so.