做一个图形设计软件,希望在运行的时候类似于delphi7那样,设计控件的时候可以修改,拖动,删除,有object inspector,设计完成后“运行”,不可以编辑
这该怎么实现?
还有要用到RTTI么,如果是的话,是不是通过在不同的窗体之间传递消息

解决方案 »

  1.   

    可以学它的,不过一般你可以在它的层次之上来抽象,譬如定义ITypeInfo接口,让你的图形都实现这个接口从而达到动态获取图形属性的目的。
    当然,你的图形如果就是Delphi中的控件,你可以直接使用它内置的RTTI。
      

  2.   


    你说的我不能全部理解,可否说的具体一些
    很多控件需要自己写,主要是一些图形对象,例如圆,椭圆,矩形,直线等等。
    以前都是在一个form内调用过程去画的,那样修改起来要麻烦很多,现在希望每种图形做成控件希望有经验的兄弟们帮帮忙啊
      

  3.   

    unit SampleShape;interfaceuses
      SysUtils, Graphics, Classes, Controls;type
      TSampleShapeType = (ssLine, sstRectangle, sstSquare, sstRoundRect, sstRoundSquare,
      sstEllipse, sstCircle);
      TSampleShape = class(TGraphicControl)
      private
        FShape: TSampleShapeType; { field to hold property value }
        FPen: TPen; {pen 对象域}
        FBrush: TBrush; {brush 对象域}    
        procedure SetShape(Value: TSampleShapeType);
        procedure SetBrush(Value: TBrush);
        procedure SetPen(Value: TPen);
        { Private declarations }
      protected
        procedure Paint; override;
        { Protected declarations }
      public
        constructor Create(AOwner: TComponent); override; {记住override 指令}
        //destructor Destroy; override;
        destructor Destroy; override; {记住override 指令}
        { Public declarations }
      published
        property Shape: TSampleShapeType read FShape write SetShape;
        property Brush: TBrush read FBrush write SetBrush;
        property Pen: TPen read FPen write SetPen;
        procedure StyleChanged(Sender: TObject);
        property Height default 65;
        property Width default 65;
        property DragCursor; {拖放属性}
        property DragMode;
        property OnDragDrop; {拖放事件}
        property OnDragOver;
        property OnEndDrag;
        property OnMouseDown; {鼠标事件}
        property OnMouseMove;
        property OnMouseUp;
        { Published declarations }
      end;procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('Samples', [TSampleShape]);
    end;{ TSampleShape }constructor TSampleShape.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner); {首先必须调用继承的构造方法}
      Width := 65;
      Height := 65;
      FPen := TPen.Create; {构造Pen}
      FBrush := TBrush.Create; {构造Brush}
      FBrush.OnChange := StyleChanged; {指定OnChange 事件的方法}
    end;procedure TSampleShape.SetBrush(Value: TBrush);
    begin
      FBrush.Assign(Value); {使用参数替换现存的Brush}
    end;procedure TSampleShape.SetPen(Value: TPen);
    begin
      FPen.Assign(Value);
    end;procedure TSampleShape.SetShape(Value: TSampleShapeType);
    begin
      if FShape <> Value then {如果不变则忽略}
      begin
        FShape := Value; {存储新值}
        Invalidate; {强制重新绘制形状}
      end;
    end;
    destructor TSampleShape.Destroy;
    begin
      FPen.Free; {销毁Pen 对象}
      FBrush.Free; {销毁Brush 对象}
      inherited Destroy; {必须调用继承的析构方法}
    end;procedure TSampleShape.StyleChanged(Sender: TObject);
    begin
      Invalidate; {删除并重画组件}
    end;procedure TSampleShape.Paint;
    var
      X, Y, W, H, S: Integer;
    begin
      inherited;
      with Canvas do
      begin
        Pen := FPen; {拷贝组件的Pen }
        Brush := FBrush; {拷贝组件的Brush }
        W := Width; {使用组件宽度}
        H := Height; {使用组件高度}
        if W < H then S := W else S := H; {保存最短的边}
          case FShape of {调整高度、宽度和位置}
            ssLine, sstRectangle, sstRoundRect, sstEllipse:
            begin
              X := 0; {原始位置是图形左上}
              Y := 0;
            end;
            sstSquare, sstRoundSquare, sstCircle:
            begin
              X := (W - S) div 2; {水平方向处于中心}
              Y := (H - S) div 2; {然后是垂直方向}
              W := S; {宽度使用最小值}
              H := S; {高度也是}
            end;
      end;
      case FShape of
        //ssLine:
          //MoveTo(0,0);
          //LineTo(50, 50);
        sstRectangle, sstSquare:
          Rectangle(X, Y, X + W, Y + H);
        sstRoundRect, sstRoundSquare:
          RoundRect(X, Y, X + W, Y + H, S div 4, S div 4);
        sstCircle, sstEllipse:
          Ellipse(X, Y, X + W, Y + H);
      end;
    end;
    end;end.画直线我也不会