winform- 实现画圆角矩形
使用winform实现画圆角矩形
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Text; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { private int intStartX = 0; private int intStartY = 0; private bool isMouseDraw = false; private Rectangle rectangle; public Form1() { InitializeComponent(); } private int[] colorR = { 255, 250, 245, 240, 235, 230, 225, 220, 215, 210, 206, 201, 196, 192, 187, 183, 179, 174, 170, 166, 162, 157, 153, 149, 145, 141, 137, 134, 130, 126, 122, 119, 115, 112, 108, 105, 101, 98, 95, 92, 88, 85, 82, 79, 76, 73, 71, 68, 65, 62, 60, 57, 54, 52, 49, 47, 45, 42, 40, 38, 36, 34, 34, 34, 33, 33, 33, 33, 33, 33, 33, 33, 32, 32, 32, 32, 32, 32, 32, 32, 31, 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 30, 30, 29, 29, 29, 29, 29, 29, 29, 28, 28, 20, 20, 21, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 32, 35, 38, 41, 44, 48, 51, 54, 57, 60, 64, 67, 70, 74, 77, 81, 84, 88, 91, 95, 98, 102, 105, 109, 113, 116, 120, 124, 128, 131, 135, 139, 143, 147, 150, 154, 158, 162, 166, 170, 174, 178, 182, 186, 190, 194, 198, 203, 207, 211, 215, 219, 223, 228, 230, 231, 232, 232, 232, 231, 231, 230, 230, 229, 229, 228, 228, 227, 227, 227, 226, 226, 225, 225, 224, 224, 223, 223, 222, 222, 221, 221, 221, 220, 220, 219, 219, 218, 218, 217, 217, 216, 216, 216, 215, 215, 214, 214, 213, 213, 212, 212, 211, 211, 210, 210, 210, 209, 209, 211, 213, 214, 216, 218, 220, 221, 223, 225, 226, 228, 230, 232, 233, 235, 237, 239, 240, 242, 244, 246, 247, 249, 0 }; private int[] colorG = { 255, 250, 245, 240, 235, 230, 226, 221, 216, 212, 208, 203, 199, 195, 191, 187, 183, 179, 175, 172, 168, 165, 161, 158, 155, 151, 148, 145, 142, 139, 136, 133, 130, 128, 125, 122, 120, 117, 115, 112, 110, 107, 105, 103, 101, 99, 97, 95, 93, 91, 89, 87, 85, 83, 81, 80, 78, 76, 75, 73, 72, 70, 73, 76, 79, 82, 85, 87, 90, 93, 96, 100, 103, 106, 109, 112, 115, 119, 122, 125, 128, 132, 133, 133, 133, 134, 134, 134, 135, 135, 136, 136, 136, 137, 137, 137, 138, 138, 138, 139, 139, 140, 140, 140, 141, 143, 144, 145, 146, 147, 149, 150, 151, 152, 154, 155, 156, 157, 158, 160, 161, 162, 163, 164, 166, 167, 168, 169, 170, 172, 173, 174, 175, 176, 178, 179, 180, 181, 183, 184, 185, 186, 187, 189, 190, 191, 192, 193, 195, 196, 197, 198, 199, 201, 202, 203, 204, 205, 207, 208, 209, 210, 212, 213, 214, 215, 216, 218, 219, 220, 221, 222, 224, 225, 226, 227, 228, 227, 226, 224, 224, 220, 217, 213, 210, 206, 203, 199, 196, 192, 189, 185, 182, 178, 175, 171, 168, 164, 161, 157, 154, 151, 147, 144, 140, 137, 133, 130, 127, 123, 120, 116, 113, 110, 106, 103, 99, 96, 93, 89, 86, 83, 79, 76, 73, 69, 66, 63, 59, 56, 53, 32, 31, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 10, 9, 0 }; private int[] colorB = { 255, 253, 251, 249, 246, 244, 242, 240, 238, 236, 234, 232, 229, 227, 225, 223, 221, 219, 217, 214, 212, 210, 208, 206, 204, 202, 200, 197, 195, 193, 191, 189, 187, 185, 183, 180, 178, 176, 174, 172, 170, 168, 165, 163, 161, 159, 157, 155, 153, 151, 148, 146, 144, 142, 140, 138, 136, 133, 131, 129, 127, 125, 125, 126, 126, 126, 127, 127, 128, 128, 128, 129, 129, 129, 130, 130, 130, 131, 131, 132, 132, 132, 130, 127, 125, 122, 119, 116, 114, 111, 108, 105, 102, 99, 96, 93, 90, 87, 84, 80, 77, 74, 71, 62, 60, 59, 57, 56, 54, 52, 51, 49, 47, 46, 44, 42, 40, 38, 37, 35, 33, 31, 29, 30, 30, 31, 31, 32, 32, 33, 33, 34, 35, 35, 36, 36, 37, 38, 38, 39, 39, 40, 41, 41, 42, 43, 43, 44, 45, 45, 46, 47, 47, 48, 49, 49, 50, 51, 51, 52, 53, 54, 54, 55, 56, 56, 57, 58, 59, 59, 60, 61, 62, 63, 63, 64, 65, 66, 66, 67, 67, 67, 67, 66, 66, 65, 65, 65, 64, 64, 64, 63, 63, 63, 62, 62, 62, 61, 61, 61, 60, 60, 59, 59, 59, 58, 58, 58, 57, 57, 57, 56, 56, 56, 55, 55, 55, 54, 54, 54, 53, 53, 53, 52, 52, 52, 51, 51, 51, 51, 50, 29, 28, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 13, 12, 11, 10, 9, 8, 6, 5, 0 }; private void button1_Click(object sender, EventArgs e) { Bitmap bitmap = new Bitmap(100, 256); Graphics g = Graphics.FromImage(bitmap); for (int i = 0; i < 256; i++) { Pen pen = new Pen(Color.FromArgb(colorR[i], colorG[i], colorB[i])); Point p1 = new Point(0, i); Point p2 = new Point(100, i); g.DrawLine(pen, p1, p2); } pictureBox1.BackColor = Color.Black; pictureBox1.BackgroundImageLayout = ImageLayout.Stretch; pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox1.Image = bitmap; g.Dispose(); } private void button2_Click(object sender, EventArgs e) { Random rd = new Random(); UInt16[] wave = new UInt16[512]; for (int i = 0; i < wave.Length; i++) { wave[i] = (UInt16)rd.Next(1, 400); } string str = ""; foreach (var item in wave) { str = str + item.ToString() + ","; } Console.WriteLine(str); } private void button3_Click(object sender, EventArgs e) { //Bitmap bitmap = new Bitmap(300, 300); //Graphics g = Graphics.FromImage(bitmap); //g.SmoothingMode = SmoothingMode.HighQuality;//去掉锯齿 //g.CompositingQuality = CompositingQuality.HighQuality;//合成图像的质量 //g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;//去掉文字的锯齿 //System.Drawing.Pen p = new System.Drawing.Pen(Color.FromArgb(255, 146, 126, 102), 2); //drawRoundedRect(g, p, 1, 1, 100, 100, 20); //pictureBox1.Image = bitmap; Bitmap bitmap = new Bitmap(100, 100); Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = SmoothingMode.HighQuality; Rectangle rectangle = new Rectangle(0, 0, 100, 100); FillRoundRectangle(g, rectangle, Color.Black, 20); pictureBox1.Image = bitmap; } /// <summary> /// C# GDI+ 绘制圆角实心矩形 /// </summary> /// <param name="g">Graphics 对象</param> /// <param name="rectangle">要填充的矩形</param> /// <param name="backColor">填充背景色</param> /// <param name="r">圆角半径</param> public static void FillRoundRectangle(Graphics g, Rectangle rectangle, Color backColor, int r) { rectangle = new Rectangle(rectangle.X, rectangle.Y, rectangle.Width - 1, rectangle.Height - 1); Brush b = new SolidBrush(backColor); g.FillPath(b, GetRoundRectangle(rectangle, r)); } /// <summary> /// 根据普通矩形得到圆角矩形的路径 /// </summary> /// <param name="rectangle">原始矩形</param> /// <param name="r">半径</param> /// <returns>图形路径</returns> private static GraphicsPath GetRoundRectangle(Rectangle rectangle, int r) { int l = 2 * r; // 把圆角矩形分成八段直线、弧的组合,依次加到路径中 GraphicsPath gp = new GraphicsPath(); gp.AddLine(new Point(rectangle.X + r, rectangle.Y), new Point(rectangle.Right - r, rectangle.Y)); gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Y, l, l), 270F, 90F); gp.AddLine(new Point(rectangle.Right, rectangle.Y + r), new Point(rectangle.Right, rectangle.Bottom - r)); gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Bottom - l, l, l), 0F, 90F); gp.AddLine(new Point(rectangle.Right - r, rectangle.Bottom), new Point(rectangle.X + r, rectangle.Bottom)); gp.AddArc(new Rectangle(rectangle.X, rectangle.Bottom - l, l, l), 90F, 90F); gp.AddLine(new Point(rectangle.X, rectangle.Bottom - r), new Point(rectangle.X, rectangle.Y + r)); gp.AddArc(new Rectangle(rectangle.X, rectangle.Y, l, l), 180F, 90F); return gp; } /// <summary> /// 带圆角矩形 /// </summary> /// <param name="radius">圆角半径</param> private void drawRoundedRect(Graphics g, System.Drawing.Pen pen, int x, int y, int width, int height, int radius) { //去掉圆角的锯齿 g.SmoothingMode = SmoothingMode.HighQuality; //上 g.DrawLine(pen, new PointF(x + radius, y), new PointF(x + width - radius, y)); //下 g.DrawLine(pen, new PointF(x + radius, y + height), new PointF(x + width - radius, y + height)); //左 g.DrawLine(pen, new PointF(x, y + radius), new PointF(x, y + height - radius)); //右 g.DrawLine(pen, new PointF(x + width, y + radius), new PointF(x + width, y + height - radius)); //左上角 g.DrawArc(pen, new Rectangle(x, y, radius * 2, radius * 2), 180, 90); //右上角 g.DrawArc(pen, new Rectangle(x + width - radius * 2, y, radius * 2, radius * 2), 270, 90); //左下角 g.DrawArc(pen, new Rectangle(x, y + height - radius * 2, radius * 2, radius * 2), 90, 90); //右下角 g.DrawArc(pen, new Rectangle(x + width - radius * 2, y + height - radius * 2, radius * 2, radius * 2), 0, 90); } } }