2008. február 12., kedd

Catch the TPageControl.HotTrack event


Problem/Question/Abstract:

When the HotTrack property of a TPageControl is True the tabsheet captions light blue for example when the mouse hovers over them. How can I display the TabSheets hint when this event occurs (the TabSheet hint should be displayed only when the mouse hovers over the TabSheet caption)?

Answer:

Use the pagecontrol's OnMouseMove event:

{tabindex may be <> pageindex if some pages have tabvisible = false!}

function FindPageforTabIndex(pagecontrol: TPageControl; tabindex: Integer): TTabSheet;
var
  i: Integer;
begin
  Assert(Assigned(pagecontrol));
  Assert((tabindex >= 0) and (tabindex < pagecontrol.pagecount));
  Result := nil;
  for i := 0 to pagecontrol.pagecount - 1 do
    if pagecontrol.pages[i].tabVisible then
    begin
      Dec(tabindex);
      if tabindex < 0 then
      begin
        result := pagecontrol.pages[i];
        break;
      end;
    end;
end;

function HintForTab(pc: TPageControl; tabindex: Integer): string;
var
  tabsheet: TTabsheet;
begin
  tabsheet := FindPageforTabIndex(pc, tabindex);
  if assigned(tabsheet) then
    result := tabsheet.hint
  else
    result := '';
end;

procedure TForm1.PageControl1MouseMove(Sender: TObject; Shift: TShiftState; X, Y:
  Integer);
var
  tabindex: Integer;
  pc: TPageControl;
  newhint: string;
begin
  pc := Sender as TPageControl;
  tabindex := pc.IndexOfTabAt(X, Y);
  if tabindex >= 0 then
  begin
    newhint := HintForTab(pc, tabindex);
    if newhint <> pc.Hint then
    begin
      pc.Hint := newhint;
      application.CancelHint;
    end;
  end
  else
    pc.Hint := '';
end;

{Attach this to every tabsheets OnMouseMove event}

procedure TForm1.TabSheetMouseMove(Sender: TObject; Shift: TShiftState; X, Y:
  Integer);
begin
  pagecontrol1.Hint := '';
end;

Nincsenek megjegyzések:

Megjegyzés küldése