C# GDI在控件上绘图的方法
程序员文章站
2023-12-16 22:51:16
本文以在chart控件上和窗体上画矩形为例子讲述了c# gdi在控件上绘图的方法。分享给大家供大家参考。具体方法如下:
具体的实现方法就不多解释了,备注很详细,代码也很简...
本文以在chart控件上和窗体上画矩形为例子讲述了c# gdi在控件上绘图的方法。分享给大家供大家参考。具体方法如下:
具体的实现方法就不多解释了,备注很详细,代码也很简单。
主要功能代码如下:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; using system.io; using system.configuration; namespace wfapp2 { public partial class data : form { public data() { initializecomponent(); //窗体 g = this.creategraphics(); //chart控件 g2 = this.chart1.creategraphics(); } public point firstpoint = new point(0, 0); //鼠标第一点 public point secondpoint = new point(0, 0); //鼠标第二点 public bool begin = false; //是否开始画矩形 /// <summary> /// 在from上画矩形 /// </summary> graphics g; /// <summary> /// 在chart1控件上画矩形 /// </summary> graphics g2; /// <summary> /// 在窗体上按下鼠标事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void data_mousedown(object sender, mouseeventargs e) { begin = true; firstpoint = new point(e.x, e.y); } /// <summary> /// 在窗体上鼠标移动开始绘图 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void data_mousemove(object sender, mouseeventargs e) { if (begin) { //清除窗体绘图面,相当于刷新了一次窗体界面然后重新绘制 g.clear(this.backcolor); //获取新的右下角坐标 secondpoint = new point(e.x, e.y); //获取两个数中的大者或小者 int minx = math.min(firstpoint.x, secondpoint.x); int miny = math.min(firstpoint.y, secondpoint.y); int maxx = math.max(firstpoint.x, secondpoint.x); int maxy = math.max(firstpoint.y, secondpoint.y); //画框 g.drawrectangle(new pen(color.red), minx, miny, maxx - minx, maxy - miny); //controlpaint.drawreversibleframe(new rectangle(minx, miny, maxx - minx, maxy - miny),this.backcolor,framestyle.dashed); } } /// <summary> /// 鼠标松开停止绘图 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void data_mouseup(object sender, mouseeventargs e) { begin = false; } /// <summary> /// 在chart控件上移动鼠标绘图 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void chart1_mousemove(object sender, mouseeventargs e) { if (begin) { //重新在chart上面绘图,此处不能用clear方法,clear会清除整个绘图界面chart控件会被清除 this.refresh(); //获取新的右下角坐标 secondpoint = new point(e.x, e.y); int minx = math.min(firstpoint.x, secondpoint.x); int miny = math.min(firstpoint.y, secondpoint.y); int maxx = math.max(firstpoint.x, secondpoint.x); int maxy = math.max(firstpoint.y, secondpoint.y); //画矩形 g2.drawrectangle(new pen(color.red), minx, miny, maxx - minx, maxy - miny); } } /// <summary> /// 鼠标松开停止绘图 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void chart1_mouseup(object sender, mouseeventargs e) { begin = false; } /// <summary> /// 在chart控件上按下鼠标 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void chart1_mousedown(object sender, mouseeventargs e) { begin = true; firstpoint = new point(e.x, e.y); } } }
希望本文所述对大家的c#程序设计有所帮助