C#實現窗體圓角的一種方法
阿新 • • 發佈:2019-02-03
實現窗體圓角的方法,我用過的有以下兩種:一是最簡單的使用圓角圖片,加到窗體的四個角的位置;二是使用Rectangle繪製。
下面是第二種方式。
程式碼:
下面是第二種方式。
程式碼:
public void SetWindowRegion() { GraphicsPath FormPath; FormPath = new System.Drawing.Drawing2D.GraphicsPath(); Rectangle rect = new Rectangle(-1, -1, this.Width + 1, this.Height); FormPath = GetRoundedRectPath(rect, 24); 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, 185, 90); // 右上角 arcRect.X = rect.Right - diameter; path.AddArc(arcRect, 275, 90); // 右下角 arcRect.Y = rect.Bottom - diameter; path.AddArc(arcRect, 356, 90); // 左下角 arcRect.X = rect.Left; arcRect.Width += 2; arcRect.Height += 2; path.AddArc(arcRect, 90, 90); path.CloseFigure(); return path; }
重寫的OnPaint方法裡呼叫SetWindowRegion。