Csharp/CSharp Tutorial/GUI Windows Forms/MenuItem — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 18:31, 26 мая 2010
Содержание
Add action event handler to MenuItem
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class MenuItemEventOption : System.Windows.Forms.Form {
private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem mnuNew; private System.Windows.Forms.MenuItem mnuFile; private System.Windows.Forms.MenuItem mnuFileOpen; private System.Windows.Forms.MenuItem mnuFileClose; private System.Windows.Forms.MenuItem mnuFileSave; private System.Windows.Forms.MenuItem mnuFileSaveAs; private System.Windows.Forms.MenuItem mnuEdit; private System.Windows.Forms.MenuItem mnuEditCopy; private System.Windows.Forms.MenuItem mnuEditPaste; private System.Windows.Forms.MenuItem mnuOption1; private System.Windows.Forms.MenuItem mnuOption2; private System.Windows.Forms.MenuItem mnuOption3; private System.Windows.Forms.MenuItem mnuROption1; private System.Windows.Forms.MenuItem mnuROption2; private System.Windows.Forms.MenuItem mnuROption3; private System.Windows.Forms.MenuItem mnuWindow; private System.Windows.Forms.MenuItem mnuOptions; private System.Windows.Forms.MenuItem mnuRadioOptions; private System.ruponentModel.Container components = null; public MenuItemEventOption() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.mnuFile = new System.Windows.Forms.MenuItem(); this.mnuNew = new System.Windows.Forms.MenuItem(); this.mnuFileOpen = new System.Windows.Forms.MenuItem(); this.mnuFileClose = new System.Windows.Forms.MenuItem(); this.mnuFileSave = new System.Windows.Forms.MenuItem(); this.mnuFileSaveAs = new System.Windows.Forms.MenuItem(); this.mnuEdit = new System.Windows.Forms.MenuItem(); this.mnuEditCopy = new System.Windows.Forms.MenuItem(); this.mnuEditPaste = new System.Windows.Forms.MenuItem(); this.mnuOptions = new System.Windows.Forms.MenuItem(); this.mnuOption1 = new System.Windows.Forms.MenuItem(); this.mnuOption2 = new System.Windows.Forms.MenuItem(); this.mnuOption3 = new System.Windows.Forms.MenuItem(); this.mnuRadioOptions = new System.Windows.Forms.MenuItem(); this.mnuROption1 = new System.Windows.Forms.MenuItem(); this.mnuROption2 = new System.Windows.Forms.MenuItem(); this.mnuROption3 = new System.Windows.Forms.MenuItem(); this.mnuWindow = new System.Windows.Forms.MenuItem(); this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuFile, this.mnuEdit, this.mnuOptions, this.mnuRadioOptions, this.mnuWindow}); this.mnuFile.Index = 0; this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuNew, this.mnuFileOpen, this.mnuFileClose, this.mnuFileSave, this.mnuFileSaveAs}); this.mnuFile.Text = "File"; this.mnuNew.Index = 0; this.mnuNew.Shortcut = System.Windows.Forms.Shortcut.CtrlN; this.mnuNew.Text = "&New"; this.mnuNew.Click += new System.EventHandler(this.mnuNew_Click); this.mnuFileOpen.Index = 1; this.mnuFileOpen.Text = "Open"; this.mnuFileOpen.Click += new System.EventHandler(this.mnuFileOpen_Click); // // mnuFileClose // this.mnuFileClose.Index = 2; this.mnuFileClose.Text = "Close"; this.mnuFileClose.Click += new System.EventHandler(this.mnuFileClose_Click); // // mnuFileSave // this.mnuFileSave.Index = 3; this.mnuFileSave.Text = "Save"; this.mnuFileSave.Click += new System.EventHandler(this.mnuFileSave_Click); // // mnuFileSaveAs // this.mnuFileSaveAs.Index = 4; this.mnuFileSaveAs.Text = "Save&As"; this.mnuFileSaveAs.Click += new System.EventHandler(this.mnuFileSaveAs_Click); // // mnuEdit // this.mnuEdit.Index = 1; this.mnuEdit.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuEditCopy, this.mnuEditPaste}); this.mnuEdit.Text = "Edit"; // // mnuEditCopy // this.mnuEditCopy.Index = 0; this.mnuEditCopy.Text = "&Copy"; this.mnuEditCopy.Click += new System.EventHandler(this.mnuEditCopy_Click); // // mnuEditPaste // this.mnuEditPaste.Index = 1; this.mnuEditPaste.Text = "Paste"; this.mnuEditPaste.Click += new System.EventHandler(this.mnuEditPaste_Click); // // mnuOptions // this.mnuOptions.Index = 2; this.mnuOptions.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuOption1, this.mnuOption2, this.mnuOption3}); this.mnuOptions.Text = "Options"; // // mnuOption1 // this.mnuOption1.Index = 0; this.mnuOption1.Text = "Option1"; this.mnuOption1.Click += new System.EventHandler(this.Option_Click); // // mnuOption2 // this.mnuOption2.Index = 1; this.mnuOption2.Text = "Option2"; this.mnuOption2.Click += new System.EventHandler(this.Option_Click); // // mnuOption3 // this.mnuOption3.Index = 2; this.mnuOption3.Text = "Option3"; this.mnuOption3.Click += new System.EventHandler(this.Option_Click); // // mnuRadioOptions // this.mnuRadioOptions.Index = 3; this.mnuRadioOptions.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuROption1, this.mnuROption2, this.mnuROption3}); this.mnuRadioOptions.Text = "Radio Options"; // // mnuROption1 // this.mnuROption1.Index = 0; this.mnuROption1.RadioCheck = true; this.mnuROption1.Text = "Radio Option 1"; this.mnuROption1.Click += new System.EventHandler(this.RadioOption_Click); // // mnuROption2 // this.mnuROption2.Index = 1; this.mnuROption2.RadioCheck = true; this.mnuROption2.Text = "Radio Option 2"; this.mnuROption2.Click += new System.EventHandler(this.RadioOption_Click); // // mnuROption3 // this.mnuROption3.Index = 2; this.mnuROption3.RadioCheck = true; this.mnuROption3.Text = "Radio Option 3"; this.mnuROption3.Click += new System.EventHandler(this.RadioOption_Click); // // mnuWindow // this.mnuWindow.Index = 4; this.mnuWindow.MdiList = true; this.mnuWindow.Text = "&Window"; // // MenuItemEventOption // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 186); this.IsMdiContainer = true; this.Menu = this.mainMenu1; this.Name = "MenuItemEventOption"; this.Text = "MenuItemEventOption"; } [STAThread] static void Main() { Application.Run(new MenuItemEventOption()); }
private void mnuNew_Click(object sender, System.EventArgs e) { } private void mnuFileOpen_Click(object sender, System.EventArgs e) { MessageBox.Show ("You clicked File Open", "Menu Event Tester", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } private void mnuFileClose_Click(object sender, System.EventArgs e) { MessageBox.Show ("You clicked File Close", "Menu Event Tester", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } private void Option_Click(object sender, System.EventArgs e) { MenuItem item = sender as MenuItem; if ( item != null ) { item.Checked = ! item.Checked; } } private void RadioOption_Click(object sender, System.EventArgs e) { MenuItem item = sender as MenuItem; Menu parent = item.Parent; if ( item != null ) { foreach ( MenuItem mi in parent.MenuItems ) mi.Checked = false; item.Checked = true; } } private void mnuEditCopy_Click(object sender, System.EventArgs e) { MessageBox.Show ("You clicked Edit Copy", "Menu Event Tester", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } private void mnuEditPaste_Click(object sender, System.EventArgs e) { MessageBox.Show ("You clicked Edit Paste", "Menu Event Tester", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } private void mnuFileSave_Click(object sender, System.EventArgs e) { MessageBox.Show ("You clicked Save", "Menu Event Tester", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } private void mnuFileSaveAs_Click(object sender, System.EventArgs e) { MessageBox.Show ("You clicked SaveAs", "Menu Event Tester", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); }
}</source>
new MenuItem()
<source lang="csharp">using System; using System.Drawing; using System.Windows.Forms;
class FirstMainMenu: Form {
public static void Main() { Application.Run(new FirstMainMenu()); } public FirstMainMenu() { MenuItem miOpen = new MenuItem("&Open...", new EventHandler(MenuFileOpenOnClick), Shortcut.CtrlO); MenuItem miSave = new MenuItem("&Save", new EventHandler(MenuFileSaveOnClick), Shortcut.CtrlS); MenuItem miSaveAs = new MenuItem("Save &As...", new EventHandler(MenuFileSaveAsOnClick)); MenuItem miDash = new MenuItem("-"); MenuItem miExit = new MenuItem("E&xit", new EventHandler(MenuFileExitOnClick)); MenuItem miFile = new MenuItem("&File", new MenuItem[] {miOpen, miSave, miSaveAs, miDash, miExit }); MenuItem miCut = new MenuItem("Cu&t", new EventHandler(MenuEditCutOnClick), Shortcut.CtrlX); MenuItem miCopy = new MenuItem("&Copy", new EventHandler(MenuEditCopyOnClick), Shortcut.CtrlC); MenuItem miPaste = new MenuItem("&Paste", new EventHandler(MenuEditPasteOnClick), Shortcut.CtrlV); MenuItem miEdit = new MenuItem("&Edit", new MenuItem[] {miCut, miCopy, miPaste}); MenuItem miAbout = new MenuItem("&About FirstMainMenu...", new EventHandler(MenuHelpAboutOnClick)); MenuItem miHelp = new MenuItem("&Help", new MenuItem[] {miAbout}); Menu = new MainMenu(new MenuItem[] {miFile, miEdit, miHelp}); } void MenuFileOpenOnClick(object obj, EventArgs ea) { MessageBox.Show("File Open item clicked!", Text); } void MenuFileSaveOnClick(object obj, EventArgs ea) { MessageBox.Show("File Save item clicked!", Text); } void MenuFileSaveAsOnClick(object obj, EventArgs ea) { MessageBox.Show("File Save As item clicked!", Text); } void MenuFileExitOnClick(object obj, EventArgs ea) { Close(); } void MenuEditCutOnClick(object obj, EventArgs ea) { MessageBox.Show("Edit Cut item clicked!", Text); } void MenuEditCopyOnClick(object obj, EventArgs ea) { MessageBox.Show("Edit Copy item clicked!", Text); } void MenuEditPasteOnClick(object obj, EventArgs ea) { MessageBox.Show("Edit Paste item clicked!", Text); } void MenuHelpAboutOnClick(object obj, EventArgs ea) { MessageBox.Show(Text + " ?"); }
}</source>
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class MenuItemEventOption : System.Windows.Forms.Form {
private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem mnuNew; private System.Windows.Forms.MenuItem mnuFile; private System.Windows.Forms.MenuItem mnuFileOpen; private System.Windows.Forms.MenuItem mnuFileClose; private System.Windows.Forms.MenuItem mnuFileSave; private System.Windows.Forms.MenuItem mnuFileSaveAs; private System.Windows.Forms.MenuItem mnuEdit; private System.Windows.Forms.MenuItem mnuEditCopy; private System.Windows.Forms.MenuItem mnuEditPaste; private System.Windows.Forms.MenuItem mnuOption1; private System.Windows.Forms.MenuItem mnuOption2; private System.Windows.Forms.MenuItem mnuOption3; private System.Windows.Forms.MenuItem mnuROption1; private System.Windows.Forms.MenuItem mnuROption2; private System.Windows.Forms.MenuItem mnuROption3; private System.Windows.Forms.MenuItem mnuWindow; private System.Windows.Forms.MenuItem mnuOptions; private System.Windows.Forms.MenuItem mnuRadioOptions; private System.ruponentModel.Container components = null; public MenuItemEventOption() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.mnuFile = new System.Windows.Forms.MenuItem(); this.mnuNew = new System.Windows.Forms.MenuItem(); this.mnuFileOpen = new System.Windows.Forms.MenuItem(); this.mnuFileClose = new System.Windows.Forms.MenuItem(); this.mnuFileSave = new System.Windows.Forms.MenuItem(); this.mnuFileSaveAs = new System.Windows.Forms.MenuItem(); this.mnuEdit = new System.Windows.Forms.MenuItem(); this.mnuEditCopy = new System.Windows.Forms.MenuItem(); this.mnuEditPaste = new System.Windows.Forms.MenuItem(); this.mnuOptions = new System.Windows.Forms.MenuItem(); this.mnuOption1 = new System.Windows.Forms.MenuItem(); this.mnuOption2 = new System.Windows.Forms.MenuItem(); this.mnuOption3 = new System.Windows.Forms.MenuItem(); this.mnuRadioOptions = new System.Windows.Forms.MenuItem(); this.mnuROption1 = new System.Windows.Forms.MenuItem(); this.mnuROption2 = new System.Windows.Forms.MenuItem(); this.mnuROption3 = new System.Windows.Forms.MenuItem(); this.mnuWindow = new System.Windows.Forms.MenuItem(); this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuFile, this.mnuEdit, this.mnuOptions, this.mnuRadioOptions, this.mnuWindow}); this.mnuFile.Index = 0; this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuNew, this.mnuFileOpen, this.mnuFileClose, this.mnuFileSave, this.mnuFileSaveAs}); this.mnuFile.Text = "File"; this.mnuNew.Index = 0; this.mnuNew.Shortcut = System.Windows.Forms.Shortcut.CtrlN; this.mnuNew.Text = "&New"; this.mnuNew.Click += new System.EventHandler(this.mnuNew_Click); this.mnuFileOpen.Index = 1; this.mnuFileOpen.Text = "Open"; this.mnuFileOpen.Click += new System.EventHandler(this.mnuFileOpen_Click); // // mnuFileClose // this.mnuFileClose.Index = 2; this.mnuFileClose.Text = "Close"; this.mnuFileClose.Click += new System.EventHandler(this.mnuFileClose_Click); // // mnuFileSave // this.mnuFileSave.Index = 3; this.mnuFileSave.Text = "Save"; this.mnuFileSave.Click += new System.EventHandler(this.mnuFileSave_Click); // // mnuFileSaveAs // this.mnuFileSaveAs.Index = 4; this.mnuFileSaveAs.Text = "Save&As"; this.mnuFileSaveAs.Click += new System.EventHandler(this.mnuFileSaveAs_Click); // // mnuEdit // this.mnuEdit.Index = 1; this.mnuEdit.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuEditCopy, this.mnuEditPaste}); this.mnuEdit.Text = "Edit"; // // mnuEditCopy // this.mnuEditCopy.Index = 0; this.mnuEditCopy.Text = "&Copy"; this.mnuEditCopy.Click += new System.EventHandler(this.mnuEditCopy_Click); // // mnuEditPaste // this.mnuEditPaste.Index = 1; this.mnuEditPaste.Text = "Paste"; this.mnuEditPaste.Click += new System.EventHandler(this.mnuEditPaste_Click); // // mnuOptions // this.mnuOptions.Index = 2; this.mnuOptions.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuOption1, this.mnuOption2, this.mnuOption3}); this.mnuOptions.Text = "Options"; // // mnuOption1 // this.mnuOption1.Index = 0; this.mnuOption1.Text = "Option1"; this.mnuOption1.Click += new System.EventHandler(this.Option_Click); // // mnuOption2 // this.mnuOption2.Index = 1; this.mnuOption2.Text = "Option2"; this.mnuOption2.Click += new System.EventHandler(this.Option_Click); // // mnuOption3 // this.mnuOption3.Index = 2; this.mnuOption3.Text = "Option3"; this.mnuOption3.Click += new System.EventHandler(this.Option_Click); // // mnuRadioOptions // this.mnuRadioOptions.Index = 3; this.mnuRadioOptions.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuROption1, this.mnuROption2, this.mnuROption3}); this.mnuRadioOptions.Text = "Radio Options"; // // mnuROption1 // this.mnuROption1.Index = 0; this.mnuROption1.RadioCheck = true; this.mnuROption1.Text = "Radio Option 1"; this.mnuROption1.Click += new System.EventHandler(this.RadioOption_Click); // // mnuROption2 // this.mnuROption2.Index = 1; this.mnuROption2.RadioCheck = true; this.mnuROption2.Text = "Radio Option 2"; this.mnuROption2.Click += new System.EventHandler(this.RadioOption_Click); // // mnuROption3 // this.mnuROption3.Index = 2; this.mnuROption3.RadioCheck = true; this.mnuROption3.Text = "Radio Option 3"; this.mnuROption3.Click += new System.EventHandler(this.RadioOption_Click); // // mnuWindow // this.mnuWindow.Index = 4; this.mnuWindow.MdiList = true; this.mnuWindow.Text = "&Window"; // // MenuItemEventOption // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 186); this.IsMdiContainer = true; this.Menu = this.mainMenu1; this.Name = "MenuItemEventOption"; this.Text = "MenuItemEventOption"; } [STAThread] static void Main() { Application.Run(new MenuItemEventOption()); }
private void mnuNew_Click(object sender, System.EventArgs e) { } private void mnuFileOpen_Click(object sender, System.EventArgs e) { MessageBox.Show ("You clicked File Open", "Menu Event Tester", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } private void mnuFileClose_Click(object sender, System.EventArgs e) { MessageBox.Show ("You clicked File Close", "Menu Event Tester", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } private void Option_Click(object sender, System.EventArgs e) { MenuItem item = sender as MenuItem; if ( item != null ) { item.Checked = ! item.Checked; } } private void RadioOption_Click(object sender, System.EventArgs e) { MenuItem item = sender as MenuItem; Menu parent = item.Parent; if ( item != null ) { foreach ( MenuItem mi in parent.MenuItems ) mi.Checked = false; item.Checked = true; } } private void mnuEditCopy_Click(object sender, System.EventArgs e) { MessageBox.Show ("You clicked Edit Copy", "Menu Event Tester", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } private void mnuEditPaste_Click(object sender, System.EventArgs e) { MessageBox.Show ("You clicked Edit Paste", "Menu Event Tester", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } private void mnuFileSave_Click(object sender, System.EventArgs e) { MessageBox.Show ("You clicked Save", "Menu Event Tester", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } private void mnuFileSaveAs_Click(object sender, System.EventArgs e) { MessageBox.Show ("You clicked SaveAs", "Menu Event Tester", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); }
}</source>
Owner Drawn MenuItem
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class MenuItemOwnerDraw : System.Windows.Forms.Form {
private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem mnuNew; private System.Windows.Forms.MenuItem mnuFile; private System.Windows.Forms.MenuItem mnuFileClose; private System.Windows.Forms.MenuItem mnuFileSave; private System.Windows.Forms.MenuItem mnuFileSaveAs; private System.Windows.Forms.MenuItem mnuEdit; private System.Windows.Forms.MenuItem mnuEditCopy; private System.Windows.Forms.MenuItem mnuEditPaste; private System.Windows.Forms.MenuItem mnuOption1; private System.Windows.Forms.MenuItem mnuOption2; private System.Windows.Forms.MenuItem mnuOption3; private System.Windows.Forms.MenuItem mnuROption1; private System.Windows.Forms.MenuItem mnuROption2; private System.Windows.Forms.MenuItem mnuROption3; private System.Windows.Forms.MenuItem mnuWindow; private System.Windows.Forms.MenuItem mnuOptions; private System.Windows.Forms.MenuItem mnuRadioOptions; private System.Windows.Forms.MenuItem mnuMenu1; private System.Windows.Forms.MenuItem mnuMenu11; private System.Windows.Forms.MenuItem mnuMenu12; private System.Windows.Forms.MenuItem mnuMenu13; private System.Windows.Forms.MenuItem mnuMenu14; private System.Windows.Forms.MenuItem mnuMenu2; private System.Windows.Forms.MenuItem mnuMenu21; private System.Windows.Forms.MenuItem mnuMenu22; private System.Windows.Forms.MenuItem mnuMenu23; private System.Windows.Forms.MenuItem mnuMenu24; private System.Windows.Forms.MenuItem mnuMerge; private System.Windows.Forms.MenuItem mnuODShazam; private string[] files = { @"YourFile.bmp", @"YourFile.bmp" }; private System.Windows.Forms.MenuItem mnuODVote; private System.Windows.Forms.MenuItem mnuSpecial; private System.ruponentModel.Container components = null; public MenuItemOwnerDraw() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.mnuFile = new System.Windows.Forms.MenuItem(); this.mnuNew = new System.Windows.Forms.MenuItem(); this.mnuFileClose = new System.Windows.Forms.MenuItem(); this.mnuFileSave = new System.Windows.Forms.MenuItem(); this.mnuFileSaveAs = new System.Windows.Forms.MenuItem(); this.mnuEdit = new System.Windows.Forms.MenuItem(); this.mnuEditCopy = new System.Windows.Forms.MenuItem(); this.mnuEditPaste = new System.Windows.Forms.MenuItem(); this.mnuOptions = new System.Windows.Forms.MenuItem(); this.mnuOption1 = new System.Windows.Forms.MenuItem(); this.mnuOption2 = new System.Windows.Forms.MenuItem(); this.mnuOption3 = new System.Windows.Forms.MenuItem(); this.mnuRadioOptions = new System.Windows.Forms.MenuItem(); this.mnuROption1 = new System.Windows.Forms.MenuItem(); this.mnuROption2 = new System.Windows.Forms.MenuItem(); this.mnuROption3 = new System.Windows.Forms.MenuItem(); this.mnuSpecial = new System.Windows.Forms.MenuItem(); this.mnuODVote = new System.Windows.Forms.MenuItem(); this.mnuODShazam = new System.Windows.Forms.MenuItem(); this.mnuWindow = new System.Windows.Forms.MenuItem(); this.mnuMenu1 = new System.Windows.Forms.MenuItem(); this.mnuMenu11 = new System.Windows.Forms.MenuItem(); this.mnuMenu12 = new System.Windows.Forms.MenuItem(); this.mnuMenu13 = new System.Windows.Forms.MenuItem(); this.mnuMenu14 = new System.Windows.Forms.MenuItem(); this.mnuMerge = new System.Windows.Forms.MenuItem(); this.mnuMenu2 = new System.Windows.Forms.MenuItem(); this.mnuMenu21 = new System.Windows.Forms.MenuItem(); this.mnuMenu22 = new System.Windows.Forms.MenuItem(); this.mnuMenu23 = new System.Windows.Forms.MenuItem(); this.mnuMenu24 = new System.Windows.Forms.MenuItem(); // // mainMenu1 // this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuFile, this.mnuEdit, this.mnuOptions, this.mnuRadioOptions, this.mnuSpecial, this.mnuWindow, this.mnuMenu1, this.mnuMenu2}); // // mnuFile // this.mnuFile.Index = 0; this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuNew, this.mnuFileClose, this.mnuFileSave, this.mnuFileSaveAs}); this.mnuFile.Text = "File"; // // mnuNew // this.mnuNew.Index = 0; this.mnuNew.Shortcut = System.Windows.Forms.Shortcut.CtrlN; this.mnuNew.Text = "&New"; this.mnuNew.Click += new System.EventHandler(this.mnuNew_Click); // // mnuFileClose // this.mnuFileClose.Index = 2; this.mnuFileClose.Text = "Close"; // // mnuFileSave // this.mnuFileSave.Index = 3; this.mnuFileSave.Text = "Save"; this.mnuFileSave.Click += new System.EventHandler(this.mnuFileSave_Click); // // mnuFileSaveAs // this.mnuFileSaveAs.Index = 4; this.mnuFileSaveAs.Text = "Save&As"; this.mnuFileSaveAs.Click += new System.EventHandler(this.mnuFileSaveAs_Click); // // mnuEdit // this.mnuEdit.Index = 1; this.mnuEdit.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuEditCopy, this.mnuEditPaste}); this.mnuEdit.Text = "Edit"; // // mnuEditCopy // this.mnuEditCopy.Index = 0; this.mnuEditCopy.Text = "&Copy"; this.mnuEditCopy.Click += new System.EventHandler(this.mnuEditCopy_Click); // // mnuEditPaste // this.mnuEditPaste.Index = 1; this.mnuEditPaste.Text = "Paste"; this.mnuEditPaste.Click += new System.EventHandler(this.mnuEditPaste_Click); // // mnuOptions // this.mnuOptions.Index = 2; this.mnuOptions.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuOption1, this.mnuOption2, this.mnuOption3}); this.mnuOptions.Text = "Options"; // // mnuOption1 // this.mnuOption1.Index = 0; this.mnuOption1.Text = "Option1"; this.mnuOption1.Click += new System.EventHandler(this.Option_Click); // // mnuOption2 // this.mnuOption2.Index = 1; this.mnuOption2.Text = "Option2"; this.mnuOption2.Click += new System.EventHandler(this.Option_Click); // // mnuOption3 // this.mnuOption3.Index = 2; this.mnuOption3.Text = "Option3"; this.mnuOption3.Click += new System.EventHandler(this.Option_Click); // // mnuRadioOptions // this.mnuRadioOptions.Index = 3; this.mnuRadioOptions.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuROption1, this.mnuROption2, this.mnuROption3}); this.mnuRadioOptions.Text = "Radio Options"; // // mnuROption1 // this.mnuROption1.Index = 0; this.mnuROption1.RadioCheck = true; this.mnuROption1.Text = "Radio Option 1"; this.mnuROption1.Click += new System.EventHandler(this.RadioOption_Click); // // mnuROption2 // this.mnuROption2.Index = 1; this.mnuROption2.RadioCheck = true; this.mnuROption2.Text = "Radio Option 2"; this.mnuROption2.Click += new System.EventHandler(this.RadioOption_Click); // // mnuROption3 // this.mnuROption3.Index = 2; this.mnuROption3.RadioCheck = true; this.mnuROption3.Text = "Radio Option 3"; this.mnuROption3.Click += new System.EventHandler(this.RadioOption_Click); // // mnuSpecial // this.mnuSpecial.Index = 4; this.mnuSpecial.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuODVote, this.mnuODShazam}); this.mnuSpecial.Text = "Special"; // // mnuODVote // this.mnuODVote.Index = 0; this.mnuODVote.OwnerDraw = true; this.mnuODVote.Text = "Vote"; this.mnuODVote.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.OnDrawItem); this.mnuODVote.Click += new System.EventHandler(this.mnuODDraw_Click); this.mnuODVote.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.OnMeasureItem); // // mnuODShazam // this.mnuODShazam.Index = 1; this.mnuODShazam.OwnerDraw = true; this.mnuODShazam.Text = "Shazam"; this.mnuODShazam.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.OnDrawItem); this.mnuODShazam.Click += new System.EventHandler(this.mnuODDraw_Click); this.mnuODShazam.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.OnMeasureItem); // // mnuWindow // this.mnuWindow.Index = 5; this.mnuWindow.MdiList = true; this.mnuWindow.MergeOrder = 99; this.mnuWindow.Text = "&Window"; // // mnuMenu1 // this.mnuMenu1.Index = 6; this.mnuMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuMenu11, this.mnuMenu12, this.mnuMenu13, this.mnuMenu14, this.mnuMerge}); this.mnuMenu1.Text = "Menu 1"; // // mnuMenu11 // this.mnuMenu11.Index = 0; this.mnuMenu11.MergeOrder = 1; this.mnuMenu11.Text = "Menu 1.1"; // // mnuMenu12 // this.mnuMenu12.Index = 1; this.mnuMenu12.MergeOrder = 2; this.mnuMenu12.Text = "Menu 1.2"; // // mnuMenu13 // this.mnuMenu13.Index = 2; this.mnuMenu13.MergeOrder = 3; this.mnuMenu13.Text = "Menu 1.3"; // // mnuMenu14 // this.mnuMenu14.Index = 3; this.mnuMenu14.MergeOrder = 4; this.mnuMenu14.Text = "Menu 1.4"; // // mnuMerge // this.mnuMerge.Index = 4; this.mnuMerge.MergeOrder = 99; this.mnuMerge.Text = "Merge!"; this.mnuMerge.Click += new System.EventHandler(this.mnuMerge_Click); // // mnuMenu2 // this.mnuMenu2.Index = 7; this.mnuMenu2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuMenu21, this.mnuMenu22, this.mnuMenu23, this.mnuMenu24}); this.mnuMenu2.Text = "Menu 2"; // // mnuMenu21 // this.mnuMenu21.Index = 0; this.mnuMenu21.MergeOrder = 1; this.mnuMenu21.Text = "Menu 2.1"; // // mnuMenu22 // this.mnuMenu22.Index = 1; this.mnuMenu22.MergeOrder = 2; this.mnuMenu22.MergeType = System.Windows.Forms.MenuMerge.Replace; this.mnuMenu22.Text = "Menu 2.2"; // // mnuMenu23 // this.mnuMenu23.Index = 2; this.mnuMenu23.MergeOrder = 3; this.mnuMenu23.MergeType = System.Windows.Forms.MenuMerge.Remove; this.mnuMenu23.Text = "Menu 2.3"; // // mnuMenu24 // this.mnuMenu24.Index = 3; this.mnuMenu24.MergeOrder = 5; this.mnuMenu24.Text = "Menu 2.4"; // // MenuItemOwnerDraw // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(536, 148); this.IsMdiContainer = true; this.Menu = this.mainMenu1; this.Name = "MenuItemOwnerDraw"; this.Text = "MenuItemOwnerDraw"; } [STAThread] static void Main() { Application.Run(new MenuItemOwnerDraw()); }
private void mnuNew_Click(object sender, System.EventArgs e) { } private void Option_Click(object sender, System.EventArgs e) { } private void RadioOption_Click(object sender, System.EventArgs e) { } private void mnuEditCopy_Click(object sender, System.EventArgs e) { } private void mnuEditPaste_Click(object sender, System.EventArgs e) { } private void mnuFileSave_Click(object sender, System.EventArgs e) { } private void mnuFileSaveAs_Click(object sender, System.EventArgs e) { } private void mnuMerge_Click(object sender, System.EventArgs e) { } private void OnDrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { Image img = Image.FromFile(files[e.Index]); Rectangle r = e.Bounds; Pen pen = new Pen(e.BackColor,2); r.Inflate(-6,-6); e.Graphics.DrawRectangle(pen,r); e.Graphics.DrawImage(img,r); } private void OnMeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e) { Image img = Image.FromFile(files[e.Index]); e.ItemHeight = img.Height; e.ItemWidth = img.Width; } private void mnuODDraw_Click(object sender, System.EventArgs e) { MenuItem item = sender as MenuItem; if ( item != null ) { string choice = item.Text; MessageBox.Show ("You clicked " + choice, "Menu Event Tester", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } }
}</source>