一个Class通过单元文件引入工程(非VCL控件类),然后在类中要获得当前活动的窗体TComponent对象,例如当前用户操作的窗体是frmMain,则类里面取得的TComponent是frmMain,用户切换到另一个窗体frmOther时类里的TComponent也相应换成这个对应的窗体对象。除了在类里写个方法在FormCreate时把类里的全局TComponent对象给设置为当前活动窗体以外,有没有能自动获知变化的方法?
调试欢乐多
那就在 OnActive 的時候寫過去。
var Handled: Boolean);
var
frm : TCustomForm;
ctl : TWinControl;
procedure GetActiveFormControl;
begin
frm := Screen.ActiveCustomForm;
if(Assigned(frm))then
ctl := frm.ActiveControl;
end;
begin
GetActiveFormControl;
Memo1.Clear;
if(Assigned(frm))then
begin
Memo1.Lines.Add(Format('Active Form: %s', [frm.Name]));
if(Assigned(ctl))then
Memo1.Lines.Add(Format('Active Control: %s', [ctl.Name]));
end;
end;
还有在我的写法里SDI下,一般ACTIVE的就是模态窗体。
var
buf:array[0..MAXBYTE]of char;
begin
buf:=#0;
GetClassName(GetActiveWindow,buf,MAXBYTE);
Result:=buf;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(GetActiveFRM);
end;
procedure TLangine.SetCurrentForm(fCurrForm: TComponent);
begin
frmComponent := fCurrForm;
end;然后我的一些过程需要依赖这个代码procedure TLangine.SetCurrentLang(fLangName: UnicodeString);
var
CurrentForm: string;
Ini: TInifile;
begin
if trim(fLangName) <> '' then
GlobLangFileName := fLangName
else
GlobLangFileName := 'English';
if FileExists(GlobLangFilePath + fLangName + '.lng') then
begin
CurrentForm := frmComponent.Name;
Ini := TIniFile.Create(GlobLangFilePath + fLangName + '.lng');
if frmComponent is TForm then
begin
(frmComponent as TForm).Font.Name :=
Ini.ReadString('Setting', 'DefaultFontName', (frmComponent as
TForm).Font.Name);
(frmComponent as TForm).Font.Size :=
Ini.ReadInteger('Setting', 'DefaultFontSize', (frmComponent as
TForm).Font.Size);
end;
end;
Ini.Free;
end;
现在我的做法是不得已要在每个窗体OnShow时加上SetCurrentForm([当前窗体名]),再在OnClos时来一次SetCurrentForm([上一次的窗体名]),搞得我都头大了。用您的方法我稍作修改移植过去试试看,应该会很好的