How to combine the child form menu into the main form in the MDI program

xiaoxiao2021-03-14  191

Here is a simple way to merge in MDI, the submenu and the parent menu.

First in the MDI's main form:

1. In the main form constructor, add the following code, pay attention to after the InitializationComponent call, such as:

// Add file menu

Menuitem Mifile = Mnumain.MenuItems.Add ("& File");

MiFile.mergeRder = 1;

MiFile.mergetype = meNuMerge.mergeitems;

Menuitem Miadddoc = New Menuitem ("& Add Document",

New EventHandler (this.fileadd_clicked),

Shortcut.ctrla);

Miadddoc.mergerder = 100;

Menuitem Miexit = New Menuitem ("E & XIT",

New EventHandler (this.fileexit_clicked),

Shortcut.ctrlx);

MIEXIT.MERGEORDER = 110;

MiFile.Menuitems.Add (MIADDDOC);

MiFile.Menuitems.Add ("-"); // Give a SEPERATOR

MiFile.MenuItems.Add (MIEXIT);

// Add window menu

Menuitem miwindow = mnumain.MenuItems.Add ("& window");

MiWindow.mergerder = 10;

MiWindow.MenuItems.Add ("& cascade",

New EventHandler (this.windowcascade_clicked);

MiWindow.MenuItems.Add ("Tile & Horizontal",

New EventHandler (this.windowtileh_clicked);

MiWindow.MenuItems.Add ("Tile & Vertical",

New EventHandler (this.windowtilev_clicked);

Miwindow.mdilist = true; // adds the mdi window list to the bottom of the menu

2. Then define functions for the corresponding MENU_CLICK events, such as:

#REGON MENU_CLICK_MESSAGE_HANDLE

// file-> add menu item handler

Protected void filed_clicked (Object Sender, System.EventArgs E)

{

Addmdichild (); // Create New MDI CHild Window

}

// file-> EXIT MENU ITEM HANDLER

Protected Void FileExit_Clicked (Object Sender, System.EventArgs E)

{

THIS.CLOSE ();

}

// Window-> Cascade Menu Item Handler

Protected void windowcascade_clicked (Object Sender, System.EventArgs E)

{

This.Layoutmdi (MDILAYOUT.CASCADE);

}

// window-> Tile Horizontally Menu Item Handler

Protected void windowtileh_clicked (Object Sender, System.EventArgs E)

{

THIS.LAYOUTMDI (MDILAYOUT.TILEHORIZONTAL);

}

// Window-> Tile Vertical Menu Item Handler

Protected void windowtilev_clicked (Object Sender, System.EventArgs E)

{

THIS.LAYOUTMDI (MDILAYOUT.TILEVERTICAL);

}

#ndregion {menu_click_message_handle}

3. Create a child form, such as:

// Add a new child window if it doesn't ket; else set focus on it

Private void addmdichild ()

{

// mychildform is the one I'm looking for

Mdichild mymdichild = null;

FOREACH (Form f in this.mdichildren)

{

IF (f is mdichild)

{

// Found IT

Mymdichild = (mdichild) f;

Break;

}

}

IF (MyMDichild! = NULL)

{

Mymdichild.show ();

Mymdichild.focus ();

}

Else

{

Mymdichild = new mdichild ();

Mymdichild.mdiparent = this;

Mymdichild.show ();

Mymdichild.focus ();

}

}

When the above step is completed, the effect of the main form is as shown in the following figure:

Then in the child form, the specific operation is as follows:

1. In the sub-form constructor, add the following code, which is also necessary after the InitializationComponent call, such as:

// Add file menu

Menuitem mifile = mnuchildmain.MenuItems.Add ("& file");

MiFile.mergetype = meNuMerge.mergeitems;

MiFile.mergeRder = 1;

Menuitem Miloaddoc = mifile.Menuitems.Add ("& loading Document",

New EventHandler (this.loadDocument_clicked);

MiLoadDoc.mergerder = 105;

// add formatting menu

Menuitem Miformat = Mnuchildmain.MenuItems.Add ("F & Ormat");

MiFormat.mergetype = meNuMerge.Add;

MiFormat.mergerder = 5;

// Font Face Sub-Menu

Misansserif = New MenuItem ("& 1." SansseriffontFamily.Name,

New EventHandler (this.formatFont_Clicked);

MiSerif = New Menuitem ("& 2." SeriffontFamily.Name, New EventHandler (this.formatfont_clicked);

Mimonospace = New Menuitem ("& 3." MonospaceFontFamily.Name,

New EventHandler (this.formatFont_Clicked);

Misansserif.checked = true;

MiFormatFontChecked = Misansserif;

Misansserif.defaultItem = TRUE;

MiFormat.MenuItems.Add ("Font & Face"

, (New Menuitem [] {Misansserif, miserif, mimonospace}

);

// Font Size Sub-Menu

Mismall = New Menuitem ("& Small", New EventHandler (this.FormatSize_clicked);

MIMEDIUM = New Menuitem ("& Medium", New EventHandler (this.formatsize_clicked);

Milarge = New Menuitem ("& Large", New EventHandler (this.formatsize_clicked);

Mimedium.checked = true;

mimedium.defaultItem = true;

MiFormatSizeChecked = mimedium;

MiFormat.Menuitems.Add ("Font & Size"

, (New Menuitem [] {mismall, mimedium, milage})

);

2. Then define functions for the corresponding MENU_CLICK events, such as:

#REGON MENU_CLICK_MESSAGE_HANDLE

// file-> loading document Menu Item Handler

Protected void loadDocument_clicked (Object Sender, System.EventArgs E)

{

Messagebox.show (this.text);

}

// Format-> Font Menu Item Handler

Protected Void FormatFont_Clicked (Object Sender, System.Eventargs E)

{

Menuitem Miclicked = (MenuItem) Sender;

MiclicKed.checked = true;

MiFormatFontChecked.checked = false;

MiFormatFontChecked = miclicked;

IF (miclicked == misansserif)

{

Currentfontfamily = SansseriffontFamily;

}

Else if (Miclicked == MiSerif)

{

CurrentFontFamily = seriffontfamily;

}

Else

{

CurrentFontFamily = monospacefontfamily;

}

RTBDoc.Font = New font (currentfontfamily, fontsize);

// Format-> Size Menu Item Handler

Protected void formatsize_clicked (Object Sender, System.Eventargs E)

{

Menuitem Miclicked = (MenuItem) Sender;

MiclicKed.checked = true;

MiFormatSizeChecked.checked = false;

MiFormatSizeChecked = miclicked;

IF (miclicked == mismall)

{

FONTSIZE = fontsizes.small;

}

Else IF (miclicked == milage)

{

FONTSIZE = FONTSIZES.LARGE;

}

Else

{

FONTSIZE = fontsizes.medium;

}

RTBDoc.Font = New Font (CurrentFontFamily, Fontsize);

}

#ndregion {menu_click_message_handle}

3. Forms of private members:

Private struct fontsizes

{

Public static float small = 8f;

Public static float medium = 12f;

Public static float large = 24f;

}

PRIVATE FLOAT FONTSIZE = fontsizes.medium;

Private meneuitem miformatfontchecked;

Private menuitem miformsizeChecked;

Private menuitem mismall;

Private menuitem mimedium;

Private menuitem milage;

Private Menuitem Misansserif;

PRIVATE MENUITEM MISERIF;

Private menuitem mimonospace;

PRIVATE FONTFAMILY DIRENTFONTFAMILY;

PRIVATE FONTFAMILY MONOSPACEFONTFAMILY;

PRIVATE FONTFAMILY DIRIFFONTFAMILY

Private system.windows.Forms.mainMenu Mnuchildmain;

PRIVATE FONTFAMILY SERIFFONTFAMILY;

In this way, the code of the child form is also completed, and the effect of the display is as shown in the following figure:

In fact, menu on MDI subsystems and menu menu on the main form, mainly to set MergeORDER for each menu, set different values ​​for MergeORDER of each menu, thus reaching the merged effect. Hereinafter, table description:

Master Fact Mergeorder Subform MergeORDER File (1) Window (10) File (1) Format (5) AddDocument (100)

Load Document (105)

EXIT (110)

Note: The red represents the MergeORDER value of each menu

The effects after the merger are as follows:

Merged Menu File (1) Format (5) Window (10) AddDocument (100)

Load Document (105)

EXIT (110)

Note: The red represents the MergeORDER value of each menu, so if you want to merge on the menu on the MDI subscription, you must meet the desired effects, you must take the MergeORDER between them, which is approximately as follows:

1. If you merge with the original menu column of the main form, first take the MergeORDER value of the root menu with the main formwork corresponding menu, and set the MenuItem's Mergetype for MenuMerge.MergeItems, where the menu is included in this column menu Look at MergeORDER for each menu.

2. If you want to add a new menu column in the main form, MenuItem's Mergetype is a location inserted by the menuMerge. Add, the menu column is inserted, and you want to see MergeORDER.

转载请注明原文地址:https://www.9cbs.com/read-129458.html

New Post(0)