我需要在TPanel上绘制,理想情况下是直接的,所以我没有其他组件可以阻止鼠标事件陷阱(我想在它上面画一点“尺寸 – 抓握”).我该怎么做呢? 要真正做到对,你应该写一个后代类.重写
我认为这比尝试在应用程序代码中绘制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;