mdichildform子窗体显示如何自动隐藏主窗体mdiform的工具栏???

解决方案 »

  1.   

    每个子窗体作上工具栏。
    这样最大化后子窗体的工具栏显示在主窗体最上面了要不然你用寻找同名Action的方式也可以作到。
    举个示例代码:
    //  当主窗体的主工具栏按钮按下时去寻找子窗体中同名的Action并执行
    procedure TfrmMain.BindActiveFormExecute(Sender: TObject);
    var
      ActiveForm: TForm;
      Action: TAction;
    begin
      Action := nil;  //  判断当前页是否有停靠窗口
      if (PageControl1.PageCount > 0) and
        (PageControl1.ActivePage.ControlCount > 0) and
        (PageControl1.ActivePage.Controls[0] is TForm) then
      begin
        ActiveForm := PageControl1.ActivePage.Controls[0] as TForm;    //  寻找当前Action
        if Sender is TAction then Action := FindSameAction(ActiveForm, Sender as TAction);
      end;  //  执行绑定过程
      if Assigned(Action) then Action.Execute;
    end;
    //  更新主窗体的Action的Enabled有效性
    procedure TfrmMain.BindActiveFormUpdate(Sender: TObject);
    var
      ActiveForm: TForm;
      MainAction, Action: TAction;
    begin
      Action := nil;  //  寻找当前快捷菜单的Active
      if (PageControl1.PageCount > 0) and
        (PageControl1.ActivePage.ControlCount > 0) and
        (PageControl1.ActivePage.Controls[0] is TForm) then
      begin
        ActiveForm := PageControl1.ActivePage.Controls[0] as TForm;
        Action := FindSameAction(ActiveForm, Sender as TAction);
      end;  if Assigned(Action) then
      begin
        Action.Update;
        MainAction := (Sender as TAction);    //  设置Action属性
        MainAction.Enabled := Action.Enabled;
        if Action.Caption <> MainAction.Caption then Action.Caption := MainAction.Caption;
        if Action.ShortCut <> MainAction.ShortCut then Action.ShortCut := MainAction.ShortCut;
      end else (Sender as TAction).Enabled := False;
    end;//  在子窗体中寻找同名的Action
    function TfrmMain.FindSameAction(Form: TForm; Action: TAction): TAction;
    var
      ActionList: TActionList;
      i, j: Integer;             //临时增加
      frmSub: TForm;             //临时增加
    begin
      Result := nil;
      ActionList := nil;  //ActionList := TActionList.Create(nil);  //  基本没用
      try
        //  循环枚举窗口上的所有元素
        for i := 0 to Form.ComponentCount - 1 do
        begin
          //  判断是否是子窗口
          if Form.Components[i] is TForm then
          begin
            frmSub := (Form.Components[i] as TForm);        //记录窗体子元件        //  循环查找子窗口中的TActionList
            for j:=0 to frmSub.ComponentCount - 1 do
            begin
              if frmSub.Components[j] is TActionList then
              begin
                ActionList := frmSub.Components[j] as TActionList;
                Break;
              end;
            end;
          end;      //  判断寻找到的控件是否是TActionList
          if Form.Components[i] is TActionList then
          begin
            ActionList := Form.Components[i] as TActionList;
            Break;
          end;
        end;    //  没有找到退出过程
        if Assigned(ActionList) then
        begin
          //  循环查找对应的Action
          for i := 0 to ActionList.ActionCount - 1 do
          begin
            if (ActionList[i] as TAction).Name = Action.Name then
            begin
              Result := ActionList[i] as TAction;
              Break;
            end;
          end;
        end;
      finally
        //ActionList.Free;      //  基本没用
      end;
    end;
    主窗体里的ActionList里的Action的onExecute 绑定BindActiveFormExecute函数,那当子窗体打开后点主窗体的工具栏就会去找同名的action执行它的onexecute
    而Onupdate应该绑定BindActiveFormUpdate过程,这样实现主窗体的工具栏按钮有效性跟据子窗体同名action来进行。