Pergunta

TAction has a OnHint event, but unfortunately not a OnHideHint event. This has the following drawback: I have ToolButtons and other controls associated with actions. Whenever the mouse is over such a control, the hint of the Action is shown in the StatusBar; (I have set the StatusBar's AutoHint property to True). But when the mouse leaves the ToolButton, the previous custom text in the StatusBar (which is not from a hint) is NOT automatically restored! Now I could write an OnMouseLeave event handler for each and every control on the form to restore my custom text in the StatusBar, but this would be cumbersome! Isn't there something which automatically restores the previous text in the StatusBar? An OnHideHint event in TAction would be ideal!

Foi útil?

Solução

That is default behavior, When AutoHint is True, the status bar automatically responds to hint actions by displaying the long version of the hint's text in the first panel.

The issue that you are having is that when you leave a control with your mouse, you are basically entering another window, it's parent control. And because that parent has no Hint string value assigned to it, the HintAction is updated to an empty string.

If you want to return the default value when there is no hint to display then drop a TApplicationEvents component on the form and use the TApplication.OnHint event like this:

var
  OriginalPanelText : String = 'BLA';

procedure TForm1.ApplicationEvents1Hint(Sender: TObject);
begin

   if StatusBar1.SimplePanel or (StatusBar1.Panels.Count = 0)
  then
    if Application.Hint <> ''
    then
       StatusBar1.SimpleText := Application.Hint
    else
       StatusBar1.SimpleText := OriginalPanelText
  else
    if Application.Hint <> ''
    then
      StatusBar1.Panels[0].Text := Application.Hint
    else
      StatusBar1.Panels[0].Text := OriginalPanelText;

end;

Outras dicas

The AutoHint magic all happens in TStatusBar.ExecuteAction. When the hint stops showing that code sets the status bar text to be empty. You could modify the behaviour like this:

type
  TStatusBar = class(ComCtrls.TStatusBar)
  private
    FRestoreTextAfterHintAction: string;
  public
    function ExecuteAction(Action: TBasicAction): Boolean; override;
  end;

function TStatusBar.ExecuteAction(Action: TBasicAction): Boolean;
var
  HintText: string;
begin
  if AutoHint and not (csDesigning in ComponentState) and
     (Action is THintAction) and not DoHint then begin
    HintText := THintAction(Action).Hint;
    if SimplePanel or (Panels.Count=0) then begin
      if HintText='' then begin
        SimpleText := FRestoreTextAfterHintAction;
      end else begin
        FRestoreTextAfterHintAction := SimpleText;
        SimpleText := HintText;
      end;
    end else begin
      if HintText='' then begin
        Panels[0].Text := FRestoreTextAfterHintAction;
      end else begin
        FRestoreTextAfterHintAction := Panels[0].Text;
        Panels[0].Text := HintText;
      end;
    end;
    Result := True;
  end else begin
    Result := inherited ExecuteAction(Action);
  end;
end;

I've used a rather crude interposer class and a brittle instance variable to store the text to be restored. You could tart this up to be a little more robust if you wish. The code above at least shows you the place you need to add your hooks.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top