马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

ZedGraph怎样实现将图形右键菜单的打印和页面设置合并为打印的二级子菜单

[复制链接]
查看965 | 回复0 | 2019-12-26 09:07:58 | 显示全部楼层 |阅读模式

场景

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禁用。

  1. foreach (ToolStripMenuItem item in menuStrip.Items)
  2. {
  3. switch (item.Name)
  4. {
  5. case "copied_to_clip":
  6. item.Text = @"复制到剪贴板";
  7. break;
  8. case "copy":
  9. item.Text = @"复制";
  10. item.Click += item_Click;
  11. break;
  12. case "page_setup":
  13. item.Text = @"打印页面设置";
  14. item.Visible = false;
  15. break;
  16. case "print":
  17. item.Text = @"打印";
  18. item.Visible = false;
  19. break;
  20. case "save_as":
  21. item.Text = @"另存图表...";
  22. break;
  23. case "set_default":
  24. item.Text = @"规复默认巨细";
  25. break;
  26. case "show_val":
  27. item.Text = @"体现节点数值";
  28. item.Visible = false;
  29. break;
  30. case "title_def":
  31. item.Text = @"标题";
  32. break;
  33. case "undo_all":
  34. item.Text = @"还原全部缩放";
  35. item.Visible = false;
  36. break;
  37. case "unpan":
  38. item.Text = @"还原上一步缩放";
  39. item.Visible = false;
  40. break;
  41. case "unzoom":
  42. item.Text = @"还原缩放";
  43. item.Visible = false;
  44. break;
  45. case "x_title_def":
  46. item.Text = @"X 轴";
  47. break;
  48. case "y_title_def":
  49. item.Text = @"Y 轴";
  50. break;
  51. }
  52. }
复制代码

然后在扩展右键菜单时自界说二级子菜单结果,并在子菜单中实验ZedControl的自带的右键变乱。

  1. ToolStripMenuItem mnuDirectPrint = new ToolStripMenuItem("直接打印");
  2. mnuDirectPrint.Name = "directPrint";
  3. mnuDirectPrint.Text = "直接打印";
  4. mnuDirectPrint.Click += delegate(object sender, EventArgs e)
  5. {
  6. control.DoPrint();
  7. };
  8. ToolStripMenuItem mnuPrintPageSet = new ToolStripMenuItem("打印页面设置");
  9. mnuPrintPageSet.Name = "printPageSet";
  10. mnuPrintPageSet.Text = "打印页面设置";
  11. mnuPrintPageSet.Click += delegate(object sender, EventArgs e)
  12. {
  13. control.DoPageSetup();
  14. };
  15. ToolStripMenuItem mnuPrint = new ToolStripMenuItem();
  16. mnuPrint.Name = "mnuPrint";
  17. mnuPrint.Text = "打印";
  18. mnuPrint.DropDownItems.Add(mnuDirectPrint);
  19. mnuPrint.DropDownItems.Add(mnuPrintPageSet);
  20. menuStrip.Items.Add(mnuPrint);
复制代码

完整示例代码

起首右键菜单变乱重新订阅

  1. zgc.ContextMenuBuilder -= zgc_ContextMenuBuilder; //上下文菜单天生变乱订阅
  2. zgc.ContextMenuBuilder += zgc_ContextMenuBuilder; //上下文菜单天生变乱订阅
复制代码

然后在实现变乱中

  1. private static void zgc_ContextMenuBuilder(ZedGraphControl control, ContextMenuStrip menuStrip, Point mousePt, ZedGraphControl.ContextMenuObjectState objState)
  2. {
  3. #region 扩展上下文菜单
  4. #region 打印
  5. ToolStripMenuItem mnuDirectPrint = new ToolStripMenuItem("直接打印");
  6. mnuDirectPrint.Name = "directPrint";
  7. mnuDirectPrint.Text = "直接打印";
  8. mnuDirectPrint.Click += delegate(object sender, EventArgs e)
  9. {
  10. control.DoPrint();
  11. };
  12. ToolStripMenuItem mnuPrintPageSet = new ToolStripMenuItem("打印页面设置");
  13. mnuPrintPageSet.Name = "printPageSet";
  14. mnuPrintPageSet.Text = "打印页面设置";
  15. mnuPrintPageSet.Click += delegate(object sender, EventArgs e)
  16. {
  17. control.DoPageSetup();
  18. };
  19. ToolStripMenuItem mnuPrint = new ToolStripMenuItem();
  20. mnuPrint.Name = "mnuPrint";
  21. mnuPrint.Text = "打印";
  22. mnuPrint.DropDownItems.Add(mnuDirectPrint);
  23. mnuPrint.DropDownItems.Add(mnuPrintPageSet);
  24. menuStrip.Items.Add(mnuPrint);
  25. #endregion
  26. #region 缩放
  27. ToolStripMenuItem mnuReductLast = new ToolStripMenuItem("还原上一步缩放");
  28. mnuReductLast.Name = "mnuReductLast";
  29. mnuReductLast.Text = "还原上一步缩放";
  30. mnuReductLast.Click += delegate(object sender, EventArgs e)
  31. {
  32. control.ZoomOut(control.GraphPane);
  33. };
  34. ToolStripMenuItem mnuReductAll = new ToolStripMenuItem("还原全部缩放");
  35. mnuReductAll.Name = "mnuReductAll";
  36. mnuReductAll.Text = "还原全部缩放";
  37. mnuReductAll.Click += delegate(object sender, EventArgs e)
  38. {
  39. control.ZoomOutAll(control.GraphPane);
  40. };
  41. ToolStripMenuItem mnuReduct = new ToolStripMenuItem();
  42. mnuReduct.Name = "mnuReduct";
  43. mnuReduct.Text = "缩放";
  44. mnuReduct.DropDownItems.Add(mnuReductLast);
  45. mnuReduct.DropDownItems.Add(mnuReductAll);
  46. menuStrip.Items.Add(mnuReduct);
  47. #endregion
  48. #endregion
  49. #region 汉化上下文菜单中的菜单项
  50. foreach (ToolStripMenuItem item in menuStrip.Items)
  51. {
  52. switch (item.Name)
  53. {
  54. case "copied_to_clip":
  55. item.Text = @"复制到剪贴板";
  56. break;
  57. case "copy":
  58. item.Text = @"复制";
  59. item.Click += item_Click;
  60. break;
  61. case "page_setup":
  62. item.Text = @"打印页面设置";
  63. item.Visible = false;
  64. break;
  65. case "print":
  66. item.Text = @"打印";
  67. item.Visible = false;
  68. break;
  69. case "save_as":
  70. item.Text = @"另存图表...";
  71. break;
  72. case "set_default":
  73. item.Text = @"规复默认巨细";
  74. break;
  75. case "show_val":
  76. item.Text = @"体现节点数值";
  77. item.Visible = false;
  78. break;
  79. case "title_def":
  80. item.Text = @"标题";
  81. break;
  82. case "undo_all":
  83. item.Text = @"还原全部缩放";
  84. item.Visible = false;
  85. break;
  86. case "unpan":
  87. item.Text = @"还原上一步缩放";
  88. item.Visible = false;
  89. break;
  90. case "unzoom":
  91. item.Text = @"还原缩放";
  92. item.Visible = false;
  93. break;
  94. case "x_title_def":
  95. item.Text = @"X 轴";
  96. break;
  97. case "y_title_def":
  98. item.Text = @"Y 轴";
  99. break;
  100. }
  101. }
  102. #endregion
  103. }
复制代码
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则