如何编写自己的构件向所在的From添加OnKeyPress处理代码(100分)

  • 主题发起人 主题发起人 foxhu
  • 开始时间 开始时间
F

foxhu

Unregistered / Unconfirmed
GUEST, unregistred user!
我知道如何使Enter转变为Tab键,通过发送消息到FROM内,
更改KEYPREVIEW,然后在OnKeyPress诶增加处理代码。

但是我希望设计成一个构件,我决定从
TComponent继承,然后获取他的ParentComponent.Handle
向TFrom的KeyPreview付值True,添加OnKeyPress处理代码。
请教各位我无法实现在自己的构件中添加TFrom的OnKeyPress
代码
 
try this:

ParentComponent.OnKeyPress=your_prodecure;
 
用:
GetParentForm(Control: TControl);
 
各位以下为我的初步考虑,结果出错,请指教,(原问题提出者)
unit EnterToTab;

interface

uses WinTypes, WinProcs, Messages, SysUtils, Classes, Controls,
Forms, Graphics, Dialogs;

type
TEnterToTab = class(TComponent)
private
FActive : Boolean; //打算用于设置启动关闭的开关
procedure AutoInitialize;
procedure AutoDestroy;
Procedure NewKeyPress(Sender: TObject; var Key: Char);
protected
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Active : Boolean read FActive write FActive;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TEnterToTab]);
end;
procedure TEnterToTab.AutoInitialize;
begin
end; { of AutoInitialize }

procedure TEnterToTab.AutoDestroy;
begin
{ No objects from AutoInitialize to free }
end; { of AutoDestroy }

constructor TEnterToTab.Create(AOwner: TComponent);
Var
MYForm: TCustomForm;
begin
inherited Create(AOwner);
AutoInitialize;
MYForm := GetParentForm(AOwner); //通过自己获取父类Form
MYForm.KeyPreview:=True; //设置父类KeyPreview
MYForm.OnKeyPress:=NewKeyPress; //设置父类OnKeyPress---出错
end;

Procedure TEnterToTab.NewKeyPress(Sender: TObject; var Key: Char);
Begin
//只想检测是否成功截取和设置OnKeyPress
//新的OnKeyPress处理程序
if Key = #13 then ShowMessage('Everything Is OK!');
then inherited KeyPress(Key); }
End;

destructor TEnterToTab.Destroy;
begin
AutoDestroy;
inherited Destroy;
end;
end.
 
To delphi fan2:
GetParentForm(Control: TControl)的参数是Control不是TComponent;

可以这样获得form:
var
form: TForm;
begin
if not (Owner is TCustomForm) then
raise Exception.Create('不能把本构件放在DATAMODULE或WEBMODUAL上')
else
begin
form := (Owner as TCustomForm);
form.keyPreview := true;
form.OnKeyPress := yourProcedure;
end;
 
我认为做这东西用处不大!
 
接受答案了.
 
后退
顶部