如何画一个TPanel [英] How to draw on a TPanel

查看:218
本文介绍了如何画一个TPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要直接绘制一个TPanel,所以我没有其他的组件可以阻止鼠标事件捕获(我想在其上绘制一些大小抓地力​​)。我应该怎么做?

I need to draw on a TPanel, ideally directly so I don't have another component on top of it getting in the way of mousevent-event trapping (I want to draw a little "size-grip" on it). How should I go about doing this?

推荐答案

为了真正做到这一点,你应该写一个后代类。覆盖 Paint 方法来绘制尺寸抓取,并覆盖 MouseDown MouseUp MouseMove 方法来添加调整大小的功能到控件。

To really do it right, you should probably write a descendant class. Override the Paint method to draw the sizing grip, and override the MouseDown, MouseUp, and MouseMove methods to add resizing functionality to the control.

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

I think that's a better solution than trying to draw onto a TPanel in your application code for a couple of reasons:


  1. Canvas 属性在 TPanel 中受保护,因此您无法从课外访问它。你可以用类型转换来解决这个问题,但这是欺骗。

  2. 可重新性听起来更像是面板的功能,而不是应用程序的一个功能,所以把它放在代码中面板控件,不在应用程序的主要代码中。

  1. The Canvas property is protected in TPanel, so you have no access to it from outside the class. You can get around that with type-casting, but that's cheating.
  2. The "resizability" sounds more like a feature of the panel than a feature of the application, so put it in code for the panel control, not in your application's main code.

这里有一些让您开始的事情:

Here's something to get you started:

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;

这篇关于如何画一个TPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆