【问题标题】:Problem while using Two Forms使用两种形式时出现问题
【发布时间】:2010-10-21 09:48:48
【问题描述】:

我不知道怎么问这个问题。问题如下。

我正在使用一个主表单和许多子表单,但不是 MDI 表单。 主窗体包含 5 个按钮和一个面板。每个按钮都会调用一个表单 在该面板内(作为父级)。在该子表单中,一个表单(Sub3)包含 TMainMenu 组件。 通过单击按钮调用时,每个表单都可以正常工作,但是在调用表单(Sub3)时,TMainMenu 不可见。我不知道如何使它可见。 请任何人帮助我。

提前致谢。

感谢和问候,

尤瓦拉杰

【问题讨论】:

  • 而且MainForm也有TMainMenu?那么您可能需要将两者“合并”。
  • @Roald 这一定不是问题,您可能完美地在两个表单中拥有两个 TMainMenus。我认为问题在于辅助 Sub3 具有某种禁用菜单的样式(例如,边框样式中的 bsDialog)。
  • 子窗口不能有菜单。当您使用面板作为 Sub3 的父级时,您将其设置为子窗口。

标签: delphi


【解决方案1】:

每个表单上只能有一个 MainMenu。虽然您可以在一个应用程序中拥有多个表单,每个表单都有自己的 MainMenu,但如果您在另一个表单中显示一个表单,则只有“外部”表单的主菜单可见。

当您将一个表单“重新设置为”另一个表单时(将 formB 显示​​为 formA 上的“组件”),那么您必须像 @skamradt 已经提到的那样合并菜单。

为此,只需让您的按钮使用“SwitchToForm”功能,例如:

type
  TMain_Form
  ...
  private
    FCurrentForm: TForm;
    procedure SwitchToForm(showForm: TForm);
  ...
  end;

procedure TMain_Form.SwitchToForm(showForm: TForm);
begin
  if (FCurrentForm <> nil) and (FCurrentForm.Name = showForm.Name) then begin
    // Naught to do
  end else begin
    // If a form is currently showing, hide it and if it has a menu, unmerge that 
    if FCurrentForm <> nil then 
    begin
      FCurrentForm.Hide;
      if Assigned(FCurrentForm.Menu) then 
      begin
        MainMenu.UnMerge(FCurrentForm.Menu);
      end;
    end;

    // Set the current form to the one passed in and re-parent that to the main form
    // If the form has a menu, merge that with the main menu of the main form and then
    // show it.
    FCurrentForm := showForm;
    with FCurrentForm do begin
      Parent := self;
      Align := alClient;
      BorderIcons := [];
      BorderStyle := bsNone;
    end;
    if Assigned(FCurrentForm.Menu) then begin
      MainMenu.Merge(FCurrentForm.Menu);
    end;
    FCurrentForm.Show;
  end;
end;

在此示例中:表单是主表单本身的父级,但您当然也可以将表单作为主表单上的面板或其他容器的父级。

【讨论】:

  • 嗨,主窗体不包含 MainMenu 控件。只有子窗体包含 MainMenu 控件。
  • 好吧,那是行不通的。正如我已经提到的,vcl 只会显示“外部”表单的主菜单。所以给它一个例如“帮助”菜单。甚至可能仅在将菜单从子表单合并到其中时才显示它,但是我不知道主菜单是否对更改其可见属性做出了很好的响应。
【解决方案2】:

您可以使用 TMainMenu.Merge 和 TMainMenu.Unmerge 将子窗体菜单与主窗体菜单合并/取消合并。在每个子表单的“OnActivate”中,我会向主表单发送一条自定义消息,该消息将取消合并来自另一个表单(可能尚未设置)的任何菜单并合并当前表单的菜单。

【讨论】:

  • 感谢大家的回复,再次感谢... by Yuvaraj
猜你喜欢
  • 2016-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多