具有自定义边框和圆边的 C# 窗体

标签 c# winforms rounded-corners formborderstyle

<分区>

我正在使用此代码制作我的表单 (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);

但是看到这个screenshot .

内部矩形没有圆边。

我怎样才能让蓝色的内部矩形也有圆边,这样它看起来就不像屏幕截图了?

最佳答案

Region 属性简单地切掉了边角。要获得真正的圆角,您必须绘制圆角矩形。

Drawing rounded rectangles

绘制所需形状的图像并将其放在透明表单上可能会更容易。更容易绘制,但无法调整大小。

关于具有自定义边框和圆边的 C# 窗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5092216/

相关文章:

c# - 使用 C# 连接到 MySql

c# - 我是否使用 GMFBridge.DLL 来正确预览/捕获流?

.net - TabPages 中的工具条经常从 Windows 窗体设计器中消失

.net - 带有图像和文本的 WinForms 按钮

c# - 如何从一个方法返回多个变量?

c# - 为上传的图像创建缩略图

Android:列表项隐藏的ListView圆角

html - 我的圆 Angular 旁边的 CSS 像素太多

css - 你能在不使用图像的情况下在 IE 中实现 CSS 圆 Angular 吗?

c# - 如何在 C# 中使用 lambda 表达式从列表中获取某些属性