下面是该类的具体实现:
using System; using System.Linq; using System.Collections.Generic; using System.Collections; using System.Text; using System.ComponentModel; using System.Windows.Forms; namespace LPGMobileDevice { class PanelMenu { private Panel panel; public Panel Panel { get { return panel; } } private MainMenu menu; public MainMenu Menu { get { return menu; } } private MenuItem mi_Main, mi_Exit; public MenuItem Mi_Main { get { return mi_Main; } } public MenuItem Mi_Exit { get { return mi_Exit; } } public PanelMenu() { panel = new Panel(); //System.Drawing.Point point=new System.Drawing.Point(0,0); //panel.Location = point; System.Drawing.Size size = new System.Drawing.Size(240, 25); panel.Size = size; menu = new MainMenu(); mi_Main = new MenuItem(); mi_Main.Text = "主页"; mi_Exit = new MenuItem(); mi_Exit.Text = "退出"; menu.MenuItems.Add(mi_Main); menu.MenuItems.Add(mi_Exit); mi_Main.Click += new EventHandler(Main); mi_Exit.Click+=new EventHandler(Exit); } private void Main(object sender, EventArgs e) { MainForm mf = new MainForm(); mf.ShowDialog(); } private void Exit(object sender, EventArgs e) { //Application.Exit(); System.Diagnostics.Process.GetCurrentProcess().Kill(); } } }
在其他窗体中调用:
PanelMenu pm = new PanelMenu(); this.Menu = pm.Menu; this.Controls.Add(pm.Panel);
代码重用能减少大量的控件拖拽和事件的编写。
但我感觉还是有更方便使用的方法,望各位指导!