我做了一些更多的测试,得到了一个更简单的解决方案,该解决方案的原理没有问题。
1)我通过递归函数获得了我的框架的基础类型对象。
2) 如果活动控件包含在标准框架中,则调用 OnKeyDown、OnKeyPress 事件执行。
如果有人需要什么,下面是我的应用程序测试代码。
Unit UnFrameBase;
Interface
Uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, ActnList;
Type
TFrameBase = Class(TFrame)
PnlBase: TPanel;
Private
Public
Procedure EventFrameKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState); Virtual;
End;
Implementation
{$R *.dfm}
{ TFrameBase }
Procedure TFrameBase.EventFrameKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
Begin
//nothing
End;
End.
Unit UnControlsClassTest;
interface
uses Classes, Controls;
Function ActiveControlAs(AControl: TWinControl;
AClass: TWinControlClass): TWinControl;
Function CheckControl(Var C: TWinControl; ControlClass: TWinControlClass): Boolean;
Var
Parent: TWinControl;
Begin
Result := (C Is ControlClass);
If (Result = False) And (C <> Nil) And (C.Parent <> Nil) Then
Begin
Parent := C.Parent;
Result := CheckControl(Parent, ControlClass);
If Result Then
C := Parent;
End;
End;
Var
C: TWinControl;
Begin
C := AControl;
If CheckControl(C, AClass) Then
Result := C
Else
Result := Nil;
End;
Unit UnMainFormTest;
Interface
Uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, UnFrameCustomer, UnFrameBase;
Type
TFormMain = Class(TForm)
Procedure FormCreate(Sender: TObject);
Procedure BtnTestarClick(Sender: TObject);
Procedure FormKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
Private
Function GetInstanceActiveFrame(AActiveControl: TWinControl): TFrameBase;
Public
{ Public declarations }
End;
Implementation
Uses UnControlsClassTest;
{$R *.dfm}
Function TFormMain.GetInstanceActiveFrame(AActiveControl: TWinControl): TFrameBase;
Begin
Result := TFrameBase(ActiveControlAs(AActiveControl, TFrameBase));
End;
Procedure TFormMain.FormKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
Var
LActiveFrame: TFrameBase;
Begin
LActiveFrame := GetInstanceActiveFrame(ActiveControl);
If Assigned(LActiveFrame) Then
LActiveFrame.EventoFrameKeyDown(Sender, Key, Shift);
If Key In [VK_DOWN, VK_RETURN] Then
Begin
Perform(WM_NEXTDLGCTL, 0, 0);
Key := 0;
End
Else If Key = VK_UP Then
Begin
Perform(WM_NEXTDLGCTL, 1, 0);
Key := 0;
End;
End;