程序如下,创建好控件后,拖在窗体上,cpu的使用率是1000%
不久就出现栈溢出
这是怎么回事,如何解决?
type  
TNComboBoxEx = class(TComboBox)
  protected
    ComboBoxBtn : TButton;
public
procedure CreateParams(var Params: TCreateParams); override;procedure TNComboBoxEx.CreateParams(var Params: TCreateParams);
begin
    inherited CreateParams(Params);
         ComboBoxBtn := TButton.Create(self);
         ComboBoxBtn.Parent := self;
         ComboBoxBtn.Top := 0;
         ComboBoxBtn.Height := Height;
         ComboBoxBtn.Width := Height;
         ComboBoxBtn.Left := Width-ComboBoxBtn.Width;
         ComboBoxBtn.Visible :=true;
end;

解决方案 »

  1.   

    那还有一个函数,(我怕贴多了各位没有时间看)
    type  
    TNComboBoxEx = class(TComboBox)
      protected
         ComboBoxBtn : TButton;
         FUsesButton: bool;
         procedure SetUsesButton(Value: bool);
      public
         procedure CreateParams(var Params: TCreateParams); override;
      published
         property UsesButton: bool read FUsesButton write SetUsesButton default false;
      end;
    procedure Register;
    ...procedure TNComboBoxEx.CreateParams(var Params: TCreateParams);
    begin
        inherited CreateParams(Params);
        if(fUsesButton) then
        begin
             ComboBoxBtn := TButton.Create(self);
             ComboBoxBtn.Parent := self;
             ComboBoxBtn.Top := 0;
             ComboBoxBtn.Height := Height;
             ComboBoxBtn.Width := Height;
             ComboBoxBtn.Left := Width-ComboBoxBtn.Width;
             ComboBoxBtn.Visible :=true;
          end;
    end;
    procedure TNComboBoxEx.SetUsesButton(Value: bool);
    begin
      if(FUsesButton<>Value) then
      begin
        if(Assigned(ComboBoxBtn)) then
           ComboBoxBtn.Free;
         FUsesButton := Value;
         if(value) then
             Style := csSimple
         else
           self.Style := csDropDown  ;
      end;
    end;
    就这样就会出现死循环(在编辑环境下),在运行环境下是好的。
      

  2.   

    结果是语句执行到CreateParams函数的
    ComboBoxBtn.Parent := self;代码时的 时候就会重新调用CreateParams函数。
    不过我还是搞不懂为什么会调用?
      

  3.   

    CreateParam是由CreateWindow调用来设置窗口初始化参数,也就是说,调用该函数时,窗口还没有创建完。不应该在这里创建其它子窗口。只有在需要改变窗口的初始参数时才重载该函数。这里应该重载构造函数,在构造函数里创建它的子窗口。
      

  4.   

    楼上说的对. 要初始化私有变量可以重载create函数。 
    CreateParam函数是用来改变窗口的风格的。