unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls;type
  TAnalysisControl = class(TCustomControl)
    public
      procedure MouseDown(Button : TMouseButton; Shift : TShiftState; X,Y : integer); override;
      procedure MouseMove(Shift : TShiftState; X,Y : Integer); override;
      procedure Paint; override;
  end;  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;const
  sizeBorder        = 2;  sc_SizeLeft       = $F001;  { these are the variations on the }
  sc_SizeRight      = $F002;  { SC_SIZE value }
  sc_SizeTop        = $F003;
  sc_SizeTopLeft    = $F004;
  sc_SizeTopRight   = $F005;
  sc_SizeBottom     = $F006;
  sc_SizeBottomRight = $F008;
  sc_SizeBottomLeft = $F007;
  sc_DragMove       = $F012;implementationuses Winprocs;
{$R *.dfm}{ TAnalysisControl }procedure TAnalysisControl.MouseDown(Button: TMouseButton;
  Shift: TShiftState; X, Y: integer);
begin
  if Button = mbLeft then
    begin
      ReleaseCapture;      if (X >= Width - sizeBorder) And NOT((Y <= sizeBorder) or (Y >= Height - sizeBorder)) then
        Self.Perform(WM_SysCommand,sc_SizeRight,0)
      else
      if Not((X <= sizeBorder) or (X >= Width - sizeBorder)) And (Y <= sizeBorder) then
        Self.Perform(WM_SysCommand,sc_SizeTop,0)
      else
      if (X <= sizeBorder) And (Y <= sizeBorder) then
        Self.Perform(WM_SysCommand,sc_SizeTopLeft,0)
      else
      if (X >= Width - sizeBorder) and (Y <= sizeBorder) then
        Self.Perform( WM_SysCommand, sc_SizeTopRight , 0 )
      else
      if Not((X <= sizeBorder) or (X >= Width - sizeBorder)) And (Y >= Height - sizeBorder) then
        Self.Perform(WM_SysCommand,sc_SizeBottom,0)
      else
      if (Y >= Height - sizeBorder) And (X <= sizeBorder) then
        Self.Perform(WM_SysCommand,sc_SizeBottomLeft,0)
      else
      if (Y >= Height - sizeBorder) and (X >= Width - sizeBorder) then
       Self.Perform(WM_SysCommand, sc_SizeBottomRight, 0)
      else
      if Not((Y <= sizeBorder) or (Y >= Height - sizeBorder)) And (X <= sizeBorder) then
       Self.Perform(WM_SysCommand,sc_SizeLeft,0)
      else
        begin
          Self.Perform(WM_SysCommand,SC_DragMove,0);
        end;  end;end;procedure TAnalysisControl.MouseMove(Shift: TShiftState; X, Y: Integer);
begin
  if (X <= sizeBorder) or (X >= Width - sizeBorder) then
    begin
      if (Y >= Height - sizeBorder) then
        begin
          if (X >= Width - sizeBorder) then
            Cursor := crSizeNWSE
          else
            Cursor := crSizeNESW;
        end
      else
        if (Y <= sizeBorder) then
          begin
            if (X >= Width - sizeBorder) then
              Cursor := crSizeNESW
            else
              Cursor := crSizeNWSE;
          end
        else
          Cursor := crSizeWE;
    end
  else
    if (Y <= sizeBorder) or (Y >= Height - sizeBorder) then
      begin
        Cursor := crSizeNS;
      end
    else
      Cursor := crDefault;end;procedure TAnalysisControl.Paint;
var
  Rect: TRect;
  info: string;
begin
  Info := '测试程序';
  With Canvas do
    begin
      Brush.Color := clWhite;
      Brush.Style := bsSolid;
      Rect := Self.GetClientRect;
      FillRect(Rect);
      //如何绘制居中的文字
      OffSetRect(Rect, 0, 4);
      DrawText(GetDc(Self.Handle), PChar(Info), Length(Info), Rect, DT_Center);
    end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
  With TAnalysisControl.Create(Self) do
  begin
    Parent := Self;
    Left := 20;
    Top := 20;
    Height := 90;
    Width := 90;
  end;
end;end.当我把继承关系修改为TAnalysisControl = class(TCustomGraphic)后,
为什么新的控件就不支持移动和改变大小操作了,请大家帮帮忙,看看是什么原因

解决方案 »

  1.   

    只有从TWinControl继承的组件才接受WM_SysCommand消息,所以对TCustomGraphic无效。
      

  2.   

    那么,我如果需要实现可以运行时动态调整大小和位置的图形控件(继承自TCustomGraphic)该如何实现呢?
      

  3.   

    那就自己写代码来调整大小和位置,也是在MouseDown和MouseMove事件里,我这里有个调整位置的例子(Label):
    var oldPT: TPoint;
    procedure TForm1.Label1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      GetCursorPos(oldPT);
    end;procedure TForm1.Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    var
      pt: TPoint;
    begin
      GetCursorPos(pt);
      if ssLeft in Shift then begin
        Label1.Left := Label1.Left + pt.X - oldPT.X;
        Label1.Top := Label1.Top + pt.Y - oldPT.Y;
      end;
      oldPT := pt;
    end;
      

  4.   

    Left和Top为调整位置,那么Height和Width不就是调整控件大小的参数吗?
      

  5.   

    重载SetBounds方法会更好一些。
      

  6.   

    有这样的控件。TSTRETCHHANDLES
    去GOOGLE里找一个就是了