c# - c# winforms表单边框中带边框颜色的圆角边框
问题描述
是否可以创建具有圆形边框并具有边框颜色的表单?我尝试了以下方法:
protected override void OnPaint(PaintEventArgs e) {
ControlPaint.DrawBorder(e.Graphics, ClientRectangle, Color.NavajoWhite,
ButtonBorderStyle.Solid);
}
和
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn
(
int nLeftRect, // x-coordinate of upper-left corner
int nTopRect, // y-coordinate of upper-left corner
int nRightRect, // x-coordinate of lower-right corner
int nBottomRect, // y-coordinate of lower-right corner
int nWidthEllipse, // height of ellipse
int nHeightEllipse // width of ellipse
);
public Main() {
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 18, 18));
}
这是结果:
我也试过:
protected override void OnPaint(PaintEventArgs e) {
ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.White, 1, ButtonBorderStyle.Solid, Color.White, 1, ButtonBorderStyle.Solid, Color.White, 2, ButtonBorderStyle.Solid, Color.White, 2, ButtonBorderStyle.Solid);
}
这是结果:
我离第二个很近,但是如何扩展圆边上的颜色边框?
解决方案
您需要手动为表单绘制边框。这是一个简单的演示,您可以参考。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
}
public void SetWindowRegion()
{
System.Drawing.Drawing2D.GraphicsPath FormPath;
FormPath = new System.Drawing.Drawing2D.GraphicsPath();
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
FormPath = GetRoundedRectPath(rect, 30);// 30 represents the size of the fillet angle
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);// top left
arcRect.X = rect.Right - diameter;//top right
path.AddArc(arcRect, 270, 90);
arcRect.Y = rect.Bottom - diameter;// buttom right
path.AddArc(arcRect, 0, 90);
arcRect.X = rect.Left;// button left
path.AddArc(arcRect, 90, 90);
path.CloseFigure();
return path;
}
private static GraphicsPath GetRoundRectangle(Rectangle rectangle, int r)
{
int l = 2 * r;
// Divide the rounded rectangle into a combination of straight lines and arcs, and add them to the path in turn
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;
}
public void FillRoundRectangle(Graphics g, Rectangle rectangle, Pen pen, int r)
{
rectangle = new Rectangle(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
g.DrawPath(pen, GetRoundRectangle(rectangle, r));
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen pen = new Pen(Color.Blue, 3);
pen.DashStyle = DashStyle.Solid;
Rectangle rectangle = new Rectangle(1, 1, this.Width - 2, this.Height - 2);
FillRoundRectangle(e.Graphics, rectangle, pen, 14);
}
private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Normal)
{
SetWindowRegion();
}
else
{
this.Region = null;
}
}
}
推荐阅读
- angular - 为某些调用禁用 Angular HttpInterceptor
- python-3.x - How does Selecor selects any element with xpath within object of requests module
- python - 如何在列表中查找重复项并对找到的每个重复项执行不同的计算?
- javascript - How can I define and ++ variable inside of function scope
- java - Java 8 Performance Issues In Windows Server 2019
- php - Prevent versioning for .woff (font) files
- postgresql - Trying to pg_restore from a pg_dump file, FOREIGN TABLEs are causing probems: ERROR: server "
" does not exist, - c# - 如何分配组合框中选定项目的值?
- facebook - Way to get image URL from the image hash ID of a Facebook Ad
- reactjs - API callback not working as soon as I insert ${city}, ${country} to query for weather on open weather API