如何在MDI子项合并菜单时添加MenuItems

时间:2010-07-16 01:23:23

标签: c# winforms mdi

由于MdiList属性,我有一个带有'Windows'列表的菜单,其中包含所有mdi子项。但是,还有其他窗口不在MDI容器中,我想在菜单中列出。我可以轻松地将它们添加到菜单中,但是当其中一个MDI孩子有菜单时会出现问题。菜单已合并,但之前我将其他窗口添加到列表中的菜单已合并,因此不正确。

使用DockPanelSuite

    void menuWindow_Popup(object sender, EventArgs e)
    {
        // Because not all windows are MDI children anymore
        // we need to find all the other windows and add them to the menu

        // Delete whaterver pre-existed
        while (menuWindow.MenuItems.Count > 1)
        {
            menuWindow.MenuItems.RemoveAt(1);
        }

        if (dockPanel.FloatWindows.Count > 0)
        {
            menuWindow.MenuItems.Add(new MenuItem("-"));

            // Then add all the floating/docked windows
            // Note MDIList takes care of the windows that are in MDI mode
            foreach (Form dockContent in dockPanel.FloatWindows)
            {
                // each event handler closure needs its own form reference (not a shared one)
                Form content = dockContent;
                var mi = new MenuItem(content.Text,
                    (EventHandler)delegate(object s, EventArgs ea)
                    {
                        if (content.WindowState == FormWindowState.Minimized)
                        {
                            content.WindowState = FormWindowState.Normal;
                        }
                        content.Show();
                        content.Focus();
                    });
                mi.Checked = (content == dockPanel.ActiveContent);
                menuWindow.MenuItems.Add(mi);
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

嗯添加以下内容似乎解决了这个问题:

        menuWindow = (MenuItem)sender;
        if (dockPanel.ActiveDocument != null)
        {
            foreach (MenuItem item in ((Form)dockPanel.ActiveDocument).MergedMenu.MenuItems)
            {
                if (item.Text == "&Window")
                {
                    menuWindow = item;
                }
            }
        }

但如果有更好的方法,仍然会留下问题