C#完成基於XML設置裝備擺設MenuStrip菜單的辦法。本站提示廣大學習愛好者:(C#完成基於XML設置裝備擺設MenuStrip菜單的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成基於XML設置裝備擺設MenuStrip菜單的辦法正文
本文實例講述了C#完成基於XML設置裝備擺設MenuStrip菜單的辦法。分享給年夜家供年夜家參考。詳細以下:
1.關於本法式的解釋
用XML設置裝備擺設MenuStrip菜單,本法式只是供給一個思緒。法式在Load函數中依據XML文件中的設置靜態生成一個MenuStrip組件的Items聚集。
法式示例以下:
2.應用到的XML文檔示例
<?xml version="1.0" encoding="gb2312"?> <!--MenuStrip:mnsMainMenu控件中的各個菜單設置裝備擺設--> <!--Menu:菜單欄中的各項,Text:顯示稱號,ShortKey:快捷鍵--> <MenuStrip> <Menu Text="文件(File)" ShortKey="Alt+F"> <Item Type="MenuItem" Text="修正窗口色彩"> <Item Type="MenuItem" Text="白色" ShortKey="Shift+F6" FunctionAttribute="ColorToRed" /> <Item Type="MenuItem" Text="綠色" ShortKey="Shift+F7" FunctionAttribute="ColorToLawnGreen" /> </Item> <Item Type="Seperator" /> <Item Type="MenuItem" Text="通俗" ShortKey="Ctrl+Shift+X" FunctionAttribute="FormNormal" /> <Item Type="MenuItem" Text="最小化" ShortKey="Ctrl+Shift+Y" FunctionAttribute="FormMinimize" /> <Item Type="MenuItem" Text="最年夜化" ShortKey="Ctrl+Shift+Z" FunctionAttribute="FormMaximize" /> <Item Type="Seperator" /> <Item Type="MenuItem" Text="封閉" ShortKey="Alt+F4" FunctionAttribute="FormClose" /> </Menu> <Menu Text="贊助(Help)" ShortKey="Alt+F"> <Item Type="MenuItem" Text="關於" FunctionAttribute="FormAbout" /> </Menu> </MenuStrip>
設置裝備擺設規矩:
①.一切的內容都在標簽<MenuStrip>中
②.每個<Menu>標簽代表了一個橫向的菜單項,個中Text屬性是顯示的文字,ShortKey屬性是選擇用的快捷鍵
③.每個<Item>標簽都在<Menu>或<Item>標簽下,表現一個菜單項,菜單項外面可以嵌套其他的Item菜單項。
④.<Item>標簽的Type屬性代表了此像的類型,分為Seperator和MenuItem兩類,前者對應ToolStripItem類,後者對應ToolStripSeparator類
⑤.<Item>標簽中,Type屬性為MenuItem時,還可以設置Text屬性(顯示文字)、ShortKey屬性(快捷鍵)、FunctionAttribute屬性(生成實例後,單擊此菜單項須要挪用的函數綁定特征值)
另:在本法式中單擊生成後的菜單挪用的是一個函數,該函數上方有一個RemarkAttribute特征,FunctionAttribute屬性的值假如與之分歧則挪用該函數。這個特征的設計其實不是逝世的,在本法式中是函數,經由修正後,也能夠對應自界說控件、類等。
3.關於法式窗體
法式窗體沒有設置其他的屬性,只要一個空的MenuStrip,名為mnsMainMenu。窗體的稱號為FormMain。
4.法式代碼
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; using System.Reflection; namespace Program22 { public partial class FormMain : Form { public FormMain() { InitializeComponent(); } //1.把MenuConfig.xml中的信息反應到菜單mnsMainMenu上 //2.顯示文本、快捷鍵等信息直接添加,FunctionName屬性添加到Tag中 //3.經由過程反射找到對應函數,完成操作 private void Form1_Load(object sender, EventArgs e) { SetMainMenu("MenuConfig.xml"); } /// <summary> /// 設置MenuStrip控件 /// </summary> /// <param name="address"></param> private void SetMainMenu(string address) { //設置前清空MenuStrip中一切項 mnsMainMenu.Items.Clear(); //讀取XML文件 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(address); XmlNode root = xmlDoc.SelectSingleNode("MenuStrip"); SetMenu(mnsMainMenu, root); } /// <summary> /// 設置MenuStrip控件第一層菜單及其子菜單 /// </summary> /// <param name="ms">MenuStrip控件</param> /// <param name="xnode">XML結點</param> private void SetMenu(MenuStrip ms, XmlNode xnode) { foreach (XmlNode xn in xnode.ChildNodes) { XmlElement xe = (XmlElement)xn; ToolStripMenuItem newtsmi = new ToolStripMenuItem(xe.GetAttribute("Text")); //設置快捷鍵 if (string.IsNullOrWhiteSpace(xe.GetAttribute("ShortKey"))) { SetShortKey(newtsmi, xe.GetAttribute("ShortKey")); } //設置挪用函數 if (!string.IsNullOrWhiteSpace(xe.GetAttribute("FunctionAttribute"))) { newtsmi.Tag = xe.GetAttribute("FunctionAttribute"); newtsmi.Click += ToolStripMenuItem_Click; } //設置子菜單 if (xn.ChildNodes.Count != 0) { SetItem(newtsmi, xn); } ms.Items.Add(newtsmi); } } /// <summary> /// 設置 ToolStripMenuItem 控件及其子菜單(遞歸設置) /// </summary> /// <param name="tsmi">ToolStripMenuItem控件</param> /// <param name="xnode">XML結點</param> private void SetItem(ToolStripMenuItem tsmi, XmlNode xnode) { foreach (XmlNode xn in xnode.ChildNodes) { XmlElement xe = (XmlElement)xn; ToolStripMenuItem newtsmi = new ToolStripMenuItem(xe.GetAttribute("Text")); switch (xe.GetAttribute("Type")) { case "MenuItem": { //設置快捷鍵 if (!string.IsNullOrWhiteSpace(xe.GetAttribute("ShortKey"))) { SetShortKey(newtsmi, xe.GetAttribute("ShortKey")); } //設置挪用函數 if (!string.IsNullOrWhiteSpace( xe.GetAttribute("FunctionAttribute"))) { newtsmi.Tag = xe.GetAttribute("FunctionAttribute"); newtsmi.Click += ToolStripMenuItem_Click; } //設置子菜單 if (xn.ChildNodes.Count != 0) { SetItem(newtsmi, xn); } tsmi.DropDownItems.Add(newtsmi as ToolStripItem); } break; case "Seperator": { tsmi.DropDownItems.Add(new ToolStripSeparator()); } break; default: break; } } } /// <summary> /// 經由過程字符串(如"Ctrl+Alt+A")斷定按鍵信息 /// </summary> /// <param name="key"></param> /// <returns></returns> private void SetShortKey(ToolStripMenuItem tsmi, string key) { System.Windows.Forms.Keys result = default(System.Windows.Forms.Keys); string[] keys = key.Split('+'); if (keys.Contains("Ctrl")) { result |= Keys.Control; } if (keys.Contains("Shift")) { result |= Keys.Shift; } if (keys.Contains("Alt")) { result |= Keys.Alt; } if (keys.Contains("A")) { result |= Keys.A; } if (keys.Contains("B")) { result |= Keys.B; } if (keys.Contains("C")) { result |= Keys.C; } if (keys.Contains("D")) { result |= Keys.D; } if (keys.Contains("E")) { result |= Keys.E; } if (keys.Contains("F")) { result |= Keys.F; } if (keys.Contains("G")) { result |= Keys.G; } if (keys.Contains("H")) { result |= Keys.H; } if (keys.Contains("I")) { result |= Keys.I; } if (keys.Contains("J")) { result |= Keys.J; } if (keys.Contains("K")) { result |= Keys.K; } if (keys.Contains("L")) { result |= Keys.L; } if (keys.Contains("M")) { result |= Keys.M; } if (keys.Contains("N")) { result |= Keys.N; } if (keys.Contains("O")) { result |= Keys.O; } if (keys.Contains("P")) { result |= Keys.P; } if (keys.Contains("Q")) { result |= Keys.Q; } if (keys.Contains("R")) { result |= Keys.R; } if (keys.Contains("S")) { result |= Keys.S; } if (keys.Contains("T")) { result |= Keys.T; } if (keys.Contains("U")) { result |= Keys.U; } if (keys.Contains("V")) { result |= Keys.V; } if (keys.Contains("W")) { result |= Keys.W; } if (keys.Contains("X")) { result |= Keys.X; } if (keys.Contains("Y")) { result |= Keys.Y; } if (keys.Contains("Z")) { result |= Keys.Z; } if (keys.Contains("0")) { result |= Keys.D0; } if (keys.Contains("1")) { result |= Keys.D1; } if (keys.Contains("2")) { result |= Keys.D2; } if (keys.Contains("3")) { result |= Keys.D3; } if (keys.Contains("4")) { result |= Keys.D4; } if (keys.Contains("5")) { result |= Keys.D5; } if (keys.Contains("6")) { result |= Keys.D6; } if (keys.Contains("7")) { result |= Keys.D7; } if (keys.Contains("8")) { result |= Keys.D8; } if (keys.Contains("9")) { result |= Keys.D9; } if (keys.Contains("F1")) { result |= Keys.F1; } if (keys.Contains("F2")) { result |= Keys.F2; } if (keys.Contains("F3")) { result |= Keys.F3; } if (keys.Contains("F4")) { result |= Keys.F4; } if (keys.Contains("F5")) { result |= Keys.F5; } if (keys.Contains("F6")) { result |= Keys.F6; } if (keys.Contains("F7")) { result |= Keys.F7; } if (keys.Contains("F8")) { result |= Keys.F8; } if (keys.Contains("F9")) { result |= Keys.F9; } if (keys.Contains("F10")) { result |= Keys.F10; } if (keys.Contains("F11")) { result |= Keys.F11; } if (keys.Contains("F12")) { result |= Keys.F12; } tsmi.ShortcutKeys = result; } /// <summary> /// 特征 RemarkAttribute,用在函數上,其 Remark 屬性決議了 /// 積分函數 Integration 應當選擇法式中的哪一個函數停止盤算 /// </summary> [AttributeUsage(AttributeTargets.Method)] public class RemarkAttribute : Attribute { string remark; public string Remark { get { return remark; } } //結構函數 public RemarkAttribute(string comment) { remark = comment; } } /// <summary> /// 單擊一個ToolStripMenuItem後觸發的事宜 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ToolStripMenuItem_Click(object sender, EventArgs e) { //假如Tag為空,則不履行任何事宜 if (string.IsNullOrWhiteSpace((string)((ToolStripMenuItem)sender).Tag)) { return; } //假如Tag存在,則尋覓具有和Tag內容分歧特征的函數並挪用 FunctionInvoke((string)((ToolStripMenuItem)sender).Tag); } /// <summary> /// 挪用具有指定特征名的函數 /// </summary> /// <param name="funcattr">函數的RemarkAttribute特征值</param> private void FunctionInvoke(string funcattr) { //須要 using System.Reflection; MethodInfo[] mi = typeof(FormMain).GetMethods( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); //找到具有指定特征的函數,停止挪用 foreach (MethodInfo m in mi) { Type t2 = typeof(RemarkAttribute); RemarkAttribute ra = (RemarkAttribute)Attribute.GetCustomAttribute(m, t2); if (ra != null && ra.Remark == funcattr) { m.Invoke(this, new object[] { }); break; } } } //須要挪用的函數 [RemarkAttribute("ColorToRed")] //窗口底色:紅 private void ChangeColorToRed() { this.BackColor = Color.Red; } [RemarkAttribute("ColorToLawnGreen")] //窗口底色:綠 private void ChangeColorToLawnGreen() { this.BackColor = Color.LawnGreen; } [RemarkAttribute("FormNormal")] //窗口年夜小:通俗 private void ChangeSizeToNrm() { this.WindowState = FormWindowState.Normal; } [RemarkAttribute("FormMinimize")] //窗口年夜小:最小化 private void ChangeSizeToMin() { this.WindowState = FormWindowState.Minimized; } [RemarkAttribute("FormMaximize")] //窗口年夜小:最年夜化 private void ChangeSizeToMax() { this.WindowState = FormWindowState.Maximized; } [RemarkAttribute("FormClose")] //加入法式 private void CloseForm() { Application.Exit(); } [RemarkAttribute("FormAbout")] //關於法式 private void AboutForm() { MessageBox.Show("Write By Tsybius 2014/5/15"); } } }
願望本文所述對年夜家的C#法式設計有所贊助。