Jump to content

text alignment in tooltip/msgbox


Go to solution Solved by pixelsearch,

Recommended Posts

Hi, I include 2 examples of what I have.. and what I want/need, I show the result in a tooltip ..

Long story short, the number before the word "replace" is different length (part number) and the number after "replace" can be another part number..

I want the 2nd, 3th line etc. to be align with the 1st line (1st letter of the part number after the replace) ... hard to explain so I have include a picture

 

And here what I have for now :

#include <String.au3>


;~ --------- Example #1 ---------

$List = ""
$gText = "987654"
$aRow = "1 2 3"

$Split = StringSplit($aRow, " ")

For $i = 1 To $Split[0]
    $List &= $Split[$i]

    $len = StringLen($gText & " replace ")

    If $i <> $Split[0] Then $List &= @CRLF & _StringRepeat(" ", $len)
Next

ToolTip($gText & " replace " & $List)
Sleep(5000)
ToolTip("")

;~ --------- Example #2 ---------

$List = ""
$gText = "5432"
$aRow = "789 456"

$Split = StringSplit($aRow, " ")

For $i = 1 To $Split[0]
    $List &= $Split[$i]

    $len = StringLen($gText & " replace ")

    If $i <> $Split[0] Then $List &= @CRLF & _StringRepeat(" ", $len)
Next

ToolTip($gText & " replace " & $List)
Sleep(5000)
ToolTip("")

The problem is the space is not the same width as a caracter so something it align perfectly but pure luck..

If someone have a idea :)

thank you

Sans titre.jpg

Edited by Resiak1811
Link to comment
Share on other sites

Link to comment
Share on other sites

#include <String.au3>
#include <FontConstants.au3>
#include <WinAPIGdi.au3>
#include <WinAPISysWin.au3>

;~ --------- Example #1 ---------

$List = ""
$gText = "987654"
$aRow = "1 2 3"

$Split = StringSplit($aRow, " ")

For $i = 1 To $Split[0]
    $List &= $Split[$i]

    $len = StringLen($gText & " replace ")

    If $i <> $Split[0] Then $List &= @CRLF & _StringRepeat(" ", $len)
Next

_ToolTip($gText & " replace " & $List)

;~ --------- Example #2 ---------

$List = ""
$gText = "5432"
$aRow = "789 456"

$Split = StringSplit($aRow, " ")

For $i = 1 To $Split[0]
    $List &= $Split[$i]

    $len = StringLen($gText & " replace ")

    If $i <> $Split[0] Then $List &= @CRLF & _StringRepeat(" ", $len)
Next

_ToolTip($gText & " replace " & $List)


Func _ToolTip($sText, $iX = Default, $iY = Default, $sTitle = Default, $iIcon = Default, $iOptions = Default, $iSeconds = 5)
    ;🏆 https://www.autoitscript.com/forum/topic/205225-tooltip-set-style-moved/#comment-1476250
    Local Static $hFont = _WinAPI_CreateFont(20, 0, 0, 0, 400, False, False, False, $DEFAULT_CHARSET, _
            $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, 0, "DejaVu Sans Mono")
    ToolTip($sText, -100, -100, $sTitle, $iIcon, $iOptions) ; hide it from view
    Local $hWnd = WinWait($sText)
    _WinAPI_SetFont($hWnd, $hFont)
    ToolTip($sText, $iX, $iY, $sTitle, $iIcon, $iOptions)
    For $i = 1 To $iSeconds
        Sleep(1000)
    Next
EndFunc   ;==>Example

 

Edited by ioa747

I know that I know nothing

Link to comment
Share on other sites

For tooltips, I would do it like this, using @Melba23 StringSize UDF (found in his signature)

#include "StringSize.au3" ; Melba23's

Local $aLine = ["987654 replace drfghdfhxdfghfgh 1", "2", "hello 34"]
Local $iW0, $iW, $sText = $aLine[0]

$iW0 = _StringSize($aLine[0], 9, 400, 0, "Tahoma")[2] ; [2] => width in pixels

For $i = 1 To Ubound($aLine) - 1
    While True
        $aLine[$i] = " " & $aLine[$i]
        $iW = _StringSize($aLine[$i], 9, 400, 0, "Tahoma")[2]
        If ($iW0 - $iW < 4) Or ($iW >= $iW0) Then ExitLoop
    WEnd
    $sText &= @LF & $aLine[$i]
Next

ToolTip($sText, Default, Default, Default, Default, 1) ; 1 = balloon
Sleep(5000)

34.png.d954d34c5e6ddcdc786f47298f045113.png

But this is only a part of the script, it may display differently depending on the OS, because to retrieve these 3 values...

9, 400, "Tahoma"

...I used a function from Yashied to retrieve the font attributes of a window based on its handle (a tooltip handle in this case)

 

Edited by pixelsearch
less lines in code
Link to comment
Share on other sites

again .. thanks to both of you ...

I try both solution and both are not working ...

but #1 : ioa747 currently I use a modified version of the StringSize script so I will test with the original file later...same prob. with the original file thanks

but #2 : pixelsearch the problem is probably the font attributes as you say

 

I though it was easier than this lol

thank you for your time

Sans titre2.jpg

Sans titre.jpg

Edited by Resiak1811
Link to comment
Share on other sites

I was aiming at the same solution as @ioa747.  The problem you have is probably because you do not own the DejaVu Sans Mono font.  Try with Courier or Terminal font.  It should work.

Link to comment
Share on other sites

3 hours ago, Resiak1811 said:

but #2 : pixelsearch the problem is probably the font attributes as you say

Yes I guessed it won't work correctly on other OS's, so...

To the people who commented on this thread (and other volunteers too !) could you please run the script below, which is a more complete version as it first retrieves the font attributes of a tooltip window on your OS. Is the final tooltip correctly right-justified now ?

Thanks & finger crossed :)

#include "StringSize.au3" ; Melba23's
#Include <FontConstants.au3>
#include <SendMessage.au3>
#include <WinAPIGdiDC.au3>
#Include <WindowsConstants.au3>

Opt("MustDeclareVars", 1) ;0=no, 1=require pre-declaration

Local $aToolText = ["987654 replace drfghdfhxdfghfgh 1", "2", "hello 34"]
Local $sToolText = _ToolTipRightJustify($aToolText)

ToolTip($sToolText, Default, Default, Default, Default, 1) ; 1 = balloon
Sleep(5000)

;==============================================
Func _ToolTipRightJustify($aToolText)

    ; Get the tooltip font attributes from a temporary hidden tooltip
    Local $sHiddenText, $hWnd, $aFont
    $sHiddenText = "Hidden tooltip to get its font"
    ToolTip($sHiddenText, -300, 0)
    $hWnd = WinGetHandle($sHiddenText)
    $aFont = _GetFont($hWnd)
    ToolTip("")

    ; Compute each tooltip line to have nearly the same number of pixels as the 1st tooltip line
    Local $iW0, $iW, $sLine, $sToolText = $aToolText[0]
    $iW0 = _StringSize($aToolText[0], $aFont[0], $aFont[1], 0, $aFont[3])[2] ; [2] => width in pixels
    For $i = 1 To Ubound($aToolText) - 1
        $sLine = $aToolText[$i]
        While True
            $sLine = " " & $sLine
            $iW = _StringSize($sLine, $aFont[0], $aFont[1], 0, $aFont[3])[2]
            If ($iW0 - $iW < 4) Or ($iW >= $iW0) Then ExitLoop
        WEnd
        $sToolText &= @LF & $sLine
    Next
    Return $sToolText
EndFunc   ;==>_ToolTipRightJustify

;==============================================
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[2] = BitOR(2 * (DllStructGetData($tFont, 'Italic') <> 0), 4 * (DllStructGetData($tFont, 'Underline') <> 0), 8 * (DllStructGetData($tFont, 'Strikeout') <> 0))
    $aFont[3] = DllStructGetData($tFont, 'FaceName')
    ; $aFont[4] = DllStructGetData($tFont, 'Quality')

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

34.png.d954d34c5e6ddcdc786f47298f045113.png

Edited by pixelsearch
typo
Link to comment
Share on other sites

  • Solution

With a small change in the script, you get this :

#include "StringSize.au3" ; Melba23's
#Include <FontConstants.au3>
#include <SendMessage.au3>
#include <WinAPIGdiDC.au3>
#Include <WindowsConstants.au3>

Opt("MustDeclareVars", 1) ;0=no, 1=require pre-declaration

Local $aToolText = ["987654 replace 17", "234", "5678", "9"]
Local $sToolText = _ToolTipJustify($aToolText, "replace")

ToolTip($sToolText, Default, Default, Default, Default, 1) ; 1 = balloon
Sleep(5000)

;==============================================
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, 0)
    $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 = " " & $sSpaces
        $iW = _StringSize($sSpaces, $aFont[0], $aFont[1], 0, $aFont[3])[2]
        If ($iW0 - $iW < 4) Or ($iW >= $iW0) Then ExitLoop
    WEnd
    For $i = 1 To Ubound($aToolText) - 1
        $sToolText &= @LF & $sSpaces & $aToolText[$i]
    Next
    Return $sToolText
EndFunc   ;==>_ToolTipJustify

;==============================================
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[2] = BitOR(2 * (DllStructGetData($tFont, 'Italic') <> 0), 4 * (DllStructGetData($tFont, 'Underline') <> 0), 8 * (DllStructGetData($tFont, 'Strikeout') <> 0))
    $aFont[3] = DllStructGetData($tFont, 'FaceName')
    ; $aFont[4] = DllStructGetData($tFont, 'Quality')

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

34d.png.2af41f2cb927bf1e8ee6828a61b29cfd.png

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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