一个Class通过单元文件引入工程(非VCL控件类),然后在类中要获得当前活动的窗体TComponent对象,例如当前用户操作的窗体是frmMain,则类里面取得的TComponent是frmMain,用户切换到另一个窗体frmOther时类里的TComponent也相应换成这个对应的窗体对象。除了在类里写个方法在FormCreate时把类里的全局TComponent对象给设置为当前活动窗体以外,有没有能自动获知变化的方法?

解决方案 »

  1.   

    如果是在MDI方式下就是 : ActiveMDIChild.name
      

  2.   

    这个我也想知道,用MDI方式就比较容易
      

  3.   


    那就在  OnActive 的時候寫過去。
      

  4.   

    没太明白你要干嘛,下面这段代码是写在ApplicationEvents.OnMessage中的,不知道符合你的要求么:procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
      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;
      

  5.   


     还有在我的写法里SDI下,一般ACTIVE的就是模态窗体。
      

  6.   

    TApplicationEvent控件中的Active或DeActive中写!
      

  7.   

    function GetActiveFRM:string;
    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;
      

  8.   

    看来用类自己获取当前活动窗体是没办法了,你这个方法我测试了非常合适,十分感谢!我主要是在类里写了个procedure SetCurrentForm(fCurrForm: TComponent);
    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([上一次的窗体名]),搞得我都头大了。用您的方法我稍作修改移植过去试试看,应该会很好的
      

  9.   

    结贴,特别感谢 Seamour ,参与回答的朋友们也放分,(*^__^*) 嘻嘻……
      

  10.   

    GetActiveWindow会把其他不相关的程序窗体也弄进去,我只是想要自己工程中的窗体,谢谢了