場景 Winforn中實現ZedGraph自定義添加右鍵菜單項(附源碼下載): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100140716 在上面博客實現的自定義右鍵以及漢化右鍵的基礎上,怎樣實現將自帶的右鍵功能比如列印和頁 ...
場景
Winforn中實現ZedGraph自定義添加右鍵菜單項(附源碼下載):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100140716
在上面博客實現的自定義右鍵以及漢化右鍵的基礎上,怎樣實現將自帶的右鍵功能比如列印和頁面設置合併為一個選項列印,並將上兩個合併為其二級子菜單。
最終實現效果如下
註:
博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關註公眾號
霸道的程式猿
獲取編程相關電子書、教程推送與免費下載。
實現
首先在迴圈所有的menuStrip.Items時候,通過item.Visible = false;將原來的列印與列印頁面設置的item禁用。
foreach (ToolStripMenuItem item in menuStrip.Items) { switch (item.Name) { case "copied_to_clip": item.Text = @"複製到剪貼板"; break; case "copy": item.Text = @"複製"; item.Click += item_Click; break; case "page_setup": item.Text = @"列印頁面設置"; item.Visible = false; break; case "print": item.Text = @"列印"; item.Visible = false; break; case "save_as": item.Text = @"另存圖表..."; break; case "set_default": item.Text = @"恢復預設大小"; break; case "show_val": item.Text = @"顯示節點數值"; item.Visible = false; break; case "title_def": item.Text = @"標題"; break; case "undo_all": item.Text = @"還原所有縮放"; item.Visible = false; break; case "unpan": item.Text = @"還原上一步縮放"; item.Visible = false; break; case "unzoom": item.Text = @"還原縮放"; item.Visible = false; break; case "x_title_def": item.Text = @"X 軸"; break; case "y_title_def": item.Text = @"Y 軸"; break; } }
然後在擴展右鍵菜單時自定義二級子菜單效果,併在子菜單中執行ZedControl的自帶的右鍵事件。
ToolStripMenuItem mnuDirectPrint = new ToolStripMenuItem("直接列印"); mnuDirectPrint.Name = "directPrint"; mnuDirectPrint.Text = "直接列印"; mnuDirectPrint.Click += delegate(object sender, EventArgs e) { control.DoPrint(); }; ToolStripMenuItem mnuPrintPageSet = new ToolStripMenuItem("列印頁面設置"); mnuPrintPageSet.Name = "printPageSet"; mnuPrintPageSet.Text = "列印頁面設置"; mnuPrintPageSet.Click += delegate(object sender, EventArgs e) { control.DoPageSetup(); }; ToolStripMenuItem mnuPrint = new ToolStripMenuItem(); mnuPrint.Name = "mnuPrint"; mnuPrint.Text = "列印"; mnuPrint.DropDownItems.Add(mnuDirectPrint); mnuPrint.DropDownItems.Add(mnuPrintPageSet); menuStrip.Items.Add(mnuPrint);
完整示例代碼
首先右鍵菜單事件重新訂閱
zgc.ContextMenuBuilder -= zgc_ContextMenuBuilder; //上下文菜單生成事件訂閱 zgc.ContextMenuBuilder += zgc_ContextMenuBuilder; //上下文菜單生成事件訂閱
然後在實現事件中
private static void zgc_ContextMenuBuilder(ZedGraphControl control, ContextMenuStrip menuStrip, Point mousePt, ZedGraphControl.ContextMenuObjectState objState) { #region 擴展上下文菜單 #region 列印 ToolStripMenuItem mnuDirectPrint = new ToolStripMenuItem("直接列印"); mnuDirectPrint.Name = "directPrint"; mnuDirectPrint.Text = "直接列印"; mnuDirectPrint.Click += delegate(object sender, EventArgs e) { control.DoPrint(); }; ToolStripMenuItem mnuPrintPageSet = new ToolStripMenuItem("列印頁面設置"); mnuPrintPageSet.Name = "printPageSet"; mnuPrintPageSet.Text = "列印頁面設置"; mnuPrintPageSet.Click += delegate(object sender, EventArgs e) { control.DoPageSetup(); }; ToolStripMenuItem mnuPrint = new ToolStripMenuItem(); mnuPrint.Name = "mnuPrint"; mnuPrint.Text = "列印"; mnuPrint.DropDownItems.Add(mnuDirectPrint); mnuPrint.DropDownItems.Add(mnuPrintPageSet); menuStrip.Items.Add(mnuPrint); #endregion #region 縮放 ToolStripMenuItem mnuReductLast = new ToolStripMenuItem("還原上一步縮放"); mnuReductLast.Name = "mnuReductLast"; mnuReductLast.Text = "還原上一步縮放"; mnuReductLast.Click += delegate(object sender, EventArgs e) { control.ZoomOut(control.GraphPane); }; ToolStripMenuItem mnuReductAll = new ToolStripMenuItem("還原所有縮放"); mnuReductAll.Name = "mnuReductAll"; mnuReductAll.Text = "還原所有縮放"; mnuReductAll.Click += delegate(object sender, EventArgs e) { control.ZoomOutAll(control.GraphPane); }; ToolStripMenuItem mnuReduct = new ToolStripMenuItem(); mnuReduct.Name = "mnuReduct"; mnuReduct.Text = "縮放"; mnuReduct.DropDownItems.Add(mnuReductLast); mnuReduct.DropDownItems.Add(mnuReductAll); menuStrip.Items.Add(mnuReduct); #endregion #endregion #region 漢化上下文菜單中的菜單項 foreach (ToolStripMenuItem item in menuStrip.Items) { switch (item.Name) { case "copied_to_clip": item.Text = @"複製到剪貼板"; break; case "copy": item.Text = @"複製"; item.Click += item_Click; break; case "page_setup": item.Text = @"列印頁面設置"; item.Visible = false; break; case "print": item.Text = @"列印"; item.Visible = false; break; case "save_as": item.Text = @"另存圖表..."; break; case "set_default": item.Text = @"恢復預設大小"; break; case "show_val": item.Text = @"顯示節點數值"; item.Visible = false; break; case "title_def": item.Text = @"標題"; break; case "undo_all": item.Text = @"還原所有縮放"; item.Visible = false; break; case "unpan": item.Text = @"還原上一步縮放"; item.Visible = false; break; case "unzoom": item.Text = @"還原縮放"; item.Visible = false; break; case "x_title_def": item.Text = @"X 軸"; break; case "y_title_def": item.Text = @"Y 軸"; break; } } #endregion }