Formatting a multi-line Tooltip to align like columns ? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
DRocks
Posts: 545
Joined: 08 May 2018, 10:20

Formatting a multi-line Tooltip to align like columns ?

28 Mar 2019, 13:26

Hello guys,


I have read a few times about the Format function in AHK but I can't practically make it to use and here's a case where I need to understand it better.
I'm trying to display a clean multi-line tooltip that shows some calculations.
The alignement of a invisible column is really important to make it consistent and look good.
Now all I have to solve the problem is guessing how many spaces it needs to display aligned.

Would someone please provide me with an example of how to properly format this kind of multi-line tooltip please ?
I'd really appreciate it.

Here's my example code that should run and display what I currently got:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance, Force ; Prevent double execution of this script
#Persistent

; Faking what are normally dynamic variables (changing with different calculations)
OverRowNumber := 1
, Qt_1 := 2
, Mt_1 := 100
, RawTs_1 := 4.35
, RawTq_1 := 8.68

; Prepare the values to display in the tooltip 
, Ligne_Quantite	 := Qt_%OverRowNumber%
, Ligne_PrixUnitaire := Mt_%OverRowNumber%
, Ligne_TPSUnitaire  := RawTs_%OverRowNumber%
, Ligne_TVQUnitaire  := RawTq_%OverRowNumber%
, Ligne_TotalHorsTx  := (Ligne_PrixUnitaire - (Ligne_TPSUnitaire + Ligne_TVQUnitaire)) * Ligne_Quantite
, Ligne_TotalTPS     := Ligne_TPSUnitaire	* Ligne_Quantite
, Ligne_TotalTVQ 	 := Ligne_TVQUnitaire	* Ligne_Quantite
, Ligne_TotalAvecTx  := Ligne_PrixUnitaire	* Ligne_Quantite


; Tooltip to format :
, TT_Tip := "Calcul ligne #"OverRowNumber " :"
		 . "`n`n   Sous-total hors taxes  :      " Ligne_TotalHorsTx "  $                    ( $ " Ligne_PrixUnitaire " * Quantité  = $ " Ligne_TotalAvecTx " - Taxes )"
		 . "`n    + TPS                           :      " Ligne_TotalTPS "  $                    ( $ " Ligne_TPSUnitaire " * Quantité )"
		 . "`n    + TVQ                          :      " Ligne_TotalTVQ "  $                    ( $ " Ligne_TVQUnitaire " * Quantité )"
		 . "`n   ________________________________________________________________________________________________________________________"
		 . "`n   Sous-total avec taxes  :      " Ligne_TotalAvecTx "  $"



ToolTip, % TT_Tip, % A_ScreenWidth//3, % A_ScreenHeight // 2.5									; Show the ToolTip
SetTimer, ToolTipOff, -4000						; Set the Timer to automatically turn off tooltip after number of ms ;*[Untitled1]
Return

ToolTipOff:
Tooltip ; Off
ExitApp
Thanks in advance
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Formatting a multi-line Tooltip to align like columns ?

28 Mar 2019, 13:35

Look for Name: String Things - Common String & Array Functions

here: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=53

I hope that helps.
DRocks
Posts: 545
Joined: 08 May 2018, 10:20

Re: Formatting a multi-line Tooltip to align like columns ?

28 Mar 2019, 13:45

wolf_II wrote:
28 Mar 2019, 13:35
Look for Name: String Things - Common String & Array Functions

here: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=53

I hope that helps.
Thank you wolf I will look at it :)
teadrinker
Posts: 1126
Joined: 29 Mar 2015, 09:41
Contact:

Re: Formatting a multi-line Tooltip to align like columns ?

28 Mar 2019, 14:41

wolf_II wrote: I hope that helps.
I'm not sure that it works as one may expect:

Image

I suppose, it could work correctly only for monospace fonts.
DRocks
Posts: 545
Joined: 08 May 2018, 10:20

Re: Formatting a multi-line Tooltip to align like columns ?

28 Mar 2019, 14:51

I had no success either with this. I tried 3 appropriate functions but no luck.


I am trying to make a listview at the moment rather than a tooltip
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Formatting a multi-line Tooltip to align like columns ?

28 Mar 2019, 14:54

@teadrinker
I would use my function LV_Box, but String things is written by professionals, so I recommend them first.
Also, LV_Box is a gui, rather than a tooltip. Lexikos says a tooltip may contain any Gui, so it is possible.
I have not tried to do that yet. You would not by chance have a gui in a tooltip example, would you?
Last edited by wolf_II on 28 Mar 2019, 15:03, edited 2 times in total.
DRocks
Posts: 545
Joined: 08 May 2018, 10:20

Re: Formatting a multi-line Tooltip to align like columns ?

28 Mar 2019, 14:57

wolf_II wrote:
28 Mar 2019, 14:54
@teadrinker
I would use my function LV_Box, but String things is written by professionals, so I recommend them first.
Also, LV_Box is a gui, rather then a tooltip. Lexikos reports, a tooltip may contain any Gui, so it is possible.
I have not tried to do that yet. You would not by chance have a gui in a tooltip example, would you?
hmm lv box might be interesting
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Formatting a multi-line Tooltip to align like columns ?

28 Mar 2019, 14:59

@DRocks: LV_box is a pain to set up first time, lemme know if I can help

Edit: I cleaned it so it is easier to use. And I renamed it on the way to LV_Show()
And I put it in my signature
Last edited by wolf_II on 29 Mar 2019, 06:51, edited 2 times in total.
teadrinker
Posts: 1126
Joined: 29 Mar 2015, 09:41
Contact:

Re: Formatting a multi-line Tooltip to align like columns ?  Topic is solved

28 Mar 2019, 15:04

As option you could using something like this to change a tooltip font:

Code: Select all

ToolTipFont(Options := "", Name := "", hwnd := "") {
    static hfont := 0
    if (hwnd = "")
        hfont := Options="Default" ? 0 : _TTG("Font", Options, Name), _TTHook()
    else
        DllCall("SendMessage", "ptr", hwnd, "uint", 0x30, "ptr", hfont, "ptr", 0)
}
 
ToolTipColor(Background := "", Text := "", hwnd := "") {
    static bc := "", tc := ""
    if (hwnd = "") {
        if (Background != "")
            bc := Background="Default" ? "" : _TTG("Color", Background)
        if (Text != "")
            tc := Text="Default" ? "" : _TTG("Color", Text)
        _TTHook()
    }
    else {
        VarSetCapacity(empty, 2, 0)
        DllCall("UxTheme.dll\SetWindowTheme", "ptr", hwnd, "ptr", 0
            , "ptr", (bc != "" || tc != "") ? &empty : 0)
        if (bc != "")
            DllCall("SendMessage", "ptr", hwnd, "uint", 1043, "ptr", bc, "ptr", 0)
        if (tc != "")
            DllCall("SendMessage", "ptr", hwnd, "uint", 1044, "ptr", tc, "ptr", 0)
    }
}
 
_TTHook() {
    static hook := 0
    if !hook
        hook := DllCall("SetWindowsHookExW", "int", 4
            , "ptr", RegisterCallback("_TTWndProc"), "ptr", 0
            , "uint", DllCall("GetCurrentThreadId"), "ptr")
}
 
_TTWndProc(nCode, _wp, _lp) {
    Critical 999
   ;lParam  := NumGet(_lp+0*A_PtrSize)
   ;wParam  := NumGet(_lp+1*A_PtrSize)
    uMsg    := NumGet(_lp+2*A_PtrSize, "uint")
    hwnd    := NumGet(_lp+3*A_PtrSize)
    if (nCode >= 0 && (uMsg = 1081 || uMsg = 1036)) {
        _hack_ = ahk_id %hwnd%
        WinGetClass wclass, %_hack_%
        if (wclass = "tooltips_class32") {
            ToolTipColor(,, hwnd)
            ToolTipFont(,, hwnd)
        }
    }
    return DllCall("CallNextHookEx", "ptr", 0, "int", nCode, "ptr", _wp, "ptr", _lp, "ptr")
}
 
_TTG(Cmd, Arg1, Arg2 := "") {
    static htext := 0, hgui := 0, WM_CTLCOLORSTATIC := 0x138, WM_GETFONT := 0x31, WS_CHILD := 0x40000000
    
    if !htext {
        Gui _TTG: +hwndhgui +%WS_CHILD%
        Gui _TTG: Add, Text, hwndhtext
    }
    Gui _TTG: %Cmd%, %Arg1%, %Arg2%
    if (Cmd = "Font") {
        GuiControl _TTG: Font, %htext%
        SendMessage, WM_GETFONT, 0, 0,, ahk_id %htext%
        return ErrorLevel
    }
    if (Cmd = "Color") {
        hdc := DllCall("GetDC", "ptr", htext, "ptr")
        SendMessage, WM_CTLCOLORSTATIC, hdc, htext,, ahk_id %hgui%
        clr := DllCall("GetBkColor", "ptr", hdc, "uint")
        DllCall("ReleaseDC", "ptr", htext, "ptr", hdc)
        return clr
    }
}

Code: Select all

#Persistent
data=
(
"Date","Pupil","Grade"
----,-----,-----
"25 May","Bloggs, Fred","C"
"25 May","Doe, Jane","B"
"15 July","Bloggs, Fred","A"
"15 April","Muniz, Alvin ""Hank""","A"
)
ToolTipFont("s10 q5", "Droid Sans Mono")
ToolTip % st_columnize(data, "csv", 2)
Image
ahk7
Posts: 238
Joined: 06 Nov 2013, 16:35

Re: Formatting a multi-line Tooltip to align like columns ?

28 Mar 2019, 15:07

Code: Select all

Data=
(join`n
Date,Pupil,Grade
25 May,Bloggs,Fred,C
25 May,Doe,Jane,B
15 July,Bloggs,Fred,A
15 April,Muniz Alvin Hank,A
)

Loop, parse, data, `n, `r
	{
	Loop, parse, A_LoopField, CSV
		{
		 show .= Format("{:-30}",A_LoopField) A_Tab
		}
	show .= "`n"
	}
ToolTip % Show
Sleep 2000
ExitApp
DRocks
Posts: 545
Joined: 08 May 2018, 10:20

Re: Formatting a multi-line Tooltip to align like columns ?

28 Mar 2019, 15:11

teadrinker wrote:
28 Mar 2019, 15:04
Nice you have it correctly on your setup.
Sad thing is I copied all your example with functions and st_ library and it doesnt order proeprly on my win10 machine. Wierd!


Image
DRocks
Posts: 545
Joined: 08 May 2018, 10:20

Re: Formatting a multi-line Tooltip to align like columns ?

28 Mar 2019, 15:12

ahk7 wrote:
28 Mar 2019, 15:07

Code: Select all

Data=
(join`n
Date,Pupil,Grade
25 May,Bloggs,Fred,C
25 May,Doe,Jane,B
15 July,Bloggs,Fred,A
15 April,Muniz Alvin Hank,A
)

Loop, parse, data, `n, `r
	{
	Loop, parse, A_LoopField, CSV
		{
		 show .= Format("{:-30}",A_LoopField) A_Tab
		}
	show .= "`n"
	}
ToolTip % Show
Sleep 2000
ExitApp
WTF this works let me analyze it. ty!

Man, I tried using format with individual values and I had no success at all and you just shown me a example of it inside a loop. very neat
teadrinker
Posts: 1126
Joined: 29 Mar 2015, 09:41
Contact:

Re: Formatting a multi-line Tooltip to align like columns ?

28 Mar 2019, 15:14

@ahk7:

Code: Select all

Data=
(join`n
Date,Pupil,Grade
25 May,Bloggs,Fred,C
25 May,Here is a very long text: 6666666666666666666666666666666666,Jane,B
15 July,Bloggs,Fred,A
15 April,Muniz Alvin Hank,Here is a very long text: 6666666666666666666666666666666666
)

Loop, parse, data, `n, `r
   {
   Loop, parse, A_LoopField, CSV
      {
       show .= Format("{:-30}",A_LoopField) A_Tab
      }
   show .= "`n"
   }
ToolTip % Show
Sleep 2000
ExitApp
teadrinker
Posts: 1126
Joined: 29 Mar 2015, 09:41
Contact:

Re: Formatting a multi-line Tooltip to align like columns ?

28 Mar 2019, 15:19

DRocks wrote: it doesnt order proeprly on my win10 machine
Perhaps you just have not Droid Sans Mono font installed in your machine. Try using a different monospaced font, such as Consolas.
Last edited by teadrinker on 28 Mar 2019, 15:22, edited 2 times in total.
DRocks
Posts: 545
Joined: 08 May 2018, 10:20

Re: Formatting a multi-line Tooltip to align like columns ?

28 Mar 2019, 15:20

teadrinker wrote:
28 Mar 2019, 15:14
Ah yeah, right it messes up now with this example
DRocks
Posts: 545
Joined: 08 May 2018, 10:20

Re: Formatting a multi-line Tooltip to align like columns ?

28 Mar 2019, 15:22

teadrinker wrote:
28 Mar 2019, 15:19
DRocks wrote: t doesnt order proeprly on my win10 machine
Perhaps you just have not Droid Sans Mono font installed in your machine. Try using a different monospaced font, such as Consolas.
Confirmed. With Consolas it works like on your screenshot :D
Thank you guys, this is great !
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Formatting a multi-line Tooltip to align like columns ?

28 Mar 2019, 15:26

Re. fonts, there's also Courier New.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
ahk7
Posts: 238
Joined: 06 Nov 2013, 16:35

Re: Formatting a multi-line Tooltip to align like columns ?

28 Mar 2019, 15:37

You can/must tinker with the value (-30 in the example), depending on the data it might not be the best solution of course, but by that time you probably want something else to present data like a listview or perhaps html (using a table)
DRocks
Posts: 545
Joined: 08 May 2018, 10:20

Re: Formatting a multi-line Tooltip to align like columns ?

28 Mar 2019, 15:46

Thank you. I am testing all options. Might end-up with a Gui ListView acting like a tooltip
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Formatting a multi-line Tooltip to align like columns ?

28 Mar 2019, 15:58

If you get one going inside a tooltip, I would love to see it :D

Return to “Ask For Help”

Who is online

Users browsing this forum: No registered users and 203 guests