加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 如何在TPanel上绘制

发布时间:2020-12-15 09:10:56 所属栏目:大数据 来源:网络整理
导读:我需要在TPanel上绘制,理想情况下是直接的,所以我没有其他组件可以阻止鼠标事件陷阱(我想在它上面画一点“尺寸 – 抓握”).我该怎么做呢? 解决方法 要真正做到对,你应该写一个后代类.重写Paint方法以绘制大小调整手柄,并覆盖MouseDown,MouseUp和MouseMove方
我需要在TPanel上绘制,理想情况下是直接的,所以我没有其他组件可以阻止鼠标事件陷阱(我想在它上面画一点“尺寸 – 抓握”).我该怎么做呢?

解决方法

要真正做到对,你应该写一个后代类.重写Paint方法以绘制大小调整手柄,并覆盖MouseDown,MouseUp和MouseMove方法以向控件添加调整大小功能.

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

> Canvas属性在TPanel中受到保护,因此您无法从类外部访问它.你可以用类型转换来解决这个问题,但这是作弊行为.
>“可恢复性”听起来更像是面板的一个功能,而不是应用程序的一个功能,因此将其放在面板控件的代码中,而不是应用程序的主代码中.

这是让你入门的东西:

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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读