C#画直线的操作有的时候我们会在实际开发遇到这样的需求,那么C#画直线是如何实现的呢?这里我们来看看具体的实现代码,通过代码的介绍希望对你的开发有所帮助。
临江网站建设公司成都创新互联公司,临江网站设计制作,有大型网站制作公司丰富经验。已为临江近千家提供企业网站建设服务。企业网站搭建\外贸网站建设要多少钱,请找那个售后服务好的临江做网站的公司定做!
C#画直线实现实例:
- //以下是完整代码,可以直接编译运行
- //-----C#画直线---------
- using System;
- using System.Collections.Generic;
- using System.Windows.Forms;
- using System.Drawing;
- namespace q2
- {
- static class Program
- {
- /// ﹤summary﹥
- /// 应用程序的主入口点。
- /// ﹤/summary﹥
- [STAThread]
- static void Main()
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new Form1());
- }
- }
- /// ﹤summary﹥
- /// 线条对象
- /// ﹤/summary﹥
- class Line
- {
- /// ﹤summary﹥
- /// 建立线条对象,并设置起点
- /// ﹤/summary﹥
- /// ﹤param name="startPoint"﹥此线条的起点﹤/param﹥
- public Line(Point startPoint)
- {
- StartPoint = startPoint;
- EndPoint = startPoint;
- }
- public Point StartPoint = Point.Empty;
- public Point EndPoint = Point.Empty;
- }
- public class DrawPanel : Control
- {
- public DrawPanel()
- {
- this.DoubleBuffered = true;
- this.SetStyle(
- ControlStyles.OptimizedDoubleBuffer |
- ControlStyles.ResizeRedraw, true);
- }
- }
- /// ﹤summary﹥
- /// C#画直线之窗口定义
- /// ﹤/summary﹥
- public class Form1 : Form
- {
- public Form1()
- {
- drawPanel.BackColor = Color.White;
- drawPanel.Cursor = Cursors.Cross;
- drawPanel.Dock = DockStyle.Fill;
- drawPanel.MouseDown +=
- new MouseEventHandler(drawPanel_MouseDown);
- drawPanel.MouseUp +=
- new MouseEventHandler(drawPanel_MouseUp);
- drawPanel.MouseMove +=
- new MouseEventHandler(drawPanel_MouseMove);
- drawPanel.Paint +=
- new PaintEventHandler(drawPanel_Paint);
- Controls.Add(drawPanel);
- }
- /// ﹤summary﹥
- /// C#画直线之用于保存绘出线条的集合
- /// ﹤/summary﹥
- private List﹤Line﹥ lines = new List﹤Line﹥();
- /// ﹤summary﹥
- /// 用于保存当前正在绘制的线条
- /// ﹤/summary﹥
- private Line drawingLine = null;
- /// ﹤summary﹥
- /// 用于显示绘图的面板组件
- /// ﹤/summary﹥
- private DrawPanel drawPanel = new DrawPanel();
- /// ﹤summary﹥
- /// 在绘图区释放鼠标,结束当前线条绘制
- /// ﹤/summary﹥
- /// ﹤param name="sender"﹥﹤/param﹥
- /// ﹤param name="e"﹥﹤/param﹥
- void drawPanel_MouseUp(object sender, MouseEventArgs e)
- {
- if (drawingLine == null) return;
- drawingLine.EndPoint = e.Location;
- drawingLine = null;
- }
- /// ﹤summary﹥
- /// 在绘图区按下鼠标,开始绘制新线条
- /// ﹤/summary﹥
- /// ﹤param name="sender"﹥﹤/param﹥
- /// ﹤param name="e"﹥﹤/param﹥
- void drawPanel_MouseDown(object sender, MouseEventArgs e)
- {
- drawingLine = new Line(e.Location);
- lines.Add(drawingLine);
- }
- ///C#画直线
- /// ﹤summary﹥
- /// 在绘图区移动鼠标时,如果正在绘制新线条,就更新绘制面板
- /// ﹤/summary﹥
- /// ﹤param name="sender"﹥﹤/param﹥
- /// ﹤param name="e"﹥﹤/param﹥
- void drawPanel_MouseMove(object sender, MouseEventArgs e)
- {
- if(drawingLine != null)
- {
- drawingLine.EndPoint = e.Location;
- drawPanel.Invalidate();
- }
- }
- /// ﹤summary﹥
- /// 绘制效果到面板
- /// ﹤/summary﹥
- /// ﹤param name="sender"﹥﹤/param﹥
- /// ﹤param name="e"﹥﹤/param﹥
- void drawPanel_Paint(object sender, PaintEventArgs e)
- {
- Bitmap bp = new Bitmap(
- drawPanel.Width, drawPanel.Height); // 用于缓冲输出的位图对象
- Graphics g = Graphics.FromImage(bp);
- g.SmoothingMode =
- System.Drawing.Drawing2D.
- SmoothingMode.AntiAlias; // 消锯齿(可选项)
- Pen p = new Pen(Color.Black);
- foreach (Line line in lines)
- {
- if (line == drawingLine)
- {
- // 当前绘制的线条是正在鼠标定位的线条
- p.Color = Color.Blue;
- }
- else
- {
- p.Color = Color.Black;
- }
- g.DrawLine(p, line.StartPoint, line.EndPoint);
- }
- // 将缓冲位图绘制到输出
- e.Graphics.DrawImage(bp, Point.Empty);
- }
- }
- }
- //C#画直线
C#画直线的相关实例就向你演示到这里,希望对了解和学习C#画直线的实现有所帮助。
分享标题:C#画直线实现实例解析
转载源于:http://www.csdahua.cn/qtweb/news39/497839.html
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网