<font color=#FF0000>怎样编些这个Activex控件</font>)(100分)

D

dream

Unregistered / Unconfirmed
GUEST, unregistred user!

小弟通过继承tcustomcom做了一个选择线壮符号的控件tmylinecombobox。

想把这个控件生成Activex控件,可在activex control wizard
加入tmylinecombobox类时,却出现“ control '' has no parent window”这错误。
有哪位大虾能指点迷津,谢了!!!

tmylinecombobox 的原码如下:


unit mylinestyle;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
tmylinecombobox = class(TCustomComboBox)
private
{ Private declarations}
plinestyle : integer;
procedure DrawPen(Control: TWinControl
Index: Integer
Rect: TRect;
State: TOwnerDrawState);
protected
{ Protected declarations }
public
{ Public declarations }
LineList :array[0..4] of integer;
Changed : Boolean;
constructor Create(AOwner: TComponent);override;
destructor Destroy;override;
procedure DoChange(Sender : TObject);
published
{ Published declarations }
property linestyle : integer read plinestyle write plinestyle;
property OnChange;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('MyControl', [TmyLineComboBox]);
end;

procedure tmylinecombobox.DrawPen(Control: TWinControl
Index: Integer
Rect: TRect;
State: TOwnerDrawState);
begin

with Canvas do begin
brush.Style:=bsclear;
brush.color:=clwhite;
pen.color:=clwhite;
Rectangle(Rect.Left,Rect.Top,Rect.Right,Rect.Bottom);
pen.Style := TpenStyle(lineList[Index]);
pen.color:=clblack;
MoveTo(Rect.Left+15,Rect.Top+Trunc(ItemHeight/2));
LineTo(Rect.Right-15,Rect.Top+Trunc(ItemHeight/2));
end;
end;

constructor tmylinecombobox.Create(AOwner: TComponent);
var
I : integer;
begin
inherited Create(AOwner);
Parent := TForm(AOwner);

OnDrawItem := DrawPen;
OnChange := DoChange;
for I := 0 to 4 do Items.Add('');
LineList[0]:= 0;
LineList[1]:= 1;
LineList[2]:= 2;
LineList[3]:= 3;
linelist[4]:=4;
Style := csOwnerDrawFixed;
itemindex:=0;

end;

destructor tmylinecombobox.Destroy;
begin
inherited Destroy;
end;

procedure tmylinecombobox.DoChange(Sender : TObject);
begin
linestyle := LineList[Self.ItemIndex];
Changed := TRUE;
end;

end.




 
it should extends TWinControl
 
//改成下面的就可以了,注意你在create里不能调用被我注释掉的那些属性(对于activex来说)
//应该把它放在loaded中调用

unit mylinecombobox;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
tmylinecombobox = class(TCustomComboBox)
private
{ Private declarations}
plinestyle : integer;
procedure DrawPen(Control: TWinControl
Index: Integer
Rect: TRect;
State: TOwnerDrawState);
protected
{ Protected declarations }
public
{ Public declarations }
LineList :array[0..4] of integer;
Changed : Boolean;
constructor Create(AOwner: TComponent);override;
destructor Destroy;override;
procedure DoChange(Sender : TObject);
procedure Loaded;override;
published
{ Published declarations }
property linestyle : integer read plinestyle write plinestyle;
property OnChange;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('MyControl', [TmyLineComboBox]);
end;

procedure tmylinecombobox.DrawPen(Control: TWinControl
Index: Integer
Rect: TRect;
State: TOwnerDrawState);
begin

with Canvas do begin
brush.Style:=bsclear;
brush.color:=clwhite;
pen.color:=clwhite;
Rectangle(Rect.Left,Rect.Top,Rect.Right,Rect.Bottom);
pen.Style := TpenStyle(lineList[Index]);
pen.color:=clblack;
MoveTo(Rect.Left+15,Rect.Top+Trunc(ItemHeight/2));
LineTo(Rect.Right-15,Rect.Top+Trunc(ItemHeight/2));
end;
end;

constructor tmylinecombobox.Create(AOwner: TComponent);
var
I : integer;
begin
inherited Create(AOwner);
//Parent := TForm(AOwner);//另外此行有问题,你的owner不一定是form,maybe panel

OnDrawItem := DrawPen;
OnChange := DoChange;
{for I := 0 to 4 do Items.Add('');}
LineList[0]:= 0;
LineList[1]:= 1;
LineList[2]:= 2;
LineList[3]:= 3;
linelist[4]:=4;
//Style := csOwnerDrawFixed;
//itemindex:=0;
end;

destructor tmylinecombobox.Destroy;
begin
inherited Destroy;
end;

procedure tmylinecombobox.DoChange(Sender : TObject);
begin
linestyle := LineList[Self.ItemIndex];
Changed := True;
end;

procedure tmylinecombobox.Loaded;
var
I:integer;
begin
inherited;
for I := 0 to 4 do Items.Add('');
Style := csOwnerDrawFixed;
itemindex:=0;
end;

end.
 
hubdog, 你的解答果然正确,能告诉我这样修改的原因吗?

还有我想在vb中引用这个ocx控件,却告诉我“dll加载失败”,
应该怎么办
多谢了!!!
 
我想你应该用排除法,把loaded中的语句挨个试一下,看一下问题到底出在那个语句
的问题,我怀疑是在create里调用某些控件属性时,某些东西还没建立起来,
所以我把他们都放在loaded,这时应该都建立起来了,调用比较可靠。
对于vb,因为我没用过,我想也是用排除法,先直接建立Tcustomcombobox的继承类,什么语句都不加,调用一下(borland的标准控件应该会成功),再逐步添加,找到
问题所在。
 
接受答案了.
 

Similar threads

顶部