具有自定义边框和圆角边缘的 C# 表单
我使用此代码使我的表单 (FormBorderStyle=none) 具有圆角边缘:
[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 Form1()
{
InitializeComponent();
Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
}
这用于在 Paint 事件上设置自定义边框:
ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.Black, 5, ButtonBorderStyle.Solid, Color.Black, 5, ButtonBorderStyle.Solid, Color.Black, 5, ButtonBorderStyle.Solid, Color.Black, 5, ButtonBorderStyle.Solid);
但请参阅此 .
内部形状矩形没有圆角边缘。
如何使蓝色内部表单矩形也具有圆角边缘,这样它看起来就不会像屏幕截图一样?
I am using this code to make my form (FormBorderStyle=none) with rounded edges:
[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 Form1()
{
InitializeComponent();
Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
}
And this to set a custom border on the Paint event:
ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.Black, 5, ButtonBorderStyle.Solid, Color.Black, 5, ButtonBorderStyle.Solid, Color.Black, 5, ButtonBorderStyle.Solid, Color.Black, 5, ButtonBorderStyle.Solid);
But see this .
The inside form rectangle doesn't have rounded edges.
How can I make the blue inside form rectangle to have rounded edge too so it wont look like the screenshot?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该地区财产只是切断了角落。要获得真正的圆角,您必须绘制圆角矩形。
绘制圆角矩形
绘制圆角矩形可能会更容易您想要的形状的图像并将其放在透明表格上。更容易绘制,但无法调整大小。
The Region propery simply cuts off the corners. To have a true rounded corner you will have to draw the rounded rectangles.
Drawing rounded rectangles
It might be easier to draw an image of the shape you want and put that on the transparent form. Easier to draw but cannot be resized.
请注意,您泄漏了 CreateRoundRectRgn(),您应该使用 DeleteObject() 使用后。
Region.FromHrgn() 复制定义,因此它不会释放句柄。
(将添加为评论,但声誉已被删除)
Note you're leaking the handle returned by CreateRoundRectRgn(), you should free it with DeleteObject() after it is used.
The Region.FromHrgn() copies the definition, so it won't free the handle.
(would add as comment but reputation is ded)