constructor TMySpeedButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Caption := Name;
end;

解决方案 »

  1.   

    to hellion(恶人):目前的情况是每个控件的caption属性都是空,与TspeedButton类控件一样,以下是我的代码,帮忙找找bug.
    unit RunButton;
    interface
    uses
      Windows, Messages, SysUtils,shellapi, Classes, Graphics, Controls, Forms, Dialogs,
      Buttons;
    type
      TcommandLine=type string;
      TRunButton = class(TSpeedButton)
      private
        Fcaption:Tcaption;
        FcommandLine:TcommandLine;
        procedure SetCommandLine(value:TcommandLine);
        procedure setcaption(value:Tcaption);
        { Private declarations }
      protected
        { Protected declarations }
      public
        constructor create(Aowner:Tcomponent);override;
        procedure Click;override;
        { Public declarations }
      published
        property Caption:Tcaption read Fcaption write setcaption;
        property CommandLine:TcommandLine read FcommandLine write SetCommandLine;
        { Published declarations }
      end;
    procedure Register;
    implementation
    procedure Register;
    begin
      RegisterComponents('Samples', [TRunButton]);
    end;
      procedure TrunButton.setcaption(value:Tcaption);
         begin
           inherited;
           Fcaption:=value;
           repaint;
         end;
      constructor TrunButton.create(Aowner:Tcomponent);
         begin
           inherited create(Aowner);
           setcaption(Name);
           width:=45;
           height:=45;
         end;
      Function IsExecutableFile(value:TcommandLine):boolean;
        begin
          result:=(uppercase(ExtractFileExt(value))='.EXE');
        end;
      procedure Trunbutton.SetCommandLine(value:TcommandLine);
        var
         Icon:Ticon;
        begin
          if not IsExecutableFile(value) then raise exception.Create(value+'不是一个可执行文件!');
          if not FileExists(value) then raise exception.Create(value+'文件不存在!');
          FcommandLine:=value;
          Icon:=Ticon.Create;
          try
             icon.Handle:=ExtractIcon(hinstance,Pchar(FcommandLine),0);
             with Glyph do
               begin
                 canvas.brush.style:=bssolid;
                 canvas.FillRect(canvas.cliprect);
                 width:=icon.Width;
                 height:=Icon.Height;
                 canvas.Draw(0,0,icon);
               end;
          finally
              icon.Free;
          end;    
        end;
      function processexecute(commandline:TcommandLine;cshow:word):integer;
         var
           rslt:longbool;
           startupinfo:Tstartupinfo;
           processinfo:Tprocessinformation;
        begin
           fillchar(startupinfo,sizeof(Tstartupinfo),#0);
           with startupinfo do
             begin
               cb:=sizeof(Tstartupinfo);
               dwflags:=startf_useshowwindow or startf_forceonfeedback;
               wshowwindow:=cshow;
             end;
           rslt:=createprocess(pchar(commandline),nil,nil,nil,false,normal_priority_class,nil,
             nil,startupinfo,processinfo);
           if rslt then
               with processinfo do
                 begin
                   waitforinputidle(hprocess,infinite);
                   closehandle(hthread);
                   closehandle(hprocess);
                   result:=0;
                 end
           else
                result:=getlasterror;
        end;
      procedure Trunbutton.Click;
         var
           rean:word;
        begin
           inherited click;
           rean:=ProcessExecute(FcommandLine,sw_shownormal);
           if rean<>0 then
              raise exception.Create('错误地执行了程序:'+inttostr(rean));
        end;
    end.
      

  2.   

      protected
        procedure SetParent(AParent: TWinControl); override;
    procedure TMySpeedButton.SetParent(AParent: TWinControl);
    begin
      inherited;
      if AParent <> nil then
        Caption := Name;
    end;
      

  3.   

    楼上,你好,问题解决了一半.
    目前在object inspector中,caption值已经可以根据控件的加入而变化,但在控件上不显示caption值,如何处理?please help.
      

  4.   

    问题解决,全UNIT如下:unit runbutton;
    interface
    uses
      Windows, Messages, SysUtils,shellapi, Classes, Graphics, Controls, Forms, Dialogs,
      Buttons;
    type
      TcommandLine=type string;
      TRunButton = class(TSpeedButton)
      private
        Fcaption:Tcaption;
        FcommandLine:TcommandLine;
        procedure SetCommandLine(value:TcommandLine);
        procedure setcaption(value:Tcaption);
        { Private declarations }
      protected
       
       procedure SetParent(AParent: TWinControl); override;
        { Protected declarations }
      public
        constructor create(Aowner:Tcomponent);override;
        procedure Click;override;
        { Public declarations }
      published
    //    property Caption:Tcaption read Fcaption write setcaption;
        property CommandLine:TcommandLine read FcommandLine write SetCommandLine;
        { Published declarations }
      end;
    procedure Register;
    implementation
    procedure TrunButton.SetParent(AParent: TWinControl);
    begin
      inherited;
      if AParent <> nil then
        Caption := Name;
    end;procedure Register;
    begin
      RegisterComponents('Samples', [TRunButton]);
    end;
      procedure TrunButton.setcaption(value:Tcaption);
        begin
          inherited;
          Fcaption:=value;
    //      repaint;
        end;
      constructor TrunButton.create(Aowner:Tcomponent);
        begin
          inherited create(Aowner);
         // setcaption(Name);
          width:=45;
          height:=45;
        end;
      Function IsExecutableFile(value:TcommandLine):boolean;
        begin
          result:=(uppercase(ExtractFileExt(value))='.EXE');
        end;
      procedure Trunbutton.SetCommandLine(value:TcommandLine);
        var
        Icon:Ticon;
        begin
          if not IsExecutableFile(value) then raise exception.Create(value+'不是一个可执行文件!');
          if not FileExists(value) then raise exception.Create(value+'文件不存在!');
          FcommandLine:=value;
          Icon:=Ticon.Create;
          try
            icon.Handle:=ExtractIcon(hinstance,Pchar(FcommandLine),0);
            with Glyph do
              begin
                canvas.brush.style:=bssolid;
                canvas.FillRect(canvas.cliprect);
                width:=icon.Width;
                height:=Icon.Height;
                canvas.Draw(0,0,icon);
              end;
          finally
              icon.Free;
          end;    
        end;
      function processexecute(commandline:TcommandLine;cshow:word):integer;
        var
          rslt:longbool;
          startupinfo:Tstartupinfo;
          processinfo:Tprocessinformation;
        begin
          fillchar(startupinfo,sizeof(Tstartupinfo),#0);
          with startupinfo do
            begin
              cb:=sizeof(Tstartupinfo);
              dwflags:=startf_useshowwindow or startf_forceonfeedback;
              wshowwindow:=cshow;
            end;
          rslt:=createprocess(pchar(commandline),nil,nil,nil,false,normal_priority_class,nil,
            nil,startupinfo,processinfo);
          if rslt then
              with processinfo do
                begin
                  waitforinputidle(hprocess,infinite);
                  closehandle(hthread);
                  closehandle(hprocess);
                  result:=0;
                end
          else
                result:=getlasterror;
        end;
      procedure Trunbutton.Click;
        var
          rean:word;
        begin
          inherited click;
          rean:=ProcessExecute(FcommandLine,sw_shownormal);
          if rean<>0 then
              raise exception.Create('错误地执行了程序:'+inttostr(rean));
        end;
    end.
      

  5.   

    解决,谢谢taxi(游少爷),lxl(蚊蚊),现在好像不能加分,等csdn好了后再加!
      

  6.   

    to taxi(游少爷):请问为什么在setparent中加入caption:=name才会改变caption,在其他位置比如说在create事件中为什么不行啊?setparent在什么时候触发?
    奇怪问题:我在此事件最后一行中加入showmessage(name)发现在设计期时当把此控件加入form时,此事件触发两次???
    而且第一次showmessage(name)是空,此时控件还在窗体的左上角,而第二次时showmessage(name)有了值,并且此时控件才移动到指定的位置,这是为什么?