但是你的循环已经找到了父窗体,为什么关闭的时候,还要(o.Parent as TForm).Close?
注意循环是在 o.Parent is TForm 时退出的,所以后面用o.Parent: while (o.Parent <> nil) and not (o.Parent is TForm) do o := o.Parent; (o.Parent as TForm).Close;或许这样更清晰,多了条语句: o := o.Parent; while (o <> nil) and not (o is TForm) do o := o.Parent; if o <> nil then // 判断一下更保险 (o as TForm).Close;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;// 直接继承TEdit,另外再写一个控件也可以。
type
TEdit = class(StdCtrls.TEdit)
protected
procedure KeyPress(var Key: Char); override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure CMEnter(var Message: TCMEnter); message CM_ENTER;
procedure CMExit(var Message: TCMExit); message CM_EXIT;
end;type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TEdit.KeyPress(var Key: Char);
var o: TWinControl;
begin
o := Self;
if Key = #13 then begin
while (o.Parent <> nil) and not (o.Parent is TForm) do o := o.Parent;
(o.Parent as TForm).Close;
Exit;
end;
inherited;
end;procedure TEdit.KeyDown(var Key: Word; Shift: TShiftState);
begin
ShowMessage(chr(Key));
inherited;
end;procedure TEdit.CMEnter(var Message: TCMEnter);
begin
ShowMessage('Enter');
inherited;
end;procedure TEdit.CMExit(var Message: TCMExit);
begin
ShowMessage('Exit');
inherited;
end;end.
while (o.Parent <> nil) and not (o.Parent is TForm) do o := o.Parent;
(o.Parent as TForm).Close;
这段是什么意思,
o := o.Parent?
while (o.Parent <> nil) and not (o.Parent is TForm) do o := o.Parent;
(o.Parent as TForm).Close;或许这样更清晰,多了条语句:
o := o.Parent;
while (o <> nil) and not (o is TForm) do o := o.Parent;
if o <> nil then // 判断一下更保险
(o as TForm).Close;
我认为没有问题呀?还有,为什么enter重载的时候,要加入message,而keydown不用
procedure keydown(var key:word; shift:TShiftState);override;
前辈.小弟想请教一下.Delphi消息机制中WM_*代表Windows标准消息.那CM_、CN_打头的代表什么呀??比较晕。