MenuItem tool tips

Just starting out? Need help? Post your questions and find answers here.
fablefox
User
User
Posts: 10
Joined: Wed Nov 26, 2014 3:27 am

MenuItem tool tips

Post by fablefox »

I've checked GadgetToolTip(#Gadget, Text$) and I noticed that Menu is not part of the gadget supported.

Is there a simple way to add tool tips to a menu item?

Thanks.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4955
Joined: Sun Apr 12, 2009 6:27 am

Re: MenuItem tool tips

Post by RASHAD »

Hi
For Windows

Code: Select all

Global  tip,ti.TOOLINFO

Procedure CurPos(parameter)
  Repeat
     GetMenuItemRect_(WindowID(0),MenuID(0),0,r.RECT)
     GetMenuItemRect_(WindowID(0),MenuID(0),1,r2.RECT)
     GetMenuItemRect_(WindowID(0),MenuID(0),2,r3.RECT)
     GetCursorPos_(p.POINT)   
      If PtInRect_(r, PeekQ(@p))
          ti\lpszText=@" One"
          SendMessage_(tip, #TTM_ADDTOOL, 0, ti)
      ElseIf PtInRect_(r2, PeekQ(@p))
          ti\lpszText=@" Two"
          SendMessage_(tip, #TTM_ADDTOOL, 0, ti)
      ElseIf PtInRect_(r3, PeekQ(@p))
          ti\lpszText=@" Three"
          SendMessage_(tip, #TTM_ADDTOOL, 0, ti)
      Else
          SendMessage_(tip, #TTM_DELTOOL, 0, ti)
      EndIf
  ForEver
EndProcedure


If OpenWindow(0, 100, 150, 195, 260, "PureBasic - Menu")
 
  If CreateMenu(0, WindowID(0))
    MenuTitle("File")
      MenuItem( 1, "&Load...")
      MenuItem( 2, "Save")
      MenuItem( 3, "Save As...")
      MenuBar()
      OpenSubMenu("Recents")
        MenuItem( 5, "Pure.png")
        MenuItem( 6, "Basic.jpg")
        OpenSubMenu("Even more !")
          MenuItem( 12, "Yeah")
        CloseSubMenu()
        MenuItem( 13, "Rocks.tga")
      CloseSubMenu()
      MenuBar()
      MenuItem( 7, "&Quit")

    MenuTitle("Edition")
      MenuItem( 8, "Cut")
      MenuItem( 9, "Copy")
      MenuItem(10, "Paste")
      
    MenuTitle("?")
      MenuItem(11, "About")

  EndIf
  
  DisableMenuItem(0, 3, 1)
  DisableMenuItem(0, 13, 1)
  
  Tip = CreateWindowEx_(#WS_EX_TOPMOST,"Tooltips_Class32",0, #TTS_ALWAYSTIP|#TTS_BALLOON,#CW_USEDEFAULT, #CW_USEDEFAULT,#CW_USEDEFAULT,#CW_USEDEFAULT, WindowID(0),0,GetModuleHandle_(0),0) 
  SetWindowTheme_(Tip, @null.w, @null.w)
  SendMessage_(tip,#TTM_SETTIPTEXTCOLOR,$0202FD,0)
  SendMessage_(tip,#TTM_SETTIPBKCOLOR,$DCFFFF,0)
  SendMessage_(Tip, #TTM_SETDELAYTIME, #TTDT_INITIAL,100)
  SendMessage_(Tip, #TTM_SETDELAYTIME, #TTDT_AUTOPOP,1000)   
  ti\cbSize = SizeOf(ti)
  ti\uFlags = #TTF_IDISHWND|#TTF_SUBCLASS
  ti\uId = WindowID(0)  
    
  thread = CreateThread(@CurPos(),30)
  Repeat

    Select WaitWindowEvent()
    
      Case #PB_Event_Menu

        Select EventMenu()  ; To see which menu has been selected

          Case 11 ; About
            MessageRequester("About", "Cool Menu example", 0)
            
          Default
            MessageRequester("Info", "MenuItem: "+Str(EventMenu()), 0)

        EndSelect

      Case #PB_Event_CloseWindow
        KillThread(thread)
        Quit = 1

    EndSelect

  Until Quit = 1

EndIf

End  
Egypt my love
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4955
Joined: Sun Apr 12, 2009 6:27 am

Re: MenuItem tool tips

Post by RASHAD »

Much simple and should be cross platform :)

Code: Select all

Global  x0,x1,x2,x3,y0,y1,Text1$,Text2$,Text3$
ExamineDesktops()

Procedure CurPos(parameter) 
  Repeat
    x = DesktopMouseX()
    y = DesktopMouseY()
    x0 = WindowX(0,#PB_Window_FrameCoordinate)+4 
    y0 = WindowY(0,#PB_Window_InnerCoordinate) - MenuHeight()
    y1 = y0 + MenuHeight()
       If x >= x0 And x <= (x0+x1) And y >= y0 And y <= y1
         ResizeGadget(1,0,5,#PB_Ignore,#PB_Ignore)
         SetGadgetText(1,Text1$)
         Delay(100)
       ElseIf x >= (x0+x1) And x <= (x0+x1+x2) And y >= y0 And y <= y1
         ResizeGadget(1,x11,5,#PB_Ignore,#PB_Ignore)
         SetGadgetText(1,Text2$)
         Delay(100)
       ElseIf x >= (x0+x1+x2) And x <= (x0+x1+x2+x3) And y >= y0 And y <= y1
         ResizeGadget(1,x22,5,#PB_Ignore,#PB_Ignore)
         SetGadgetText(1,Text3$)
         Delay(100)
       Else
         ResizeGadget(1,-100,0,#PB_Ignore,#PB_Ignore)
         SetGadgetText(1,"")
         Delay(100)
       EndIf
  ForEver
EndProcedure


If OpenWindow(0, 100, 150, 195, 260, "PureBasic - Menu")
TextGadget(1,10,60,100,16,"tiuiutiti",#PB_Text_Center)
SetGadgetColor(1,#PB_Gadget_BackColor,$DBFEFD)
SetGadgetColor(1,#PB_Gadget_FrontColor,$0000FF)

 
  If CreateMenu(0, WindowID(0))
    MenuTitle("File")
      MenuItem( 1, "&Load...")
      MenuItem( 2, "Save")
      MenuItem( 3, "Save As...")
      MenuBar()
      OpenSubMenu("Recents")
        MenuItem( 5, "Pure.png")
        MenuItem( 6, "Basic.jpg")
        OpenSubMenu("Even more !")
          MenuItem( 12, "Yeah")
        CloseSubMenu()
        MenuItem( 13, "Rocks.tga")
      CloseSubMenu()
      MenuBar()
      MenuItem( 7, "&Quit")

    MenuTitle("Edition")
      MenuItem( 8, "Cut")
      MenuItem( 9, "Copy")
      MenuItem(10, "Paste")
      
    MenuTitle("?")
      MenuItem(11, "About")

  EndIf  
  DisableMenuItem(0, 3, 1)
  DisableMenuItem(0, 13, 1)
  
  sp$ = Space(4)
  
  StartDrawing(WindowOutput(0))
    x1 = TextWidth(sp$+GetMenuTitleText(0,0));*5
    x2 = TextWidth(sp$+GetMenuTitleText(0,1));*5
    x3 = TextWidth(sp$+GetMenuTitleText(0,2));*5
  StopDrawing()
   
 
  Text1$ = "File ToolTip"
  Text2$ = "Edition ToolTip"
  Text3$ = "Help ToolTip"  
   
  thread = CreateThread(@CurPos(),30)
  Repeat

    Select WaitWindowEvent()
    
      Case #PB_Event_Menu

        Select EventMenu()  ; To see which menu has been selected

          Case 11 ; About
            MessageRequester("About", "Cool Menu example", 0)
            
          Default
            MessageRequester("Info", "MenuItem: "+Str(EventMenu()), 0)

        EndSelect

      Case #PB_Event_CloseWindow
        KillThread(thread)
        Quit = 1

    EndSelect

  Until Quit = 1

EndIf

End   
Edit :Bugs fixed
Last edited by RASHAD on Wed Apr 29, 2015 7:41 pm, edited 1 time in total.
Egypt my love
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 464
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: MenuItem tool tips

Post by Mindphazer »

Hi Rashad
It doesn't work on OS X because on OS X menus are -always- on the top of the screen, they are not attached to a window...
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
fablefox
User
User
Posts: 10
Joined: Wed Nov 26, 2014 3:27 am

Re: MenuItem tool tips

Post by fablefox »

Thanks.

Too complicated for what I planned currently. I was thinking just a few lines, the way GadgetTooltip works.

Anyway, thanks again. I'll keep this in mind when my app is ready to use it.
Post Reply