1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,Dialogs, Menus,
AppEvnts, StdCtrls, ExtCtrls, ComCtrls;
type
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;
type
TForm1 = class(TForm)
Label1: TLabel;
StatusBar1: TStatusBar;
MainMenu1: TMainMenu;
Fichier1: TMenuItem;
Ouvrir1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure ApplicationEvents1Hint(Sender: TObject) ;
private
{ Déclarations privées }
miHint : TMenuItemHint;
procedure WMMenuSelect(var Msg: TWMMenuSelect) ; message WM_MENUSELECT;
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin miHint := TMenuItemHint.Create(self);
end;
procedure TForm1.ApplicationEvents1Hint(Sender: TObject);
begin //StatusBar1.SimpleText := 'App.OnHint : ' + Application.Hint;
// Remarque : le code fonctionne aussi en neutralisant la StatusBar comme ci-dessus
// et même en conservant cette StatusBar avec AutoHint=True celle-ci n'affiche pas un duplicata du Hint
// qui apparaît à côté du menuItem
end;
procedure TForm1.WMMenuSelect(var Msg: TWMMenuSelect);
var menuItem : TMenuItem;
hSubMenu : HMENU;
begin
inherited; // from TCustomForm (ensures 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
// TMenuItemHint ---------------------------------------------------------------
constructor TMenuItemHint.Create(AOwner: TComponent) ;
begin inherited;
showTimer := TTimer.Create(self) ;
showTimer.Interval := Application.HintPause;
hideTimer := TTimer.Create(self) ;
hideTimer.Interval := Application.HintHidePause;
end; //Create
destructor TMenuItemHint.Destroy;
begin hideTimer.OnTimer := nil;
showTimer.OnTimer := nil;
self.ReleaseHandle;
inherited;
end; //Destroy
procedure TMenuItemHint.DoActivateHint(menuItem: TMenuItem) ;
begin //force remove of the "old" hint window
hideTime(self) ;
if (menuItem = nil) or (menuItem.Hint = '') then begin
activeMenuItem := nil;
Exit;
end;
activeMenuItem := menuItem;
showTimer.OnTimer := ShowTime;
hideTimer.OnTimer := HideTime;
end; //DoActivateHint
procedure TMenuItemHint.ShowTime(Sender: TObject) ;
var r : TRect;
wdth : integer;
hght : integer;
begin if activeMenuItem <> nil then begin
//position and resize
wdth := Canvas.TextWidth(activeMenuItem.Hint) ;
hght := Canvas.TextHeight(activeMenuItem.Hint) ;
r.Left := Mouse.CursorPos.X + 16;
r.Top := Mouse.CursorPos.Y + 16;
r.Right := r.Left + wdth + 6;
r.Bottom := r.Top + hght + 4;
ActivateHint(r,activeMenuItem.Hint) ;
end;
showTimer.OnTimer := nil;
end; //ShowTime
procedure TMenuItemHint.HideTime(Sender: TObject) ;
begin //hide (destroy) hint window
self.ReleaseHandle;
hideTimer.OnTimer := nil;
end; // HideTime
END. |
Partager