[ACCEPTED]-How to draw on a TPanel-tpanel

Accepted answer
Score: 10

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

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

  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 1 to get you started:

  TSizablePanel = class(TPanel)
    FDragOrigin: TPoint;
    FSizeRect: TRect;
    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;

procedure TSizeablePanel.Paint;
  // 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.

procedure TSizeablePanel.MouseDown;
  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.
  end else inherited;
Score: 7

This is one of the many many ways that Raize Components can 11 make your life easier. I just go into Delphi, drop 10 on a TRzPanel, and type:


I'm 9 sure there are other solutions - but I don't 8 have to look for them with Raize.

(just a 7 satisfied customer for about 10 years...)

EDIT: Given 6 your goal, and your statement that you have 5 Raize Components already, I should also 4 point out that TRzSizePanel handles resizing 3 of the panel and useful events like OnCanResize 2 (to determine whether you want to allow 1 resizing to a particular new width or height).

Score: 4

The simplest way to do it is to just put 5 a TImage on the panel. But if you really 4 don't want to do that, type TCanvas into 3 the code editor, hit F1, and have fun learning 2 about how it works under the hood. (Don't 1 say I didn't warn you...)

Score: 2

How to Add Size Handles to Controls being 1 Resized at Run-Time: http://delphi.about.com/library/weekly/aa110105a.htm

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

More Related questions