我需要在TPanel上绘制,理想情况下是直接的,所以我没有其他组件可以阻止鼠标事件捕获(我想在它上面画一点“size-grip”) 。我应该怎么做呢?

有帮助吗?

解决方案

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

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

  1. Canvas 属性受 TPanel 保护,因此您无法从类外部访问它。你可以通过打字来解决这个问题,但这是作弊。
  2. “可恢复性”听起来更像是面板的一个功能而不是应用程序的一个功能,所以把它放在面板控件的代码中,而不是应用程序的主代码中。
  3. 这是让你入门的东西:

    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;
    

其他提示

这是 Raize Components 可以让您的生活更轻松的众多方法之一。我只是进入Delphi,放在TRzPanel上,输入:

  

... RzPanel1.Canvas.Rectangle

我确信还有其他解决方案 - 但我不需要用Raize来寻找它们。

(只是满意的客户约10年......)

编辑:鉴于你的目标,以及你已经拥有Raize Components的声明,我还应该指出TRzSizePanel处理面板的大小调整和OnCanResize等有用的事件(以确定是否要允许调整大小到特定的新宽度)或高度)。

最简单的方法是在面板上放置一个TImage。但是如果你真的不想这样做,可以在代码编辑器中输入TCanvas,点击F1,然后有趣地了解它是如何工作的。 (不要说我没有警告你......)

如何在运行时调整大小的控件添加大小句柄: http://delphi.about.com/library/weekly/aa110105a.htm

TAdvPanel: http://www.tmssoftware.com/site/advpanel.asp

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top