代码之家  ›  专栏  ›  技术社区  ›  Jamo

如何在t面板上绘制

  •  3
  • Jamo  · 技术社区  · 16 年前

    我需要画一个Tpanel,最好是直接画,这样我就不会在上面有其他的组件妨碍鼠标事件捕获(我想在上面画一个小的“大小夹点”)。我该怎么做呢?

    4 回复  |  直到 8 年前
        1
  •  9
  •   Rob Kennedy    16 年前

    要真正做到这一点,您应该编写一个后代类。重写 Paint 方法绘制大小调整手柄,并重写 MouseDown , MouseUp MouseMove 方法向控件添加调整大小功能。

    我认为这比试图利用 TPanel 在应用程序代码中,有以下几个原因:

    1. 这个 Canvas 在中保护属性 面板 ,因此您无法从类外访问它。你可以通过打字来解决这个问题,但那是作弊。
    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;
    
        2
  •  7
  •   Argalatyr Rodrigo    16 年前

    这是许多方法之一 Raize Components 可以让你的生活更轻松。我刚进入Delphi,打开一个TrzPanel,输入:

    rzpanel1.canvas.rectangle…

    我相信还有其他的解决方案-但我不必用raize来寻找它们。

    (仅仅是一个满意的客户大约10年…)

    编辑:考虑到您的目标,以及您已经有raize组件的声明,我还应该指出trzsizepanel处理面板的大小调整和一些有用的事件,如oncanresize(确定是否允许将大小调整到特定的新宽度或高度)。

        3
  •  4
  •   Mason Wheeler    16 年前

    最简单的方法就是在面板上加一个时间。但是如果你真的不想这样做,可以在代码编辑器中输入tcanvas,点击f1,在引擎盖下学习它是如何工作的。(不要说我没有警告你…)

        4
  •  2
  •   Bob S    16 年前

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

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