Implement tooltips in a TListView (Views: 30)
Problem/Question/Abstract: Is there a possibility to get tooltips in a common TListView component under Delphi 4.0? I want to display details if the user moves the mouse over an item and wait a little (same function like the component names in Delphi, if you move your mouse over a component). Answer: There is an event handler in Delphi 5, which makes it possible for you to get tooltips for each item of a ListView easily: TListView.OnInfoTip. In Delphi 3 and 4, you have to write your own hint event handler, which you assign to the method OnShowHint of TApplication: unit Test_u1; { ... } type TForm1 = class(TForm) ListView1: TListView; { ... } private procedure DisplayHint(var HintStr: string; var CanShow: Boolean; var HintInfo: THintInfo); end; { ... } implementation {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject); var NewItem: TListItem; begin Application.OnShowHint := DisplayHint; { ... } end; procedure TForm1.DisplayHint; var Item: TListItem; Rect: TRect; begin CanShow := true; {Trace the item of ListView1, which is found on the mouse position X, Y. If the mouse isn't dragged over a item, result will be nil.} Item := ListView1.GetItemAt(HintInfo.CursorPos.X, HintInfo.CursorPos.Y); if Item <> nil then begin Rect := Item.DisplayRect(drBounds); {in coordinates of ListView1!} HintInfo.HintStr := 'Mouse is over Item ' + Item.Caption; end else begin Rect := ActiveControl.ClientRect; HintInfo.HintStr := GetShortHint(TControl(ActiveControl).Hint); end; { Converting into coordinates of screen. } Rect.TopLeft := ActiveControl.ClientToScreen(Rect.TopLeft); Rect.BottomRight := ActiveControl.ClientToScreen(Rect.BottomRight); with HintInfo do begin HintPos.Y := Rect.Top + GetSystemMetrics(SM_CYCURSOR); HintPos.X := Rect.Left + GetSystemMetrics(SM_CXCURSOR); HintMaxWidth := TControl(ActiveControl).ClientWidth; HintColor := clInfoBk; ReshowTimeout := 10; HideTimeout := 100; end; end; end. BTW: The type THintInfo is used to define the appearance and the function of the HintWindow: type THintWindowClass = class of THintWindow; THintInfo = record HintControl: TControl; HintWindowClass: THintWindowClass; HintPos: TPoint; HintMaxWidth: Integer; HintColor: TColor; CursorRect: TRect; CursorPos: TPoint; ReshowTimeout: Integer; HideTimeout: Integer; HintStr: string; HintData: Pointer; end; |