Question

I want a TPageControl and some TTabSheets, with 'per tabsheet' tooltip hints visible as I hover over each tab in turn.

Is there any way of getting this effect in Delphi 2009?

Was it helpful?

Solution

Just hook the Page Control's Mouse Move event and use the TabAtPos property to determine which tab the mouse is hovering over. Then assign that tab's Hint to the Page Control's hint property.

procedure TForm.PageMouseMove(Sender: TObject; Shift: TShiftState; X, Y: integer);
var
  tabindex: integer;
begin
  tabindex := PageControl.IndexOfTabAt(X, Y);
  if (tabindex >= 0) and (PageControl.Hint <> PageControl.Pages[tabindex].Hint) then
  begin
    Application.CancelHint;
    PageControl.Hint := PageControl.Pages[tabindex].Hint;
    PageControl.ShowHint := true;
  end;
end;

CancelHint/ShowHint will take care of updating the hint window when mouse moves directly from one tab to another.

Improved but ugly version below also temporarily changes HintPause to 0 when mouse is moved directly from tab to tab so that the hint is redisplayed immediately. (The "ugly" part of the solution goes to the Application.ProcessMessages call which forces hint messages to be processed before HintPause is restored.)

procedure TForm.PagesMouseMove(Sender: TObject; Shift: TShiftState; X, Y: integer);
var
  hintPause: integer;
  tabindex: integer;
begin
  tabindex := PageControl.IndexOfTabAt(X, Y);
  if (tabindex >= 0) and (PageControl.Hint <> PageControl.Pages[tabindex].Hint) then
  begin
    hintPause := Application.HintPause;
    try
      if PageControl.Hint <> '' then
        Application.HintPause := 0;
      Application.CancelHint;
      PageControl.Hint := PageControl.Pages[tabindex].Hint;
      PageControl.ShowHint := true;
      Application.ProcessMessages; // force hint to appear
    finally Application.HintPause := hintPause; end;
  end;
end;

To hide the hint on the main page body, assign the following method to the page control's OnMouseLeave event.

procedure TForm.PageMouseLeave(Sender: TObject);
begin
  PageControl.Hint := '';
  PageControl.ShowHint := false;
end;

OTHER TIPS

In Raize Components, this can be accomplished by setting the trzpagecontrol.tabhints property to true. Good components can save you a lot of time (therefore money).

(just a happy customer, btw)

Update (in response to comment from @Rigel) from raize.com FAQ (Raize Components tab):

What happened to Raize Components?

Back in 2015 Embarcadero acquired Raize Components from us and rebranded the product as the Konopka Signature VCL Controls (KSVC). Initially they sold the product separately, but for the past several releases of RAD Studio, the components have been available for free through the GetIt Package Manager. Simply open the GetIt Package Manager from the Delphi or C++Builder Tools menu and search for “Konopka” to locate the installer. The component names, units, and packages are the same as they were in Raize Components, just the product name is different.

1 - fill in the .Hint property, and set the .ShowHint property to True for the PageControl (assuming each tabsheet has ParentShowHint set to true; otherwise you'll have to set each page individually).

2 - Assign this event to the PageControl's OnChange event handler:

procedure TForm1.PageControl1Change(Sender: TObject);
begin
  PageControl1.Hint := PageControl1.ActivePage.Hint;
end;

After you do that, the hint will be whatever the active tab is. I am not sure how to make it change the hint based on where the mouse is hovering - that's an interesting phenomenon I've never noticed before, actually.

On the tPageControl.OnMouseMove find TabIndex by Pgctrl.IndexOfTabAt( X, Y ) and assign TabSheet hint to the tPageControl hint

Look here:

http://www.delphigroups.info/2/9/321680.html

Originally working on a C++ Builder 6 (!) project (so please forgive any typo in this transcript), I started with the answer of Gerard[1] and reduced the code as much as possible. To better control the calls of Application.CancelHint, I introduced the member FLastHintTabIndex, it must be initialized with -1.

procedure TForm1.PageControl1MouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
var
  TabIndex: Integer;
begin
  TabIndex := PageControl1.IndexOfTabAt(X, Y);
  if FLastHintTabIndex <> TabIndex then
      Application.CancelHint;
  if TabIndex <> -1 then
      PageControl1.Hint = PageControl1.Pages[TabIndex].Hint;
  FLastHintTabIndex := TabIndex;
end;

[1] my answer doesn't contain much new, but I find all that code and text too distracting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top