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

场景

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
        }

 

你可能感兴趣的:(ZedGraph怎样实现将图形右键菜单的打印和页面设置合并为打印的二级子菜单)