c# 通过鼠标点击绘制多边形
生活随笔
收集整理的這篇文章主要介紹了
c# 通过鼠标点击绘制多边形
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、問題描述
c#中,希望用鼠標點擊事件,實現多邊形的繪制;
2、解決思路
a、創建一個鏈表,將鼠標左鍵選中的點存儲到鏈表中;
b、根據鏈表中的值,利用c#的DrawPolygon()函數繪制;
3、核心代碼
List<Point> polyPoints = null;bool cliceMenu = true;private void pictureBox1_MouseDown(object sender, MouseEventArgs e){if (cliceMenu){if (e.Button == MouseButtons.Left){if (polyPoints == null) polyPoints = new List<Point>();polyPoints.Add(e.Location);}else if (e.Button == MouseButtons.Right&&polyPoints!=null){//Pen pen = new Pen(Color.Red, 3);//這個是在picturebox控件中創建的圖形using (var g = pictureBox1.CreateGraphics()){g.SmoothingMode = SmoothingMode.AntiAlias;g.Clear(pictureBox1.BackColor);//填充多邊形//using (SolidBrush br = new SolidBrush(Color.FromArgb(100, Color.Yellow)))//{// g.FillPolygon(br, polyPoints.ToArray());//}if (polyPoints.ToArray().Length >= 3){//繪制輪廓g.DrawPolygon(Pens.Red, polyPoints.ToArray());//繪制頂點foreach (Point p in polyPoints){g.FillEllipse(Brushes.Red, new Rectangle(p.X - 2, p.Y - 2, 4, 4));}} polyPoints = null;}}}}參考:
1、C#如何利用鼠標繪制多邊形
?
總結
以上是生活随笔為你收集整理的c# 通过鼠标点击绘制多边形的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: opencvsharp打开相机并视频显示
- 下一篇: c#中用鼠标点击事件实现抠图