我正在尝试使用具有圆角的用户控件.它没有固定的大小,但通常宽度不超过120像素.
我需要用户控件及其内容(标签和表格)具有圆形边缘,看起来像一个圆形框.
我用过这段代码.
[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 static System.Drawing.Region GetRoundedRegion(int controlWidth, int controlHeight)
{
return System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, controlWidth - 5, controlHeight - 5, 20, 20));
}
Run Code Online (Sandbox Code Playgroud)
这给控件圆角但是在它运行了几次之后我已经将多个用户控件添加到表单中它将导致泄漏,我将在我的用户控件上获得带有红叉的白盒.
有没有更好的方法呢?
我在我的Text Class中有这个方法,我似乎无法翻转整个文本.
我正在使用Matrix来转换GraphicsPath用于绘制字符串的内容.
以下是我使用@ Jimi答案的代码:
public LayerClass DrawString(LayerClass.Type _text, string text, RectangleF rect, Font _fontStyle, Brush brush, float angle, PaintEventArgs e)
{
using (StringFormat string_format = new StringFormat())
{
SizeF stringSize = e.Graphics.MeasureString(text, _fontStyle);
rect.Location = new PointF(Shape.center.X - (rect.Width / 2), Shape.center.Y - (rect.Height / 2));
GraphicsContainer gc = e.Graphics.BeginContainer();
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
//e.Graphics.DrawRectangle(Pens.Red, Rectangle.Round(rect));
RectangleF r = new RectangleF(rect.Location, rect.Size);
GraphicsPath path = new GraphicsPath();
if (text == "" || text == " …Run Code Online (Sandbox Code Playgroud) 我正在创建一个具有圆形边框的表单(如本问题所示)。
由于这个人似乎也有问题,我似乎无法绘制圆形边框。
这是我用来设置实际边框形状的代码:
// ... within InitializeComponent ...
this.FormBorderStyle = FormBorderStyle.None;
IntPtr handle = CreateRoundRectRgn(0, 0, Width, Height, 20, 20);
Region = System.Drawing.Region.FromHrgn(handle);
DeleteObject(handle);
this.ResizeRedraw = true;
Run Code Online (Sandbox Code Playgroud)
这是覆盖OnPaint并绘制边框轮廓的代码。
protected override void OnPaint(PaintEventArgs e)
{
// I've tried modifying the parameters here.
GraphicsPath path = MyRoundedRectangle.Create(0, 0, Width, Height, 10, MyRoundedRectangle.RectangleCorners.All);
Pen p = new Pen(Brushes.Black, 3f);
e.Graphics.DrawPath(p, path);
}
Run Code Online (Sandbox Code Playgroud)
的内容与此问题MyRoundedRectangle中提供的代码相同,其中答案链接到此页面,其中包含 的代码。 MyRoundedRectangle
我想要一个完整的周围边框,但我得到的是:
我想创建一个按钮和一个带圆角的容器。我正在使用区域来绘制角落,代码附在下面。然而,角落似乎并不平滑,有什么方法可以解决这个问题,任何帮助将不胜感激。下面附上图片,因为我还不允许上传图片。
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn(
int nLeftRect,
int nTopRect,
int nRightRect,
int nBottomRect,
int nWidthEllipse,
int nHeightEllipse
);
Run Code Online (Sandbox Code Playgroud)
public Login()
{
InitializeComponent();
this.Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 30, 30));
this.logo.Image = Properties.Resources.logo;
this.btn_login.Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, this.btn_login.Width, this.btn_login.Height, 10, 10));
}
Run Code Online (Sandbox Code Playgroud)