欢迎光临
我们一直在努力

C#画直线实现实例解析

C#画直线的操作有的时候我们会在实际开发遇到这样的需求,那么C#画直线是如何实现的呢?这里我们来看看具体的实现代码,通过代码的介绍希望对你的开发有所帮助。

C#画直线实现实例:


 
  1. //以下是完整代码,可以直接编译运行   
  2. //-----C#画直线---------   
  3. using System;   
  4. using System.Collections.Generic;   
  5. using System.Windows.Forms;   
  6. using System.Drawing;   
  7.  
  8. namespace q2   
  9. {   
  10. static class Program   
  11. {   
  12. /// ﹤summary﹥   
  13. /// 应用程序的主入口点。   
  14. /// ﹤/summary﹥   
  15. [STAThread]   
  16. static void Main()   
  17. {   
  18. Application.EnableVisualStyles();   
  19. Application.SetCompatibleTextRenderingDefault(false);   
  20. Application.Run(new Form1());   
  21. }   
  22. }   
  23.  
  24. /// ﹤summary﹥   
  25. /// 线条对象   
  26. /// ﹤/summary﹥   
  27. class Line   
  28. {   
  29. /// ﹤summary﹥   
  30. /// 建立线条对象,并设置起点   
  31. /// ﹤/summary﹥   
  32. /// ﹤param name="startPoint"﹥此线条的起点﹤/param﹥   
  33. public Line(Point startPoint)   
  34. {   
  35. StartPoint = startPoint;   
  36. EndPoint = startPoint;   
  37. }   
  38. public Point StartPoint = Point.Empty;   
  39. public Point EndPoint = Point.Empty;   
  40. }   
  41.  
  42. public class DrawPanel : Control   
  43. {   
  44. public DrawPanel()   
  45. {   
  46. this.DoubleBuffered = true;   
  47. this.SetStyle(  
  48. ControlStyles.OptimizedDoubleBuffer |   
  49. ControlStyles.ResizeRedraw, true);   
  50. }   
  51. }   
  52.  
  53. /// ﹤summary﹥   
  54. /// C#画直线之窗口定义   
  55. /// ﹤/summary﹥   
  56. public class Form1 : Form   
  57. {   
  58. public Form1()   
  59. {   
  60. drawPanel.BackColor = Color.White;   
  61. drawPanel.Cursor = Cursors.Cross;   
  62. drawPanel.Dock = DockStyle.Fill;   
  63. drawPanel.MouseDown +=   
  64. new MouseEventHandler(drawPanel_MouseDown);   
  65. drawPanel.MouseUp +=   
  66. new MouseEventHandler(drawPanel_MouseUp);   
  67. drawPanel.MouseMove +=   
  68. new MouseEventHandler(drawPanel_MouseMove);   
  69. drawPanel.Paint +=   
  70. new PaintEventHandler(drawPanel_Paint);   
  71. Controls.Add(drawPanel);   
  72. }   
  73.  
  74. /// ﹤summary﹥   
  75. /// C#画直线之用于保存绘出线条的集合   
  76. /// ﹤/summary﹥   
  77. private List﹤Line﹥ lines = new List﹤Line﹥();   
  78.  
  79. /// ﹤summary﹥   
  80. /// 用于保存当前正在绘制的线条   
  81. /// ﹤/summary﹥   
  82. private Line drawingLine = null;   
  83.  
  84. /// ﹤summary﹥   
  85. /// 用于显示绘图的面板组件   
  86. /// ﹤/summary﹥   
  87. private DrawPanel drawPanel = new DrawPanel();   
  88.  
  89. /// ﹤summary﹥   
  90. /// 在绘图区释放鼠标,结束当前线条绘制   
  91. /// ﹤/summary﹥   
  92. /// ﹤param name="sender"﹥﹤/param﹥   
  93. /// ﹤param name="e"﹥﹤/param﹥   
  94. void drawPanel_MouseUp(object sender, MouseEventArgs e)   
  95. {   
  96. if (drawingLine == nullreturn;   
  97. drawingLine.EndPoint = e.Location;   
  98. drawingLine = null;   
  99. }   
  100.  
  101. /// ﹤summary﹥   
  102. /// 在绘图区按下鼠标,开始绘制新线条   
  103. /// ﹤/summary﹥   
  104. /// ﹤param name="sender"﹥﹤/param﹥   
  105. /// ﹤param name="e"﹥﹤/param﹥   
  106. void drawPanel_MouseDown(object sender, MouseEventArgs e)   
  107. {   
  108. drawingLine = new Line(e.Location);   
  109. lines.Add(drawingLine);   
  110. }   
  111. ///C#画直线  
  112. /// ﹤summary﹥   
  113. /// 在绘图区移动鼠标时,如果正在绘制新线条,就更新绘制面板   
  114. /// ﹤/summary﹥   
  115. /// ﹤param name="sender"﹥﹤/param﹥   
  116. /// ﹤param name="e"﹥﹤/param﹥   
  117. void drawPanel_MouseMove(object sender, MouseEventArgs e)   
  118. {   
  119. if(drawingLine != null)   
  120. {   
  121. drawingLine.EndPoint = e.Location;   
  122. drawPanel.Invalidate();   
  123. }   
  124. }   
  125.  
  126. /// ﹤summary﹥   
  127. /// 绘制效果到面板   
  128. /// ﹤/summary﹥   
  129. /// ﹤param name="sender"﹥﹤/param﹥   
  130. /// ﹤param name="e"﹥﹤/param﹥   
  131. void drawPanel_Paint(object sender, PaintEventArgs e)   
  132. {   
  133. Bitmap bp = new Bitmap(  
  134. drawPanel.Width, drawPanel.Height); // 用于缓冲输出的位图对象   
  135. Graphics g = Graphics.FromImage(bp);   
  136.  
  137. g.SmoothingMode =   
  138. System.Drawing.Drawing2D.  
  139. SmoothingMode.AntiAlias; // 消锯齿(可选项)   
  140.  
  141. Pen p = new Pen(Color.Black);   
  142. foreach (Line line in lines)   
  143. {   
  144. if (line == drawingLine)   
  145. {   
  146. // 当前绘制的线条是正在鼠标定位的线条   
  147. p.Color = Color.Blue;   
  148. }   
  149. else   
  150. {   
  151. p.Color = Color.Black;   
  152. }   
  153. g.DrawLine(p, line.StartPoint, line.EndPoint);   
  154. }   
  155. // 将缓冲位图绘制到输出   
  156. e.Graphics.DrawImage(bp, Point.Empty);   
  157. }   
  158. }   
  159. }  
  160. //C#画直线  

C#画直线的相关实例就向你演示到这里,希望对了解和学习C#画直线的实现有所帮助。

【编辑推荐】

  1. C#单元测试的一个小故事
  2. C#单元测试概念及作用的浅析
  3. C#单元测试使用的必要性的浅析
  4. C#单元测试的运行浅析
  5. 常见的C#单元测试工具介绍

赞(0) 打赏
未经允许不得转载:九八云安全 » C#画直线实现实例解析

评论 抢沙发