请哪位高人看看这代码有什么错?急呀.(200) (200分)

9

9861

Unregistered / Unconfirmed
GUEST, unregistred user!
老是出错:Resource TMyGrid not found.
============================================================
unit ChenwBox;

interface

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

type
TChenWCombobox = class;
//TMyGrid=class(TDBGrid) 如果改成这句这没错.
TMyGrid=class(TForm)
private
FMyGrid:TChenWCombobox;

procedure CreateParams(var params:TCreateParams);override;
procedure WMSetFocus(var Msg:TWMSetFocus);message WM_SETFOCUS;
public
constructor Create(AOwner:TComponent);override;
end;

TChenWCombobox = class(TComboBox)
private
FMyForm:TMyGrid;
FSpeedButton : TSpeedButton;
FDowned:Boolean;
FFlag:Boolean;
procedure ShowCombobox;
procedure HideComboBox;
procedure CMCancelMode(var Msg:TCMCancelMode);message CM_CANCELMODE;
procedure WM_LButtonDown(var Msg:TWMMouse);message WM_LBUTTONDOWN;
public
Constructor Create(AOwner:TComponent);override;
destructor Destroy;override;

published
{ Published declarations }
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('DevTools', [TChenWCombobox]);
end;

{ TChenWCombobox }

procedure TChenWCombobox.CMCancelMode(var Msg: TCMCancelMode);
begin
inherited;
if FDowned then
begin
HideComboBox;
end;
end;

constructor TChenWCombobox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FMyForm:=TMyGrid.Create(self);
FDowned:=false;
//以下按钮在Creat时显不出来,也很奇怪.
FSpeedButton := TSpeedButton.Create(Self);
FSpeedButton.Left := FMyForm.Width;
FSpeedButton.Height := 19; // two less than TEdit's Height
FSpeedButton.Width := 19;
FSpeedButton.Caption := '...';
FSpeedButton.Parent := Self;

end;

destructor TChenWCombobox.Destroy;
begin
FMyForm.Free;
FMyForm:=nil;
inherited Destroy;
end;

procedure TChenWCombobox.HideComboBox;
begin
ShowWindow(FMyForm.handle,SW_HIDE);
FDowned:=not FDowned;
end;


procedure TChenWCombobox.ShowCombobox;
var
Point:TPoint;
begin
Point.x:=Left;
Point.y:=top+Height;
Point:=Parent.ClienttoScreen(Point);
SetWindowPos(FMyForm.Handle,HWND_TOP,Point.x,Point.y,0,0,SWP_NOACTIVATE or SWP_HideWindow or SWP_NOSIZE); ShowWindow(FMyForm.Handle,SW_SHOW);
FDowned:=not FDowned;
end;

procedure TChenWCombobox.WM_LButtonDown(var Msg: TWMMouse);
begin
if not FDowned then
begin
ShowComboBox;
end
else
begin
HideCombobox;
end;
end;

{ TMyGrid }

constructor TMyGrid.Create(AOwner: TComponent);
begin
inherited Create(AOwner); // 就是执行到这句后出错.why??
FMyGrid:=TChenWCombobox(AOwner);

ControlStyle:=ControlStyle + [csNoDesignVisible, csReplicatable,
csAcceptsControls];


Width:=150;
Height:=130;

Visible := False;
Parent:=TWinControl(AOwner);
end;

procedure TMyGrid.CreateParams(var params: TCreateParams);
begin
inherited CreateParams(params);
with Params do
begin
Style:=style or ws_popup;
EXStyle:=EXStyle or WS_EX_CLIENTEDGE or WS_EX_TOOLWINDOW;
end;
end;

procedure TMyGrid.WMSetFocus(var Msg: TWMSetFocus);
begin
Inherited;
FMyGrid.SetFocus;
end;

end.

 
下面代码我试过了,有点小毛病,不过没有你说的问题。你看看吧
unit xx;

interface

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

type
TChenWCombobox = class;
TMyGrid=class(TDBGrid) //如果改成这句这没错.
//TMyGrid=class(TForm)
private
FMyGrid:TChenWCombobox;
protected
procedure CreateParams(var params:TCreateParams);override;
procedure WMSetFocus(var Msg:TWMSetFocus);message WM_SETFOCUS;
public
constructor Create(AOwner:TComponent);override;
end;

TChenWCombobox = class(TComboBox)
private
FMyForm:TMyGrid;
FSpeedButton : TSpeedButton;
FDowned:Boolean;
// FFlag:Boolean;
procedure ShowCombobox;
procedure HideComboBox;
procedure CMCancelMode(var Msg:TCMCancelMode);message CM_CANCELMODE;
procedure WM_LButtonDown(var Msg:TWMMouse);message WM_LBUTTONDOWN;
public
Constructor Create(AOwner:TComponent);override;
destructor Destroy;override;

published
{ Published declarations }
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('DevTools', [TChenWCombobox]);
end;

{ TChenWCombobox }

procedure TChenWCombobox.CMCancelMode(var Msg: TCMCancelMode);
begin
inherited;
if FDowned then
begin
HideComboBox;
end;
end;

constructor TChenWCombobox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FMyForm:=TMyGrid.Create(self);
FDowned:=false;
//以下按钮在Creat时显不出来,也很奇怪.
FSpeedButton := TSpeedButton.Create(Self);
FSpeedButton.Left := FMyForm.Width;
FSpeedButton.Height := 19; // two less than TEdit's Height
FSpeedButton.Width := 19;
FSpeedButton.Caption := '...';
FSpeedButton.Parent := Self;

end;

destructor TChenWCombobox.Destroy;
begin
FMyForm.Free;
FMyForm:=nil;
inherited Destroy;
end;

procedure TChenWCombobox.HideComboBox;
begin
ShowWindow(FMyForm.handle,SW_HIDE);
FDowned:=not FDowned;
end;


procedure TChenWCombobox.ShowCombobox;
var
Point:TPoint;
begin
Point.x:=Left;
Point.y:=top+Height;
Point:=Parent.ClienttoScreen(Point);
SetWindowPos(FMyForm.Handle,HWND_TOP,Point.x,Point.y,0,0,SWP_NOACTIVATE or SWP_HideWindow or SWP_NOSIZE); ShowWindow(FMyForm.Handle,SW_SHOW);
FDowned:=not FDowned;
end;

procedure TChenWCombobox.WM_LButtonDown(var Msg: TWMMouse);
begin
if not FDowned then
begin
ShowComboBox;
end
else
begin
HideCombobox;
end;
end;

{ TMyGrid }

constructor TMyGrid.Create(AOwner: TComponent);
begin
inherited Create(AOwner); // 就是执行到这句后出错.why??
FMyGrid:=TChenWCombobox(AOwner);

ControlStyle:=ControlStyle + [csNoDesignVisible, csReplicatable,
csAcceptsControls];


Width:=150;
Height:=130;

Visible := False;
Parent:=TWinControl(AOwner);
end;

procedure TMyGrid.CreateParams(var params: TCreateParams);
begin
inherited CreateParams(params);
with Params do
begin
Style:=style or ws_popup;
EXStyle:=EXStyle or WS_EX_CLIENTEDGE or WS_EX_TOOLWINDOW;
end;
end;

procedure TMyGrid.WMSetFocus(var Msg: TWMSetFocus);
begin
Inherited;
FMyGrid.SetFocus;
end;

end.

 
to:tseug
注意这呀:
TMyGrid=class(TDBGrid) //如果改成这句这没错.
TMyGrid=class(TForm) //我要的是这句.
 
仔细看看帮助中关于TForm.Create方法,它是不允许没有相关的DFM文件就创建的,代替的
办法可以用CreateNew。你可以先自己按照要求设计一个Form,然后保存DFM文件为Temp.dfm
用以下方法创建:
Form2 := TForm.CreateNew(Application);
ReadComponentResFile('Temp.dfm', Form2);
 
to 0738:
我在D:/下建了一个Temp.dfm后
我这样写的
TMyForm = class(TForm)
...

FMyGrid := TMyForm.CreateNew(Application);
ReadComponentResFile('D:/Temp.dfm', FMyGrid);

但是在执行 ReadComponentResFile('D:/Temp.dfm', FMyGrid);时出错:
Project1.exe raised exception class EinvalidImage with message'Invalid stream format'
 
将DFM文件保存在ChenwBox.pas相同目录下,文件名为ChenwBox.dfm
我试过了,没错。
 
但我这为什么有错呢?请各位再看一下,谢谢了.
unit ChenwBox;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,DBGrids, DB, mPYBMComboBox, Buttons;

type
TChenWCombobox = class;
//TMyGrid=class(TDBGrid) 如果改成这句这没错.
TMyGrid=class(TForm)
private
FEdit:TChenWCombobox;

procedure CreateParams(var params:TCreateParams);override;
procedure WMSetFocus(var Msg:TWMSetFocus);message WM_SETFOCUS;
public
constructor Create(AOwner:TComponent);override;
end;

TChenWCombobox = class(TMPYBMComboBox)
private
FMyForm:TMyGrid;
FSpeedButton : TSpeedButton;
FDateSourse:TDataSource;
FDowned:Boolean;
FFlag:Boolean;
procedure ShowCombobox;
procedure HideComboBox;
procedure CMCancelMode(var Msg:TCMCancelMode);message CM_CANCELMODE;
procedure WM_LButtonDown(var Msg:TWMMouse);message WM_LBUTTONDOWN;
public
Constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
procedure SetDataSource(Value : TDataSource);
procedure Select(Value : Boolean = True);
published
{ Published declarations }
property ComDataSource:TDataSource Read FDateSourse write SetDataSource;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('DevTools', [TChenWCombobox]);
end;

{ TChenWCombobox }

procedure TChenWCombobox.CMCancelMode(var Msg: TCMCancelMode);
begin
inherited;
if FDowned then
begin
HideComboBox;
end;
end;

constructor TChenWCombobox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
// FMyForm:=TMyGrid.Create(self);
FMyForm := TMyGrid.CreateNew(Self);
ReadComponentResFile('ChenwBox.dfm', FMyForm);//这句出错,我也放到同一目录下了.

FDowned:=false;
end;

destructor TChenWCombobox.Destroy;
begin
FMyForm.Free;
FMyForm:=nil;
inherited Destroy;
end;

procedure TChenWCombobox.HideComboBox;
begin
ShowWindow(FMyForm.handle,SW_HIDE);
FDowned:=not FDowned;
end;

procedure TChenWCombobox.Select(Value: Boolean = true);
begin
FFlag := Value;
end;

procedure TChenWCombobox.SetDataSource(Value: TDataSource);
begin
FDateSourse := Value;
end;

procedure TChenWCombobox.ShowCombobox;
var
Point:TPoint;
begin
Point.x:=Left;
Point.y:=top+Height;
Point:=Parent.ClienttoScreen(Point);
SetWindowPos(FMyForm.Handle,HWND_TOP,Point.x,Point.y,0,0,SWP_NOACTIVATE or SWP_HideWindow or SWP_NOSIZE); ShowWindow(FMyForm.Handle,SW_SHOW);
FDowned:=not FDowned;
end;

procedure TChenWCombobox.WM_LButtonDown(var Msg: TWMMouse);
begin
if not FDowned then
begin
ShowComboBox;
end
else
begin
HideCombobox;
end;
end;

{ TMyGrid }

constructor TMyGrid.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FEdit:=TChenWCombobox(AOwner);

ControlStyle:=ControlStyle + [csNoDesignVisible, csReplicatable,
csAcceptsControls];


Width:=150;
Height:=130;

Visible := False;
Parent:=TWinControl(AOwner);
end;

procedure TMyGrid.CreateParams(var params: TCreateParams);
begin
inherited CreateParams(params);
with Params do
begin
Style:=style or ws_popup;
EXStyle:=EXStyle or WS_EX_CLIENTEDGE or WS_EX_TOOLWINDOW;
end;
end;

procedure TMyGrid.WMSetFocus(var Msg: TWMSetFocus);
begin
Inherited;
FEdit.SetFocus;
end;

end.
 
我跟踪时,发现执行完这句后:
ReadComponentResFile('ChenwBox.dfm', FMyForm);//这句出错,我也放到同一目录下了.
就跳到这里来了,是不是这的原因? 被Free了?
destructor TChenWCombobox.Destroy;
begin
FMyForm.Free;
FMyForm:=nil;
inherited Destroy;
end;
 
是不是你的TMyForm类和保存的dfm文件不匹配啊,再仔细查查
 
我猜一猜是不是少了
implementation
{$R *.DFM}
這一句?
 
unit ChenwBox;

interface

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

type
TChenWCombobox = class;
TMyGrid=class(TForm)
private
FMyGrid:TChenWCombobox;

procedure CreateParams(var params:TCreateParams);override;
procedure WMSetFocus(var Msg:TWMSetFocus);message WM_SETFOCUS;
public
constructor Create(AOwner:TComponent);override;
end;

TChenWCombobox = class(TComboBox)
private
FMyForm:TMyGrid;
FSpeedButton : TSpeedButton;
FDowned:Boolean;
FFlag:Boolean;
procedure ShowCombobox;
procedure HideComboBox;
procedure CMCancelMode(var Msg:TCMCancelMode);message CM_CANCELMODE;
procedure WM_LButtonDown(var Msg:TWMMouse);message WM_LBUTTONDOWN;
protected
procedure CreateParams(var Params: TCreateParams);//这里需要重载这个函数。
public
Constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
published
{ Published declarations }
end;

procedure Register;

implementation
{$R ChenwBox.dfm}
/////////////////////////////////////////////////////
//记得要加这句。
//否则编译器不知道你的DFM资源在什么地方。
//////////////////////////////////////////////////

procedure Register;
begin
RegisterComponents('DevTools', [TChenWCombobox]);
end;

{ TChenWCombobox }

procedure TChenWCombobox.CMCancelMode(var Msg: TCMCancelMode);
begin
inherited;
if FDowned then
begin
HideComboBox;
end;
end;

constructor TChenWCombobox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FMyForm:=TMyGrid.Create(self);
FDowned:=false;
////////////////////////////////////////////////////////////////////////////////////////////////
//以下按钮在Creat时显不出来,也很奇怪.
//并不奇怪!在VB中有容器这个概念,在Delphi虽然不怎么提,但也有类似的概念,TCombobox不是容器,
//所以你虽然把一个SpeedButton的Parent设成它,但它是不接受子控件的,所以显示不出来!
//如果真要把一个按钮放在TCombobox中建议你看重载TCombobox的CreateParams函数,在Params.Style中加入
//WS_CLIPCHILDREN。也就是说把它变成一个容器,可以接受子控件。详细代码见最后。^_^。
////////////////////////////////////////////////////////////////////////////////
FSpeedButton := TSpeedButton.Create(Self);
FSpeedButton.Left := FMyForm.Width;
FSpeedButton.Height := 19; // two less than TEdit's Height
FSpeedButton.Width := 19;
FSpeedButton.Caption := '...';
FSpeedButton.Parent := Self;

end;

destructor TChenWCombobox.Destroy;
begin
FMyForm.Free;
FMyForm:=nil;
inherited Destroy;
end;

procedure TChenWCombobox.HideComboBox;
begin
ShowWindow(FMyForm.handle,SW_HIDE);
FDowned:=not FDowned;
end;


procedure TChenWCombobox.ShowCombobox;
var
Point:TPoint;
begin
Point.x:=Left;
Point.y:=top+Height;
Point:=Parent.ClienttoScreen(Point);
SetWindowPos(FMyForm.Handle,HWND_TOP,Point.x,Point.y,0,0,SWP_NOACTIVATE or SWP_HideWindow or SWP_NOSIZE); ShowWindow(FMyForm.Handle,SW_SHOW);
FDowned:=not FDowned;
end;

procedure TChenWCombobox.WM_LButtonDown(var Msg: TWMMouse);
begin
if not FDowned then
begin
ShowComboBox;
end
else
begin
HideCombobox;
end;
end;

{ TMyGrid }

constructor TMyGrid.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
// 就是执行到这句后出错.why??
//只要指明DFM文件就不会出错了!
FMyGrid:=TChenWCombobox(AOwner);

ControlStyle:=ControlStyle + [csNoDesignVisible, csReplicatable,
csAcceptsControls];


Width:=150;
Height:=130;

Visible := False;
Parent:=TWinControl(AOwner);
end;

procedure TMyGrid.CreateParams(var params: TCreateParams);
begin
inherited CreateParams(params);
with Params do
begin
Style:=style or ws_popup;
EXStyle:=EXStyle or WS_EX_CLIENTEDGE or WS_EX_TOOLWINDOW;
end;
end;

procedure TMyGrid.WMSetFocus(var Msg: TWMSetFocus);
begin
Inherited;
FMyGrid.SetFocus;
end;

end.





procedure TChenWCombobox.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.Style := Params.Style or WS_CLIPCHILDREN;
end;
 
没这个组件把!
 
我没细看,既然代码都拿出来了,为什么不写详细点的说明?
:)
 
to aizb:
谢谢了,不过我这里出现这个错:
Invalid stream format

不知是什么原因?难道我随便建一个窗体命名为ChenwBox.dfm不行么?
 
不會吧,這樣怎讓人看呢
 
没人知道么~~5555~~~~
 
错误主要有以下两点:
1:在程序中用到了窗体,但却没有对应的dfm文件;
2:在不能拥有子控件的控件TCombobox上建立控件;

你可按如下方法修改:
1:增加相应的dfm文件:
新建立一个工程,把窗体的Name改为MyGrid(和你的代码对应);
保存单元文件,保存时把默认的文件名Unit1改为ChenwBox;
用如下所附代码覆盖单元ChenwBox中的代码;代码中要有{$R *.dfm}。
2:使TCombobox可以拥有控件:
重建CreateParams函数即可,内容见如下代码;

3:保存,得到的ChenwBox.pas和ChenwBox.dfm,就是你需要的。

在Delphi7中通过。

代码:

unit ChenwBox;

interface

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

type
TChenWCombobox = class;
TMyGrid=class(TForm)
private
FMyGrid:TChenWCombobox;

procedure CreateParams(var params:TCreateParams);override;
procedure WMSetFocus(var Msg:TWMSetFocus);message WM_SETFOCUS;
public
constructor Create(AOwner:TComponent);override;
end;

TChenWCombobox = class(TComboBox)
private
FMyForm:TMyGrid;
FSpeedButton : TSpeedButton;
FDowned:Boolean;
FFlag:Boolean;
procedure ShowCombobox;
procedure HideComboBox;
procedure CMCancelMode(var Msg:TCMCancelMode);message CM_CANCELMODE;
procedure WM_LButtonDown(var Msg:TWMMouse);message WM_LBUTTONDOWN;
protected
procedure CreateParams(var Params: TCreateParams); //******
public

Constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
published
{ Published declarations }
end;

procedure Register;

implementation
{$R *.dfm} //要有****

procedure Register;
begin
RegisterComponents('DevTools', [TChenWCombobox]);
end;

{ TChenWCombobox }

procedure TChenWCombobox.CMCancelMode(var Msg: TCMCancelMode);
begin
inherited;
if FDowned then
begin
HideComboBox;
end;
end;

constructor TChenWCombobox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FMyForm:=TMyGrid.Create(self);
FDowned:=false;
FSpeedButton := TSpeedButton.Create(Self);
FSpeedButton.Left := FMyForm.Width;
FSpeedButton.Height := 19; // two less than TEdit's Height
FSpeedButton.Width := 19;
FSpeedButton.Caption := '...';
FSpeedButton.Parent := Self;
end;

destructor TChenWCombobox.Destroy;
begin
FMyForm.Free;
FMyForm:=nil;
inherited Destroy;
end;

procedure TChenWCombobox.HideComboBox;
begin
ShowWindow(FMyForm.handle,SW_HIDE);
FDowned:=not FDowned;
end;


procedure TChenWCombobox.ShowCombobox;
var
Point:TPoint;
begin
Point.x:=Left;
Point.y:=top+Height;
Point:=Parent.ClienttoScreen(Point);
SetWindowPos(FMyForm.Handle,HWND_TOP,Point.x,Point.y,0,0,SWP_NOACTIVATE or

SWP_HideWindow or SWP_NOSIZE); ShowWindow(FMyForm.Handle,SW_SHOW);
FDowned:=not FDowned;
end;

procedure TChenWCombobox.WM_LButtonDown(var Msg: TWMMouse);
begin
if not FDowned then
begin
ShowComboBox;
end
else
begin
HideCombobox;
end;
end;

procedure TChenWCombobox.CreateParams(var Params: TCreateParams); //****
begin
inherited CreateParams(Params);
Params.Style := Params.Style or WS_CLIPCHILDREN;
end;

{ TMyGrid }

constructor TMyGrid.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FMyGrid:=TChenWCombobox(AOwner);

ControlStyle:=ControlStyle + [csNoDesignVisible, csReplicatable,
csAcceptsControls];


Width:=150;
Height:=130;

Visible := False;
Parent:=TWinControl(AOwner);
end;

procedure TMyGrid.CreateParams(var params: TCreateParams);
begin
inherited CreateParams(params);
with Params do
begin
Style:=Style or WS_POPUP;
EXStyle:=EXStyle or WS_EX_CLIENTEDGE or WS_EX_TOOLWINDOW;
end;
end;

procedure TMyGrid.WMSetFocus(var Msg: TWMSetFocus);
begin
Inherited;
FMyGrid.SetFocus;
end;

end.
 
to:LiWD
你是怎么找到这个帖子的?是李卫东吧.
这个在combobox上显示出窗体我已经知道了,但不知怎样在显示的窗体上进行操作.你要
是有这方面的书给俺推荐一下.呵呵呵.你何时来北京?
 

Similar threads

I
回复
0
查看
508
import
I
I
回复
0
查看
672
import
I
I
回复
0
查看
467
import
I
顶部