初学编写组件,因为image控件的父类中没有caption属性,如何将这个属性加进去呢

解决方案 »

  1.   

    type
       Tcustomimg=class(TImage);
       private
         FCaption:String;
         procedure setcaption(value:String);
       published
         property Caption:String write setCaption read Fcaption;  //--增加属性
       end;
    ...
    procedure TCustomImg.setcaption(value:String);
    begin
       Fcaption:=value;
    end;
    ....
      

  2.   

    to  Sorder(剑客) :
    我试过,编译通不过
      

  3.   

    就是自己写一个类,从Timage继承一下
    然后自己加上caption的属性就可以了
     Sorder(剑客)应该就满足条件了等我给你写一个看看
      

  4.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls;type
      TForm1 = class(TForm)
        Image1: TImage;
      private
        { Private declarations }
      public
        { Public declarations }
      end;
      TNewImage = class(TImage)
      private
        fcaption : string;
        procedure setcaption(captionstring:string);
      published
        property  caption : string read fcaption write setcaption;
      end;
    var
      Form1: TForm1;
      image1:TNewImage;implementation{$R *.dfm}
    procedure TNewImage.setcaption(captionString:string);
    begin
        fcaption:=captionString;
    end;end.
    ------------------------------------------------------------
    delphi7下面编译通过了
      

  5.   

    to heluqing(鉴之小河) 
    编译是可以通过,但是实际使用控件时,输入caption属性值并不能在控件上显示。这是为什么呢?
      

  6.   

    哦,明白了!
    我刚才写的那个是在image上添加了一个string的属性
    那个东西只能保存string类的数据但是不能显示
    如果你想要显示的话
    就在public里面定义一个Tlabel的属性
    然后写一个setcaptionlabel1.caption:=captionstring;就可以了
      

  7.   

    谢谢各位,但是这好像解决不了label重画时闪烁的问题^_^
      

  8.   

    晕,揭帖到挺快的这样就行了!type
      Taa=class(TImage)
        private
        fcaption : string;
        procedure setcaption(captionstring:string);
      protected
        procedure Paint; override;
      published
        property  caption : string read fcaption write setcaption;
      end;  TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
       Image1:Taa;  public
        { Public declarations }
      end;
    var
      Form1: TForm1;implementation{$R *.dfm}{ Taa }procedure Taa.setcaption(captionstring: string);
    begin
     fcaption:=captionString;
    end;
    procedure Taa.Paint;
    begin
      inherited;
      Canvas.Brush.Style:=bsClear;
      Canvas.TextOut(0,0,self.caption);
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
     Image1:=Taa.Create(self);
     Image1.Parent:=self;
     Image1.caption:='asdasdasd';
     Image1.Visible:=True;
     image1.Picture.LoadFromFile('D:\ShippingOrder\bmp\050.bmp');
     DoubleBuffered:=true;
    end;
      

  9.   

    to  chijingde() :
    天啊,如果我一个界面上有多个image,那启不是每个都要这样写,不然的话就闪烁得不行?
      

  10.   

    晕阿你不是从TImage类继承下来的吗?所有用你自己的类创建的实例都会有这个特性阿至于闪烁只要把父容器的DoubleBuffered双缓冲打开就行了
      

  11.   

    to chijingde() 
    不好意思,因为你的设置是在FormCreate里实现的。我想知道如何把他写到组件中直接应用就可以呢?