Monday, May 11, 2020

How to Display Menu Item Hints in Delphi Applications

Use specific coding language to program Delphi applications to display a hint, or tooltip, when the mouse hovers over a menu component. If the ShowHint property is set to true and you add text to the hint property, this message will be displayed when the mouse is placed over the component (a TButton, for example). Enable Hints for Menu Items Because of the way Windows is designed, even if you set the value for the hint property to a menu item, the popup hint will not get displayed. However, the Windows start menu items do display hints. The favorites menu in Internet Explorer also displays menu item hints. It is possible to use the OnHint event of the global application variable in Delphi applications to display menu item hints in a status bar. Windows does not expose the messages needed to support a traditional OnMouseEnter event. However, the WM_MENUSELECT message is sent when the user selects a menu item. The WM_MENUSELECT implementation of the TCustomForm (ancestor of the TForm) sets the menu item hint to Application.Hint so it can be used in the Application.OnHint event. If you want to add menu item popup hints (tooltips) to your Delphi application menus, focus on the WM_MenuSelect message. Popup Hints Since you cannot rely on the Application.ActivateHint method to display the hint window for menu items (as menu handling is completely done by Windows), to get the hint window displayed you must create your own version of the hint window by deriving a new class from the THintWindow. Heres how to create a TMenuItemHint class. This is a hint widow that actually gets displayed for menu items! First, you need to handle the WM_MENUSELECT Windows message: type TForm1 class(TForm) ... private procedure WMMenuSelect(var Msg: TWMMenuSelect) ; message WM_MENUSELECT; end...implementation...procedure TForm1.WMMenuSelect(var Msg: TWMMenuSelect) ;var  Ã‚  menuItem : TMenuItem;  Ã‚  hSubMenu : HMENU;begin inherited; // from TCustomForm (so that Application.Hint is assigned) menuItem : nil; if (Msg.MenuFlag $FFFF) or (Msg.IDItem 0) then begin if Msg.MenuFlag and MF_POPUP MF_POPUP then begin hSubMenu : GetSubMenu(Msg.Menu, Msg.IDItem) ; menuItem : Self.Menu.FindItem(hSubMenu, fkHandle) ; end else begin menuItem : Self.Menu.FindItem(Msg.IDItem, fkCommand) ; end; end;  Ã‚  miHint.DoActivateHint(menuItem) ;end; (*WMMenuSelect*) Quick info: the WM_MENUSELECT message is sent to a menus owner window when the user selects (but does not click) a menu item. Using the FindItem method of the TMenu class, you can get the menu item currently selected. Parameters of the FindItem function relate to the properties of the message received. Once we know what menu item the mouse is over, we call the DoActivateHint method of the TMenuItemHint class. The miHint variable is defined as var miHint : TMenuItemHint and is created in the Forms OnCreate event handler. Now, whats left is the implementation of the TMenuItemHint class. Heres the interface part: TMenuItemHint class(THintWindow)private activeMenuItem : TMenuItem; showTimer : TTimer; hideTimer : TTimer; procedure HideTime(Sender : TObject) ; procedure ShowTime(Sender : TObject) ;public constructor Create(AOwner : TComponent) ; override; procedure DoActivateHint(menuItem : TMenuItem) ; destructor Destroy; override;end; Basically, the DoActivateHint function calls the ActivateHint method of the THintWindow using the TMenuItems Hint property (if it is assigned). The showTimer is used to ensure that the HintPause of the Application elapses before the hint is displayed. The hideTimer uses Application.HintHidePause to hide the hint window after a specified interval. Using Menu Item Hints While some might say that it is not a good design to display hints for menu items, there are situations where actually displaying menu item hints is much better than using a status bar. A most recently used (MRU) menu item list is one such case. A custom taskbar menu is another.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.