GadgetTooltip, using lines instead of single line

Just starting out? Need help? Post your questions and find answers here.
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: GadgetTooltip, using lines instead of single line

Post by mk-soft »

@Mijikai

Thank you, I was already looking for the code ...

But don't use SysAllocString, just the pointer to the text.
TTM_AddTool creates a copy of the text itself.
Rect was wrong because tooltip is subclass from gadget

Update
- hInst not used
- Added FreeToolTip for release resources

Code: Select all

EnableExplicit

Procedure.i ToolTip(Gadget, Text.s, Width = 150)
  Protected hwnd.i
  Protected ti.TOOLINFO
  If IsGadget(Gadget)
    ti\cbSize = SizeOf(TOOLINFO)
    ti\uFlags = #TTF_SUBCLASS
    ti\hWnd = GadgetID(Gadget)
    ti\uId = Gadget
    ti\lpszText = @Text
    ti\rect\left = 0
    ti\rect\top = 0
    ti\rect\right = GadgetWidth(Gadget)
    ti\rect\bottom = GadgetHeight(Gadget)
    hwnd = CreateWindowEx_(#Null,#TOOLTIPS_CLASS,#Null,
                                   #WS_POPUP|#TTS_ALWAYSTIP,
                                   #CW_USEDEFAULT,#CW_USEDEFAULT,#CW_USEDEFAULT,
                                   #CW_USEDEFAULT,ti\hWnd,#Null,ti\hInst,#Null)
    If hwnd
      SetProp_(ti\hWnd, "MyToolTipHandle", hwnd)
      SendMessage_(hwnd,#TTM_ADDTOOL,#Null,@ti)
      SendMessage_(hwnd,#TTM_SETMAXTIPWIDTH,#Null,Width)
      ProcedureReturn #True
    EndIf
  EndIf
  ProcedureReturn #False
EndProcedure

Procedure FreeToolTip(Gadget)
  Protected r1, hwnd, ti.TOOLINFO
  If IsGadget(Gadget)
    hwnd = RemoveProp_(GadgetID(Gadget), "MyToolTipHandle")
    If hwnd
      ti\cbSize = SizeOf(TOOLINFO)
      ti\hWnd = GadgetID(Gadget)
      ti\uId = Gadget
      SendMessage_(hwnd,#TTM_DELTOOL,#Null,@ti)
    EndIf
  EndIf
EndProcedure

    
Procedure.i Main()
  If OpenWindow(0,0,0,800,600,"Dummy",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
    ButtonGadget(1,10,30,200,30,"Hello World!")
    ToolTip(1,"Hello World!" + #CR$ + "How does this magic work?" + #CR$ + "Black Magic!!!!")
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Break
        Case #PB_Event_Gadget
          Select EventGadget()
            Case 1
              FreeToolTip(1)
          EndSelect
      EndSelect
    ForEver
    FreeToolTip(1)      
    CloseWindow(0)  
  EndIf
  ProcedureReturn #Null
EndProcedure

End Main()

Last edited by mk-soft on Thu Nov 09, 2023 7:31 pm, edited 2 times in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Mijikai
Addict
Addict
Posts: 1520
Joined: Sun Sep 11, 2016 2:17 pm

Re: GadgetTooltip, using lines instead of single line

Post by Mijikai »

Thank you @mk-soft now its flawless, you also added the Width as parameter 8)
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: GadgetTooltip, using lines instead of single line

Post by mk-soft »

A little more ...

Update viewtopic.php?p=610526#p610526
- ti\hInst not used, because text not from resources
- Added FreeToolTip for release resources

;)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Mijikai
Addict
Addict
Posts: 1520
Joined: Sun Sep 11, 2016 2:17 pm

Re: GadgetTooltip, using lines instead of single line

Post by Mijikai »

Nice @mk-soft
i also took another look and changed some things.

Code: Select all

EnableExplicit

Procedure.i ToolTipCreate(Gadget.i,Text.s,Width.i = 150)
  Protected hwnd.i
  Protected ti.TOOLINFO
  If IsGadget(Gadget)
    ti\cbSize = SizeOf(TOOLINFO)
    ti\uFlags = #TTF_SUBCLASS
    ti\hWnd = GadgetID(Gadget)
    ti\lpszText = @Text
    ti\rect\right = GadgetWidth(Gadget)
    ti\rect\bottom = GadgetHeight(Gadget)
    hwnd = FindWindow_(#TOOLTIPS_CLASS,Str(ti\hWnd))
    If Not hwnd
      hwnd = CreateWindowEx_(#Null,#TOOLTIPS_CLASS,Str(ti\hWnd),
                             #WS_POPUP|#TTS_ALWAYSTIP,
                             #CW_USEDEFAULT,#CW_USEDEFAULT,#CW_USEDEFAULT,
                             #CW_USEDEFAULT,ti\hWnd,#Null,#Null,#Null)
      
      If hwnd
        SendMessage_(hwnd,#TTM_ADDTOOL,#Null,@ti)
        SendMessage_(hwnd,#TTM_SETMAXTIPWIDTH,#Null,Width)
        ProcedureReturn #True
      EndIf
    EndIf
  EndIf
  ProcedureReturn #False
EndProcedure

Procedure.i ToolTipRelease(Gadget.i)
  Protected hwnd.i
  Protected ti.TOOLINFO
  If IsGadget(Gadget)
    ti\cbSize = SizeOf(TOOLINFO)
    ti\hWnd = GadgetID(Gadget)
    hwnd = FindWindow_(#TOOLTIPS_CLASS,Str(ti\hWnd))
    If hwnd
      SendMessage_(hwnd,#TTM_DELTOOL,#Null,@ti)
      SendMessage_(hwnd,#WM_CLOSE,#Null,#Null)
    EndIf
  EndIf
  ProcedureReturn #Null
EndProcedure

    
Procedure.i Main()
  If OpenWindow(0,0,0,800,600,"Dummy",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
    ButtonGadget(1,10,30,200,30,"Hello World!")
    ButtonGadget(2,10,90,200,30,"Hello World!")
    ToolTipCreate(1,"Hello World!" + #CR$ + "How does this magic work?" + #CR$ + "Black Magic!!!!")
    ToolTipCreate(2,"Hello World!" + #CR$ + "How does this magic work?" + #CR$ + "Black Magic!!!!")
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Break
        Case #PB_Event_Gadget
          Select EventGadget()
            Case 1
              ToolTipRelease(1)
          EndSelect
      EndSelect
    ForEver
    ToolTipRelease(1) 
    ToolTipRelease(2)
    CloseWindow(0)  
  EndIf
  ProcedureReturn #Null
EndProcedure

End Main()
hoangdiemtinh
User
User
Posts: 97
Joined: Wed Nov 16, 2022 1:51 pm

Re: GadgetTooltip, using lines instead of single line

Post by hoangdiemtinh »

Thank @mk-soft @Mijikai for sharing.

I want to add background color and text color. But why don't they work ?

Code: Select all

SendMessage_(hwnd, #TTM_SETDELAYTIME, #TTDT_INITIAL, 0)
SendMessage_(hwnd, #TTM_SETTIPBKCOLOR, #Black, 0)                    ;BackColor Tooltip
SendMessage_(hwnd, #TTM_SETTIPTEXTCOLOR, #Red, 0)             ;TextColor Tooltip
PC: Windows 10 x64, 8GB RAM. PB ver: 6.x
--
I love PB5 vs PB6 :)
User avatar
jacdelad
Addict
Addict
Posts: 2032
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: GadgetTooltip, using lines instead of single line

Post by jacdelad »

https://learn.microsoft.com/en-us/windo ... tipbkcolor
Look at "remarks", no effect when using visual styles.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Post Reply