gpt4 book ai didi

delphi - 如何在 TPanel 上绘图

转载 作者:行者123 更新时间:2023-12-03 15:15:11 25 4
gpt4 key购买 nike

我需要在 TPanel 上绘制,最好是直接绘制,这样我上面就不会再有其他组件妨碍 mouseevent 事件捕获(我想在上面绘制一些“大小握把”)。我应该怎样做呢?

最佳答案

要真正做到正确,您可能应该编写一个后代类。重写 Paint 方法来绘制大小调整夹点,并重写 MouseDownMouseUpMouseMove 方法来添加调整控件的功能大小。

我认为这是比尝试在应用程序代码中绘制 TPanel 更好的解决方案,原因如下:

  1. Canvas 属性在 TPanel 中受到保护,因此您无法从类外部访问它。您可以通过类型转换来解决这个问题,但这是作弊。
  2. “可调整大小”听起来更像是面板的功能,而不是应用程序的功能,因此请将其放入面板控件的代码中,而不是应用程序的主代码中。

这里有一些可以帮助您入门的内容:

type
TSizablePanel = class(TPanel)
private
FDragOrigin: TPoint;
FSizeRect: TRect;
protected
procedure Paint; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer); override;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer); override;
end;

procedure TSizeablePanel.Paint;
begin
inherited;
// Draw a sizing grip on the Canvas property
// There's a size-grip glyph in the Marlett font,
// so try the Canvas.TextOut method in combination
// with the Canvas.Font property.
end;

procedure TSizeablePanel.MouseDown;
begin
if (Button = mbLeft) and (Shift = [])
and PtInRect(FSizeRect, Point(X, Y)) then begin
FDragOrigin := Point(X, Y);
// Need to capture mouse events even if the mouse
// leaves the control. See also: ReleaseCapture.
SetCapture(Handle);
end else inherited;
end;

关于delphi - 如何在 TPanel 上绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/813693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com