场景

winforn中设置zedgraph曲线图的属性、坐标轴属性、刻度属性:

https://blog.csdn.net/badao_liumang_qizhi/article/details/100112573

在鼠标经过曲线附近时获取最近曲线以及曲线上点的坐标,并以此坐标表示一条十字线。

效果

 

 

注:

博客主页:

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

绑定鼠标移动事件

zgc.mousemove += zgc_mousemove;

 

其中zgc是 zedgraphcontrol zgc

 

private static void zgc_mousemove(object sender, mouseeventargs e)
        {
            using (graphics gc = global.zedgraphcontrol1.creategraphics())
            using (pen pen = new pen(color.green))
            {
                //设置画笔的宽度
                pen.width = 1;
                pen.dashstyle = system.drawing.drawing2d.dashstyle.dashdot;
                rectanglef rect = global.zedgraphcontrol1.graphpane.chart.rect;
                //确保在画图区域
                if (rect.contains(e.location))
                {
                    global.zedgraphcontrol1.refresh();
                    //画竖线
                    gc.drawline(pen, e.x, rect.top, e.x, rect.bottom);
                    //画横线
                    gc.drawline(pen, rect.left, e.y, rect.right, e.y);


                }
            }
        }

 

注:

global.zedgraphcontrol1是全局域的zedgraphcontrol对象。

这样后会随着鼠标的移动产生十字线,但是在离开pane时怎样将画好的十字线删除掉。

绑定其鼠标移除事件

zgc.mouseleave += zgc_mouseleave;

 

实现方法

 

private static void zgc_mouseleave(object sender, eventargs e)
        {
            using (graphics gc = global.zedgraphcontrol1.creategraphics())
            using (pen pen = new pen(color.green))
            {
                //设置画笔的宽度
                pen.width = 1;
                pen.dashstyle = system.drawing.drawing2d.dashstyle.dashdot;
                rectanglef rect = global.zedgraphcontrol1.graphpane.chart.rect;
                //确保在画图区域
                global.zedgraphcontrol1.refresh();
                //画竖线
                gc.drawline(pen, 0, 0, 0, 0);
                //画横线
                gc.drawline(pen, 0, 0, 0, 0);
            }
        }

 

这里采取的方法是采用将线画到原点的方式使其消失。