delphi 如何使TBalloonHint始终向下显示,而不受屏幕位置的影响?

ttisahbt  于 2022-11-04  发布在  其他
关注(0)|答案(2)|浏览(195)

创建一个VCL表单应用程序,将一个TBalloonHint(名称:balloonhint测试)和一个TButton(名称:btnTest)并编写以下代码:

procedure TForm2.FormCreate(Sender: TObject);
begin
  balloonhintTest.HideHint;
  balloonhintTest.Style := bhsStandard;
end;

procedure TForm2.btnTestMouseEnter(Sender: TObject);
begin
  if not balloonhintTest.ShowingHint then
  begin
    balloonhintTest.Title := 'My Title';
    balloonhintTest.Description := 'MyDescription';
    balloonhintTest.ShowHint(Self.ClientToScreen(Point(btnTest.Left + (btnTest.Width div 2), btnTest.Top + btnTest.Height)));
  end;
end;

procedure TForm2.btnTestMouseLeave(Sender: TObject);
begin
  balloonhintTest.HideHint;
end;

现在运行程序并将鼠标指针悬停在按钮上。
这是窗口位于屏幕上部时的外观:

这是窗口位于屏幕下部时的外观:

如您所见-尽管提示坐标始终相同-提示在第一种情况下显示为“向下”(所需结果),在第二种情况下显示为“向上”(显然不是所需结果),这取决于窗口在屏幕上的垂直位置。

在这种情况下,我如何显示气球提示,使其始终与屏幕位置无关?

(请注意:我对ShowHint方法的其他重载不感兴趣--我只想知道在上面的例子中如何总是向下显示提示,因为这只是一个更复杂例子的简化场景)。

roejwanj

roejwanj1#

可能最简单的方法是基于TBalloonHint创建您自己的类,

type
  TMyHint = class(TBalloonHint)
  strict private
    FControl: TControl;
  public
    procedure PaintHint(HintWindow: TCustomHintWindow); override;
    constructor Create(AOwner: TComponent; const AControl: TControl);
  end;

constructor TMyHint.Create(AOwner: TComponent; const AControl: TControl);
begin
  inherited Create(AOwner);
  FControl := AControl;
end;

procedure TMyHint.PaintHint(HintWindow: TCustomHintWindow);
var
  Point: TPoint;
begin
  Point := FControl.Parent.ClientToScreen(TPoint.Create(FControl.Left, FControl.Top + FControl.Height));
  HintWindow.Top := Point.Y;

  inherited;
end;

将其创建为

procedure TMainForm.FormCreate(Sender: TObject);
begin
  balloonHintTest := TMyHint.Create(Self, btnTest);
  balloonHintTest.Style := bhsStandard;
end;
wtlkbnrh

wtlkbnrh2#

我认为如果你只检查显示点是否在balloonhint所属的表单的下半部分,并且如果是的话,添加诸如此类的东西,这会更容易。balloonHint的高度可以通过textHeight计算出来

procedure TForm2.FormCreate(Sender: TObject);
    begin
      balloonhintTest.HideHint;
      balloonhintTest.Style := bhsStandard;
    end;

    procedure TForm2.btnTestMouseEnter(Sender: TObject);
    begin
      if not balloonhintTest.ShowingHint then
      begin
        balloonhintTest.Title := 'My Title';
        balloonhintTest.Description := 'MyDescription';
        TPoint pointCheck = Self.ClientToScreen(Point(btnTest.Left + (btnTest.Width div 2), btnTest.Top + btnTest.Height)); 
        if(pointCheck.y>0.5*Self.Height)
            int yShift = balloonhintTest.Height; 
            pointCheck.y = pointCheck.y - yShift;  
        end if 
        balloonhintTest.ShowHint(pointCheck);
      end;
    end;

    procedure TForm2.btnTestMouseLeave(Sender: TObject);
    begin
      balloonhintTest.HideHint;
    end;

相关问题