Title: Custom statusbar to show hints without any coding
Question: How do I show tooltips/hints for menu items and any visual control
Answer:
There are several methods to show hints in a statusbar (or similar control), each of these methods requires more then one line of code!
There are more important things to be spending your time on, drop the following code into a file and save it as TheStatusbar.pas, now install it in the Delphi IDE menu
File-Components-Install Component
Once installed drop the statusbar onto a form, set the hint property of one or more controls (do some menu items too for the test). Compile/run the project and while running place the mouse cursor over any of the items which you set the hint property. You should see the hint in the statusbar.
Note: I have not tested this in D6 but be forewarned that "DsgnIntf" in the USES clause will most likely be a problem, if so then remove it and all code that does the About dialog.
unit TheStatusBar;
{*****************************************************************}
{ Description }
{ Makes it easy to show hints for components and menu items in the}
{ statusbar. All the programmer needs to do is write the text for }
{ the desired item to show hints. You do not have to set ShowHints}
{ to True on a form unless you want ballon style hints also. }
{ }
{ Tested with D3 to D5 }
{=================================================================}
{ USE TSmartStatusBar AT YOUR OWN RISK. }
{ I AM NOT RESPONSIBLE FOR ANY HARM THIS COMPONENT MIGHT CAUSE!! }
{ }
{ Kevin S. Gallagher }
{ E-Mail : Gallaghe@teleport.com Home }
{ kevin.s.gallagher@state.or.us Work }
{*****************************************************************}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, DsgnIntf;
type
TAbout = class(TPropertyEditor)
public
procedure Edit; override ;
function GetAttributes: TPropertyAttributes; override ;
function GetValue: string; override ;
end;
TSmartStatusBar = class(TStatusBar)
private
FSizeGrip: Boolean ;
FAbout: TAbout ;
FOldOnHint: TNotifyEvent ;
FSysOnHint: TNotifyEvent ;
FShowAppHint: boolean ;
procedure SetShowAppHint(b : boolean) ;
protected
procedure DispAppHint(Sender : TObject) ;
public
constructor Create(AOwner: TComponent); override ;
published
property About: TAbout read FAbout write FAbout ;
property ShowAppHint : boolean read FShowAppHint write SetShowAppHint default True ;
end;
procedure Register;
implementation
uses Commctrl ;
procedure TAbout.Edit ;
begin
Application.MessageBox('TSmartStatusBar component' + #13 +
'1998-2001 Kevin S. Gallagher - This component is freeware.',
'About TSmartStatusBar Component', MB_OK + MB_ICONINFORMATION) ;
end;
function TAbout.GetAttributes: TPropertyAttributes ;
begin
Result:= [paMultiSelect, paDialog, paReadOnly] ;
end ;
function TAbout.GetValue: string ;
begin
Result:= '(about)' ;
end ;
constructor TSmartStatusBar.Create(AOwner: TComponent) ;
begin
if not(AOwner is TForm) then
raise EInvalidOperation.Create('Can only drop me on a form') ;
inherited Create(AOwner) ;
SimplePanel := True ;
FSizeGrip := True ;
FShowAppHint := True ;
ShowAppHint := True ;
FSysOnHint := Application.OnHint ;
{ Allow controls to be placed onto this component }
ControlStyle := ControlStyle + [csAcceptsControls] ;
end ;
procedure TSmartStatusBar.SetShowAppHint(b : boolean) ;
begin
FShowAppHint := b ;
if not(csDesigning in ComponentState) then
begin
if b then
begin
FOldOnHint := Application.OnHint ;
Application.OnHint := DispAppHint ;
end else
Application.OnHint := FOldOnHint ;
end;
end;
procedure TSmartStatusBar.DispAppHint(Sender : TObject) ;
begin
if not(csDesigning in ComponentState) then
if FShowAppHint then
SimpleText := Application.Hint ;
if Assigned(FOldOnHint) then
FOldOnHint(Sender) ;
end;
procedure Register;
begin
RegisterComponents('Win32', [TSmartStatusBar]);
RegisterPropertyEditor(TypeInfo(TAbout), TSmartStatusBar, 'ABOUT', TAbout);
end;
end.
Make sure to read the comments below on "AutoHint"
Example code for working with a main form and a child form:
{ Here we stop the hints from being displayed in this the main form
and show them in the child form. }
procedure TForm1.cmdTestOneClick(Sender: TObject);
var
f:TfrmChild;
begin
SmartStatusBar1.ShowAppHint := False ;
f:= TfrmChild.Create(Self) ;
try
f.ShowModal ;
SmartStatusBar1.ShowAppHint := True ;
finally
f.release ;
end ;
end;
{ Here we stop the hints from being displayed in the child form and
show them in the main form. Please note that if the child form is
not disabled then both forms will show the same hint which would
be confusing to some users. }
procedure TForm1.cmdTestTwoClick(Sender: TObject);
var
f:TfrmChild;
begin
f:= TfrmChild.Create(Self) ;
try
f.SmartStatusBar1.ShowAppHint := False ;
f.ShowModal ;
finally
f.release ;
end ;
end;