C#绘制三角形并填充,使用winform实现qq聊天气泡
首先是需求,需要制作一个聊天气泡, 但是winform中有没有类似Android的.9图,只有自己设计图形拼接气泡。
第一种是绘制空心三角形,第二种是绘制三角形区域,可以指定RGB颜色。
private void Form1_Paint(object sender, PaintEventArgs e) { Pen pen = new Pen(Color.Red, 1); e.Graphics.DrawLine(pen, 10, 10, 50, 10); e.Graphics.DrawLine(pen, 10, 10, 50, 50); e.Graphics.DrawLine(pen, 50, 10, 50, 50); Color color = System.Drawing.Color.FromArgb(((int)(((byte)(34)))), ((int)(((byte)(54)))), ((int)(((byte)(82))))); Brush brushes = new SolidBrush(color); Point[] point new Point[3]; point[0] = new Point(50,100); point[1] = new Point(100,50); point[2] = new Point(100,100); e.Graphics.FillPolygon(brushes, point); }
效果:
可以作为气泡中的箭头,另外四个角用椭圆:
绘制圆角矩形的代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//窗口圆角 private void Main_Paint(object sender, PaintEventArgs e) { List<Point> list = new List<Point>(); int width = this.Width; int height = this.Height; #region 四个圆角 //左上 list.Add(new Point(0, 47)); list.Add(new Point(1, 42)); list.Add(new Point(2, 38)); list.Add(new Point(3, 36)); list.Add(new Point(4, 33)); list.Add(new Point(5, 32)); list.Add(new Point(6, 29)); list.Add(new Point(7, 27)); list.Add(new Point(8, 26)); list.Add(new Point(9, 24)); list.Add(new Point(10, 22)); list.Add(new Point(11, 21)); list.Add(new Point(12, 20)); list.Add(new Point(13, 19)); list.Add(new Point(14, 17)); list.Add(new Point(15, 16)); list.Add(new Point(16, 15)); list.Add(new Point(17, 14)); list.Add(new Point(19, 13)); list.Add(new Point(20, 12)); list.Add(new Point(21, 11)); list.Add(new Point(22, 10)); list.Add(new Point(24, 9)); list.Add(new Point(26, 8)); list.Add(new Point(27, 7)); list.Add(new Point(29, 6)); list.Add(new Point(32, 5)); list.Add(new Point(33, 4)); list.Add(new Point(36, 3)); list.Add(new Point(38, 2)); list.Add(new Point(42, 1)); list.Add(new Point(47, 0)); //右上 list.Add(new Point(width - 47, 0)); list.Add(new Point(width - 42, 1)); list.Add(new Point(width - 38, 2)); list.Add(new Point(width - 36, 3)); list.Add(new Point(width - 33, 4)); list.Add(new Point(width - 32, 5)); list.Add(new Point(width - 29, 6)); list.Add(new Point(width - 27, 7)); list.Add(new Point(width - 26, 8)); list.Add(new Point(width - 24, 9)); list.Add(new Point(width - 22, 10)); list.Add(new Point(width - 21, 11)); list.Add(new Point(width - 20, 12)); list.Add(new Point(width - 19, 13)); list.Add(new Point(width - 17, 14)); list.Add(new Point(width - 16, 15)); list.Add(new Point(width - 15, 16)); list.Add(new Point(width - 14, 17)); list.Add(new Point(width - 13, 19)); list.Add(new Point(width - 12, 20)); list.Add(new Point(width - 11, 21)); list.Add(new Point(width - 10, 22)); list.Add(new Point(width - 9, 24)); list.Add(new Point(width - 8, 26)); list.Add(new Point(width - 7, 27)); list.Add(new Point(width - 6, 29)); list.Add(new Point(width - 5, 32)); list.Add(new Point(width - 4, 33)); list.Add(new Point(width - 3, 36)); list.Add(new Point(width - 2, 38)); list.Add(new Point(width - 1, 42)); list.Add(new Point(width - 0, 47)); //右下 list.Add(new Point(width - 0, height - 47)); list.Add(new Point(width - 1, height - 42)); list.Add(new Point(width - 2, height - 38)); list.Add(new Point(width - 3, height - 36)); list.Add(new Point(width - 4, height - 33)); list.Add(new Point(width - 5, height - 32)); list.Add(new Point(width - 6, height - 29)); list.Add(new Point(width - 7, height - 27)); list.Add(new Point(width - 8, height - 26)); list.Add(new Point(width - 9, height - 24)); list.Add(new Point(width - 10, height - 22)); list.Add(new Point(width - 11, height - 21)); list.Add(new Point(width - 12, height - 20)); list.Add(new Point(width - 13, height - 19)); list.Add(new Point(width - 14, height - 17)); list.Add(new Point(width - 15, height - 16)); list.Add(new Point(width - 16, height - 15)); list.Add(new Point(width - 17, height - 14)); list.Add(new Point(width - 19, height - 13)); list.Add(new Point(width - 20, height - 12)); list.Add(new Point(width - 21, height - 11)); list.Add(new Point(width - 22, height - 10)); list.Add(new Point(width - 24, height - 9)); list.Add(new Point(width - 26, height - 8)); list.Add(new Point(width - 27, height - 7)); list.Add(new Point(width - 29, height - 6)); list.Add(new Point(width - 32, height - 5)); list.Add(new Point(width - 33, height - 4)); list.Add(new Point(width - 36, height - 3)); list.Add(new Point(width - 38, height - 2)); list.Add(new Point(width - 42, height - 1)); list.Add(new Point(width - 47, height - 0)); //左下 list.Add(new Point(47, height - 0)); list.Add(new Point(42, height - 1)); list.Add(new Point(38, height - 2)); list.Add(new Point(36, height - 3)); list.Add(new Point(33, height - 4)); list.Add(new Point(32, height - 5)); list.Add(new Point(29, height - 6)); list.Add(new Point(27, height - 7)); list.Add(new Point(26, height - 8)); list.Add(new Point(24, height - 9)); list.Add(new Point(22, height - 10)); list.Add(new Point(21, height - 11)); list.Add(new Point(20, height - 12)); list.Add(new Point(19, height - 13)); list.Add(new Point(17, height - 14)); list.Add(new Point(16, height - 15)); list.Add(new Point(15, height - 16)); list.Add(new Point(14, height - 17)); list.Add(new Point(13, height - 19)); list.Add(new Point(12, height - 20)); list.Add(new Point(11, height - 21)); list.Add(new Point(10, height - 22)); list.Add(new Point(9, height - 24)); list.Add(new Point(8, height - 26)); list.Add(new Point(7, height - 27)); list.Add(new Point(6, height - 29)); list.Add(new Point(5, height - 32)); list.Add(new Point(4, height - 33)); list.Add(new Point(3, height - 36)); list.Add(new Point(2, height - 38)); list.Add(new Point(1, height - 42)); list.Add(new Point(0, height - 47)); #endregion Point[] points = list.ToArray(); GraphicsPath shape = new GraphicsPath(); shape.AddPolygon(points); this.Region = new System.Drawing.Region(shape); }
时间会记录下一切。