Mega Code Archive

 
Categories / Delphi / Examples
 

Creating custom hints for yoru applicationcomponent

Title: Creating custom hints for yoru application/component Question: This is a simple solution to creating/adding custom hints to your application or component (mostly usefull to component writing, or people drawing images/information to onscreen canvas's). Answer: Often when development custom components, or displaying information on a canvas, you want to display a fly over hint -inside- the component instead of beneath it like standard hints, this can be accomplished with a THintWindow and TTimer component. Simply add the following to your form/component: type THintEvent = procedure(Sender: TObject; var AHint: string) of object; private HintWindow: THintWindow; HintTimer: TTimer; fShowHint: Boolean; fOnHint: THintEvent; published property ShowHint: Boolean read fShowHint write fShowHint; property OnHint: THintEvent read fOnHint write fOnHint; and in the Create code: begin ... HintWindow := THintWindow.Create(Self); HintWindow.Color := clInfoBk; HintTimer := TTimer.Create(Self); HintTimer.Enabled := False; HintTimer.Interval := 3000; HintTimer.OnTimer := HintTimerEvent; ... end; Add the following procedure: procedure TMapField.TimerHWinEvent(Sender: TObject); begin HintWindow.ReleaseHandle; // Hide the Hint Window HintTimer.Enabled := False; end; Now we're ready to actually display your hint, usually this code would be called in response to some move movement event, personally I write a function called HandleHint (called from the mouse event) which goes something this: procedure TForm1.HandleHint(X,Y: Integer); var p: TPoint; s: string; sWidth: integer; sHeight: integer; i, iWidth: integer; sList: TStringList; begin sList := TStringList.Create; GetCursorPos(P); s := 'Set this to whatever your hint will be'; // Provide an event to modify s - usefull for when in components if Assigned(fOnHint) then fOnHint(Self, s); // Only show the hint if ShowHint is true and it differs from // the last hint we showed. if (ShowHint) and (HintWindow.Caption s) then begin HintTimer.Enabled := False; // Find longest string for a multi-line hint. sList.Text := s; sWidth := 0; for i := 0 to sList.Count - 1 do begin iWidth := HintWindow.Canvas.TextWidth(sList.Strings[i]); if iWidth sWidth then sWidth := iWidth; end; sHeight := HintWindow.Canvas.TextHeight(s) * sList.Count; HintWindow.ActivateHint( Rect(P.X,P.Y, P.x + sWidth + 6, P.Y + sHeight),s); HintTimer.Enabled := True; end; // if (ShowHint)... end; And as with all good code, in your Destroy method, don't forget to free what we made: begin ... HintWindow.Free; HintTimer.Free; ... end; Mark Derricutt / Talios