Winform圆角窗体绘制
将Winform窗体改为圆角的:
方法一:
private void FormLogin_Paint(object sender, PaintEventArgs e) { //圆角窗体 orm form = ((Form)sender); List<Point> list = new List<Point>(); int width = form.Width; int height = form.Height; //左上 list.Add(new Point(0, 5)); list.Add(new Point(1, 5)); list.Add(new Point(1, 3)); list.Add(new Point(2, 3)); list.Add(new Point(2, 2)); list.Add(new Point(3, 2)); list.Add(new Point(3, 1)); list.Add(new Point(5, 1)); list.Add(new Point(5, 0)); //右上 list.Add(new Point(width - 5, 0)); list.Add(new Point(width - 5, 1)); list.Add(new Point(width - 3, 1)); list.Add(new Point(width - 3, 2)); list.Add(new Point(width - 2, 2)); list.Add(new Point(width - 2, 3)); list.Add(new Point(width - 1, 3)); list.Add(new Point(width - 1, 5)); list.Add(new Point(width - 0, 5)); //右下 list.Add(new Point(width - 0, height - 5)); list.Add(new Point(width - 1, height - 5)); list.Add(new Point(width - 1, height - 3)); list.Add(new Point(width - 2, height - 3)); list.Add(new Point(width - 2, height - 2)); list.Add(new Point(width - 3, height - 2)); list.Add(new Point(width - 3, height - 1)); list.Add(new Point(width - 5, height - 1)); list.Add(new Point(width - 5, height - 0)); //左下 list.Add(new Point(5, height - 0)); list.Add(new Point(5, height - 1)); list.Add(new Point(3, height - 1)); list.Add(new Point(3, height - 2)); list.Add(new Point(2, height - 2)); list.Add(new Point(2, height - 3)); list.Add(new Point(1, height - 3)); list.Add(new Point(1, height - 5)); list.Add(new Point(0, height - 5)); Point[] points = list.ToArray(); GraphicsPath shape = new GraphicsPath(); shape.AddPolygon(points); //将窗体的显示区域设为GraphicsPath的实例 form.Region = new System.Drawing.Region(shape); }
需要将formborderstyle设置为None;之前试过了次方法,但是界面会一直不停的闪烁,检查了下也不知道是什么原因造成的(但是有时候是可以的)。所以就使用了下面的第二种方法。此方法是完全没有问题的。
方法二:
public void SetWindowRegion() { System.Drawing.Drawing2D.GraphicsPath FormPath; FormPath = new System.Drawing.Drawing2D.GraphicsPath(); Rectangle rect = new Rectangle(0, 22, this.Width, this.Height - 22);//this.Left-10,this.Top-10,this.Width-10,this.Height-10); FormPath = GetRoundedRectPath(rect, 30); this.Region = new Region(FormPath); } private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius) { int diameter = radius; Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter)); GraphicsPath path = new GraphicsPath(); // 左上角 path.AddArc(arcRect, 180, 90); // 右上角 arcRect.X = rect.Right - diameter; path.AddArc(arcRect, 270, 90); // 右下角 arcRect.Y = rect.Bottom - diameter; path.AddArc(arcRect, 0, 90); // 左下角 arcRect.X = rect.Left; path.AddArc(arcRect, 90, 90); path.CloseFigure(); return path; } protected override void OnResize(System.EventArgs e) { this.Region = null; SetWindowRegion(); }