Jump to content

Recommended Posts

Posted (edited)

Hi,

This post is related to this post : text alignment in tooltip/msgbox

This function (_ToolTipJustify) work pretty well (thank again @pixelsearch ) but now I try to get it work with GUICtrlCreateLabel

 

#include <FontConstants.au3>
#include <GUIConstantsEx.au3>
#include <SendMessage.au3>
#include <StringConstants.au3>
#include "StringSize.au3" ; Melba23
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Local $hGUI = GUICreate("Example", 500, 450)

Local $iWriteB4 = GUICtrlCreateButton("Write Log b4", 60, 415, 85, 25)
Local $iWriteAfter = GUICtrlCreateButton("Write Log After", 210, 415, 85, 25)
Local $iOK = GUICtrlCreateButton("OK", 370, 415, 85, 25)

GUICtrlCreateLabel("Last Modif.:", 15, 10, 245, 65)
$LastMod = GUICtrlCreateLabel("", 90, 10, 370, 170)

GUICtrlCreateLabel("Last Modif.(2):", 15, 210, 245, 65)
$LastMod2 = GUICtrlCreateLabel("", 90, 210, 370, 170)


GUISetState(@SW_SHOW, $hGUI)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $iOK
            ExitLoop

        Case $iWriteB4
            UpdateRow("Qty", "12345678", "9", "10")
            UpdateRow("Qty", "DC12345678", "99", "100")
            UpdateRow("Qty", "DC_987_DC", "2999", "3000")

        Case $iWriteAfter
            UpdateRow2("Qty", "12345678", "9", "10")
            UpdateRow2("Qty", "DC12345678", "99", "100")
            UpdateRow2("Qty", "DC_987_DC", "2999", "3000")

    EndSwitch
WEnd
GUIDelete($hGUI)



Func _GetFont($hWnd)
    Local $hDC, $hFont, $tFont, $aFont[5]

    $hDC = _WinAPI_GetDC($hWnd)
    $hFont = _SendMessage($hWnd, $WM_GETFONT)
    $tFont = DllStructCreate($tagLOGFONT)

    _WinAPI_GetObject($hFont, DllStructGetSize($tFont), DllStructGetPtr($tFont))
    $aFont[0] = -Round(DllStructGetData($tFont, 'Height') / _WinAPI_GetDeviceCaps($hDC, $LOGPIXELSY) * 72, 1)
    $aFont[1] = DllStructGetData($tFont, 'Weight')
    $aFont[3] = DllStructGetData($tFont, 'FaceName')

    _WinAPI_ReleaseDC($hWnd, $hDC)
    Return $aFont
EndFunc   ;==>_GetFont

Func _GUICtrlSetData($iControlID, $iControlData)
    If GUICtrlRead($iControlID) <> $iControlData Then GUICtrlSetData($iControlID, $iControlData)
EndFunc   ;==>_GUICtrlSetData

Func _ToolTipJustify($aToolText, $sAlignAfterThis)
    ; Get the tooltip font attributes from a temporary hidden tooltip
    Local $sHiddenText, $hWnd, $aFont
    $sHiddenText = "Hidden Tooltip to get its Font"

    ToolTip($sHiddenText, -300, -300)
    $hWnd = WinGetHandle($sHiddenText)
    $aFont = _GetFont($hWnd)
    ToolTip("")

    Local $iPos, $sLeft, $iW0, $iW, $sSpaces, $sToolText = $aToolText[0]
    $iPos = StringInStr($aToolText[0], $sAlignAfterThis) + StringLen($sAlignAfterThis)
    $sLeft = StringLeft($aToolText[0], $iPos)
    $iW0 = _StringSize($sLeft, $aFont[0], $aFont[1], 0, $aFont[3])[2] ; [2] => width in pixels

    While True
        $sSpaces &= " "
        $iW = _StringSize($sSpaces, $aFont[0], $aFont[1], 0, $aFont[3])[2]
        If ($iW0 - $iW < 1) Or ($iW >= $iW0) Then ExitLoop
    WEnd

    For $i = 1 To UBound($aToolText) - 1
        $sToolText &= @LF & $sSpaces & $aToolText[$i]
    Next

    Return $sToolText
EndFunc   ;==>_ToolTipJustify

Func UpdateLogs()
    $Logs = GUICtrlRead($LastMod)
    $LogsSplit = StringSplit($Logs, @CRLF, $STR_ENTIRESPLIT)

    Local $LogsSp
    For $i = 1 To $LogsSplit[0]
        If $i > 12 Then ExitLoop
        $LogsSp &= $LogsSplit[$i] & @CRLF
    Next

    _GUICtrlSetData($LastMod, $LogsSp)
EndFunc   ;==>UpdateLogs

Func UpdateRow($var, $var1, $var2, $var3)
    $LastModLine = GUICtrlRead($LastMod)
    $LastModLine = "[" & @HOUR & ":" & @MIN & "] Changed from : Item (" & $var1 & ") : " & $var & " : " & $var2 & @CRLF _
             & "                               to : Item (" & $var1 & ") : " & $var & " : " & $var3 & @CRLF & $LastModLine

    _GUICtrlSetData($LastMod, $LastModLine)
    UpdateLogs() ;~ limit logs at max 12 entry
EndFunc   ;==>UpdateRow

Func UpdateRow2($var, $var1, $var2, $var3)
    Local $aArray_Base[2] = ["[" & @HOUR & ":" & @MIN & "] Item (" & $var1 & ") : " & $var & " : changed from : " & $var2, "to : " & $var3], $aArray = $aArray_Base
    $LastModLine = _ToolTipJustify($aArray, "changed")

    _GUICtrlSetData($LastMod2, $LastModLine & @CRLF & GUICtrlRead($LastMod2))
    UpdateLogs() ;~ limit logs at max 12 entry
EndFunc   ;==>UpdateRow2

 

Before (in UpdateRow func.) I use space to align text but since @pixelsearch find a way to "text alignment" in a tooltip..

I try to get it work with GUICtrlCreateLabel..

 

I tried many things but without success :(

any idea ? thank you for your time.

(I have attached a picture of what I hope .. the image on the right is what I hope)

test.jpg

Edited by Resiak1811
Posted

 

#include <GUIConstantsEx.au3>

Global $hGUI = GUICreate("Example", 600, 250)

Global $iWriteB4 = GUICtrlCreateButton("Write Log b4", 60, 215, 85, 25)
Global $iOK = GUICtrlCreateButton("OK", 370, 215, 85, 25)

GUICtrlCreateLabel("Last Modif.:", 15, 10, 245, 65)
Global $LastMod = GUICtrlCreateLabel("", 90, 10, 500, 170)
GUICtrlSetFont(-1, 11, 400, 0, "DejaVu Sans Mono")

GUISetState(@SW_SHOW, $hGUI)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $iOK
            ExitLoop

        Case $iWriteB4
            UpdateRow("Qty", "12345678", "9", "10")
            UpdateRow("Qty", "DC12345678", "99", "100")
            UpdateRow("Qty", "DC_987_DC", "2999", "3000")

    EndSwitch
WEnd
GUIDelete($hGUI)

Func UpdateRow($var, $var1, $var2, $var3)
    Local $LastModLine = GUICtrlRead($LastMod)
    Local $sEntry
    $sEntry =  StringFormat("[%02i:%02i] Changed from : Item %12s : %3s : %5s\n", @HOUR, @MIN, "(" & $var1 & ")", $var, $var2)
    $sEntry &= StringFormat("                  to : Item %12s : %3s : %5s\n", "(" & $var1 & ")", $var, $var3)
    $sEntry &= $LastModLine
    GUICtrlSetData($LastMod, $sEntry)
EndFunc   ;==>UpdateRow

 

I know that I know nothing

  • Solution
Posted (edited)

@Resiak1811 hi
To achieve your goal, you need a few modifications in your script :

1) For a better accurate alignment :

; $LastModLine = _ToolTipJustify($aArray, "changed")
$LastModLine = _ToolTipJustify($aArray, "changed fr")

2) As you're gonna update a label control, then you shouldn't retrieve the font from a Tooltip control, they don't have the same font height (on my computer, 8.3 vs 9)

; ToolTip($sHiddenText, -300, -300)
; $hWnd = WinGetHandle($sHiddenText)

Local $idHiddenLabel = GUICtrlCreateLabel($sHiddenText, -300, -300)
$hWnd = GUICtrlGetHandle($idHiddenLabel)

 

; ToolTip("")
GUICtrlDelete($idHiddenLabel)

Result :

Justifyinlabel.png.dca66c610460d166b1607671af69d1ec.png

 

Edited by pixelsearch
typo

"I think you are searching a bug where there is no bug... don't listen to bad advice."

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...