C#畫直線實現實例解析
作者:webajax
C#畫直線的具體實現是什么呢?C#畫直線的操作使用的方法是什么呢?那么這里向你介紹了一個完整的C#畫直線的實現實例代碼,希望對你有所幫助。
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#畫直線的實現有所幫助。
【編輯推薦】
責任編輯:仲衡
來源:
CSDN博客