您好,登錄后才能下訂單哦!
本篇內容主要講解“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#畫直線的實現方法”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。