我做了一些測試,並得到了這個原理更簡單的解決方案沒有問題。
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;
不知道,但我認爲原來的問題需要這個編輯: 1.我創建了兩個事件處理程序:onKeyDown和中的OnKeyPress我TFRAME。 2.在使用TFrame的TForm中,我編寫了兩個事件處理程序,它們連接到TForm的OnKeyDown和OnKeyPress。這些事件處理程序首先調用TFrame的同名方法。根據這些調用的結果,TForm事件處理程序可以自行處理密鑰。 – RobertFrank