【发布时间】:2019-05-15 17:47:38
【问题描述】:
我有一个代码可以帮助我制作一个无边框的圆角WinForm。它工作正常,但问题是它没有边框,所以我想给它添加圆角边框。另外,我只希望 TopLeft 和 BottomRight 圆角。
这是我当前的代码:
public partial class mainForm : Form
{
[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();
this.FormBorderStyle = FormBorderStyle.None;
Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
}
在WPF 中很容易实现,但我如何在WinForms 中获得它?
我该怎么办?
【问题讨论】:
-
代码好像和this question一样。您可以使用
ControlPaint手动绘制边框。还是你想成为able to resize无边框形式? -
是的,是一样的。另外,我现在还没有考虑调整大小,但是是的,边框是我唯一想要的。除此之外,我希望能够只有两个圆角。
-
CreateRoundRectRgn() 不会只给你两个圆角。请改用 .NET Region(GraphicsPath) 构造函数。而且您不能真正忽略窗口自行调整大小的可能性,因此这至少需要在 Load 事件中完成,可能在 Resize 事件中完成。