Reputation: 5
My application have parent form with MenuStrip
, ToolStrip
and isMdiContainer=true
.
My Menu strip
was merged with child form menu strip items
.
When i open child form contol box items from child form was showed.
Child icon was showed in parent Menu Strip
and controls was showed.
I hide all but again was showing on start.
I set ControlBox, MinimizeBox, MaximizeBox, ShowIcon to false and FormBorderStyle to none.
private void partneriToolStripMenuItem_Click(object sender, EventArgs e)
{
ChildForm newMDIChild = new ChildForm();
newMDIChild.Show();
newMDIChild.MdiParent = this;
newMDIChild.tsChilds.Visible = false;
newMDIChild.WindowState = FormWindowState.Maximized;
newMDIChild.FormBorderStyle = FormBorderStyle.None;
newMDIChild.ControlBox = false;
newMDIChild.MinimizeBox = false;
newMDIChild.MaximizeBox = false;
newMDIChild.ShowIcon = false;
}
I dont expect from anyone to finish my job i just want solution.
Upvotes: 0
Views: 1445
Reputation: 11801
When you maximize a MDI-Childform on a MDI-Parent with MenuStrip set as the parent's MainMenuStrip Property, four items are added to the MainMenuStrip.
The first is of type System.Windows.Forms.MdiControlStrip+SystemMenuItem and the remaining three are of type System.Windows.Forms.MdiControlStrip+ControlBoxMenuItem. You can observe this by subscribing to the menu's ItemAdded Event. You can also use this event to to set these items' Visible
property to false. These item types are not publicly exposed, so Reflection must be used to obtain the type information.
The following example code demonstrates this with the addition feature of being able to toggle the item's visibility.
using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
namespace WindowsFormsApp4
{
public partial class MdiParent : Form
{
private int childIndex;
private Type MdiControlStrip_SystemMenuItem;
private Type MdiControlStrip_ControlBoxMenuItem;
private bool mdiChildControlBoxEnabled = true;
public MdiParent()
{
InitializeControls();
Assembly asm = this.GetType().BaseType.Assembly;
MdiControlStrip_SystemMenuItem = asm.GetType("System.Windows.Forms.MdiControlStrip+SystemMenuItem");
MdiControlStrip_ControlBoxMenuItem = asm.GetType("System.Windows.Forms.MdiControlStrip+ControlBoxMenuItem");
}
private void InitializeControls()
{
SuspendLayout();
Size = new Size(800, 400);
Text = "MDI Parent";
IsMdiContainer = true;
MenuStrip menu = new MenuStrip { Dock = DockStyle.Top };
ToolStripMenuItem addChild = new ToolStripMenuItem { Text = "Add Child", AutoSize = true };
addChild.Click += (s, e) => { AddChildForm(); };
menu.Items.Add(addChild);
ToolStripMenuItem restoreChild = new ToolStripMenuItem { Text = "restore ActiveMdiChild", AutoSize = true };
restoreChild.Click += (s, e) => { if (ActiveMdiChild != null) ActiveMdiChild.WindowState = FormWindowState.Normal; };
menu.Items.Add(restoreChild);
ToolStripMenuItem showChildControlBox = new ToolStripMenuItem { Text = "Show Child ControlBox - " + mdiChildControlBoxEnabled.ToString(), AutoSize = true };
showChildControlBox.Click += (s,e) => {
mdiChildControlBoxEnabled = !mdiChildControlBoxEnabled;
showChildControlBox.Text = "Show Child ControlBox - " + mdiChildControlBoxEnabled.ToString();
SetMDIMenuItemVisiblity(mdiChildControlBoxEnabled);
};
menu.Items.Add(showChildControlBox);
Controls.Add(menu);
MainMenuStrip = menu;
MainMenuStrip.ItemAdded += MainMenuStrip_ItemAdded;
ResumeLayout(true);
}
private void MainMenuStrip_ItemAdded(object sender, ToolStripItemEventArgs e)
{
if (!mdiChildControlBoxEnabled)
{
Type itemType = e.Item.GetType();
if (itemType == MdiControlStrip_SystemMenuItem || itemType == MdiControlStrip_ControlBoxMenuItem)
{
e.Item.Visible = false;
}
}
}
private void SetMDIMenuItemVisiblity(bool visible)
{
foreach (ToolStripMenuItem item in MainMenuStrip.Items)
{
Type itemType = item.GetType();
if (itemType == MdiControlStrip_SystemMenuItem || itemType == MdiControlStrip_ControlBoxMenuItem)
{
item.Visible = visible;
}
}
}
private void AddChildForm()
{
childIndex += 1;
ChildForm cf = new ChildForm();
cf.Text += childIndex.ToString();
cf.MdiParent = this;
cf.Show();
}
private class ChildForm : Form
{
public ChildForm()
{
SuspendLayout();
Size = new Size(300, 200);
Text = "MDI Child #";
BackColor = Color.Bisque;
MenuStrip menu = new MenuStrip { Dock = DockStyle.Top };
ToolStripMenuItem someItem = new ToolStripMenuItem { Text = "Child Menu Item", AutoSize = true };
menu.Items.Add(someItem);
menu.Visible = false;
Controls.Add(menu);
ResumeLayout(true);
}
}
}
}
Upvotes: 4
Reputation: 63772
MDI doesn't support modifying the window chrome, so you can't really do this. You'll need to either manually handle the corresponding window messages (loads of work), or fake the whole "window" thing (e.g. use a panel to which you add controls instead of MDI).
Upvotes: 2