我自定义了一个图形控件(TmySelfCtrl),继承自TGraphicControl。完工后,在设计窗口拖拽控件到窗体,然后运行,一切正常。但是当我在运行时,动态添加本控件,却无法显示控件。
运行时,动态添加控件的代码如下:procedure TForm1.Button1Click(Sender: TObject);
var
  tmpCtrl:TmySelfCtrl;//我自定义的控件类
begin
  tmpCtrl := TmySelfCtrl.create(nil);
  tmpCtrl.Parent := Form1;
  tmpCtrl.Left := 20;
  tmpCtrl.Top := 20;
  tmpCtrl.Width := 100;
  tmpCtrl.Height := 50;
end;
运行时,添加 Button1 看不到控件。但是把控件换成系统自带的控件比如:TShape控件,就能正常显示。请问高手是怎么回事。

解决方案 »

  1.   

    tmpCtrl := TmySelfCtrl.create(Self); 
      

  2.   

    tmpCtrl := TmySelfCtrl.create(Self);
    tmpCtrl.Visible := True;
      

  3.   

    tmpCtrl := TmySelfCtrl.create(self);
    tmpCtrl.show;最好不要用TmySelfCtrl.create(nil);
      

  4.   

    不行呀,2、3、4楼的方法不行呀。因为同样的调用自带控件可以使用,我的不行。所以我怀疑是我的控件编写的某个部分有问题。所以大家想想会出在什么地方。我的控件在设计时,拖到界面上,然后运行程序,没有任何问题。其实我的控件就是模仿VB的line控件,做得一个控件。用于再两个元素之间建立连线。原理就是根据用户输入的坐标值,在其间画一条线段。给点建议吧。
      

  5.   


    ls的方法应该可以的,既然lz怀疑代码问题,就贴出代码吧!
      

  6.   

    简化的代码如下,请指教unit zjfLine;interfaceuses
      SysUtils, Classes, Controls,Graphics;type
      TzjfLine = class(TGraphicControl)
      private
        lineX1,lineX2,lineY1,lineY2:integer;
        linecolor:Tcolor;
        linewidth:integer;
        procedure SetX1(const Value: integer);
        procedure SetX2(const Value: integer);
        procedure SetY1(const Value: integer);
        procedure SetY2(const Value: integer);
        procedure SetColor(const Value: Tcolor);
        procedure SetWidth(const Value: Integer);
        { Private declarations }
      protected
        { Protected declarations }
        procedure Paint;override;
      public
        { Public declarations }
        constructor Create(AOwner:Tcomponent);override;
        
      published
        { Published declarations }
        property OnMouseDown;
        property OnMouseUp;
        property OnMouseMove;
        property lnWidth:Integer read lineWidth write SetWidth;
        property lnColor:Tcolor read lineColor write SetColor;
        property X1:integer read lineX1 write SetX1;
        property X2:integer read lineX2 write SetX2;
        property Y1:integer read lineY1 write SetY1;
        property Y2:integer read lineY2 write SetY2;
      end;procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('ZJF', [TzjfLine]);
    end;{ TzjfLine }constructor TzjfLine.Create(AOwner: Tcomponent);
    begin
      inherited Create(AOwner);
      X1 := 10;
      Y1 := 10;
      X2 := 40;
      Y2 := 40;
      linewidth := 1;
      linecolor := clBlack;
    end;
    procedure TzjfLine.Paint;
    begin
      //inherited;
      Canvas.Pen.Width := lineWidth;
      Canvas.Pen.Color := lineColor;
      if lineX1 < lineX2 then
      begin
        If lineY1 < lineY2 then
        begin
          Left := lineX1;
          Top := lineY1;
          Width := lineX2-lineX1;
          Height := lineY2 - lineY1;
          Canvas.MoveTo(0,0);
          Canvas.LineTo(Width-1,Height-1);
        end
        else if lineY1=lineY2 then
        begin
          Left := lineX1;
          Top := lineY1;
          Width := lineX2 - lineX1;
          Height := canvas.Pen.Width;
          Canvas.MoveTo(0,0);
          Canvas.LineTo(width,0);
        end
        else
        begin
          Left := lineX1;
          Top := lineY2;
          width := lineX2 - lineX1;
          Height := lineY1 - lineY2;
          Canvas.MoveTo(0,height-1);
          Canvas.LineTo(width-1,0);
        end;
      end
      else if lineX1=lineX2 then
      begin
        if lineY1 < lineY2 then
        begin
          Left := lineX1;
          Top := lineY1;
          Width := canvas.Pen.Width;
          height := lineY2 - lineY1;
          Canvas.MoveTo(0,0);
          Canvas.LineTo(0,height);
        end
        else if lineY1 = lineY2 then
        begin
          Left := lineX1;
          Top := lineY1;
          width := Canvas.Pen.Width;
          height := Canvas.Pen.Width;
          Canvas.MoveTo(0,0);
          Canvas.LineTo(width,height);
        end
        else
        begin
          Left := lineX1;
          Top := lineY2;
          width := Canvas.Pen.Width;
          height := lineY2 - lineY1;
          Canvas.MoveTo(0,0);
          Canvas.LineTo(0,height);
        end;
      end
      else
      begin
        If lineY1 < lineY2 then
        begin
          Left := lineX2;
          Top := lineY1;
          Width := lineX1-lineX2;
          height := lineY2 -lineY1;
          Canvas.MoveTo(width-1,0);
          Canvas.LineTo(0,height-1);
        end
        else if lineY1=lineY2 then
        begin
          Left := lineX2;
          Top := lineY1;
          height := Canvas.Pen.width;
          width := lineX1 - lineX2;
          Canvas.MoveTo(0,0);
          Canvas.LineTo(width,0);
        end
        else
        begin
          Left := lineX2;
          Top := lineY2;
          Width := lineX1-lineX2;
          height := lineY1 - lineY2;
          Canvas.MoveTo(0,0);
          Canvas.LineTo(width-1,height-1);
        end;
      end;
    end;procedure TzjfLine.SetColor(const Value: Tcolor);
    begin
      lineColor := Value;
      Invalidate;
    end;procedure TzjfLine.SetWidth(const Value: Integer);
    begin
      lineWidth := Value;
      Invalidate;
    end;procedure TzjfLine.SetX1(const Value: integer);
    begin
      lineX1 := Value;
      Invalidate;
    end;procedure TzjfLine.SetX2(const Value: integer);
    begin
      lineX2 := Value;
      Invalidate;
    end;procedure TzjfLine.SetY1(const Value: integer);
    begin
      lineY1 := Value;
      Invalidate;
    end;procedure TzjfLine.SetY2(const Value: integer);
    begin
      lineY2 := value;
      Invalidate;
    end;end.
      

  7.   

    谢谢sanguomi的线下指教,问题解决了。是因为没有初始化控件宽度和高度的问题,非常感谢 sanguomi 也希望其他朋友从我的案例中有所心得。结贴!