如何创建带有圆角的用户控件? [英] How to create a User Control with rounded corners?

查看:83
本文介绍了如何创建带有圆角的用户控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用一个带有圆角的用户控件。它没有固定的大小,但通常宽度不超过120像素。



我需要用户控件及其内容(标签和表格)

我使用了这段代码。

  [DllImport( Gdi32.dll,EntryPoint = CreateRoundRectRgn)] 
私有静态外部IntPtr CreateRoundRectRgn

int nLeftRect,//上坐标的x坐标左角
int nTopRect,//左上角的y坐标
int nRightRect,//右下角的x坐标
int nBottomRect,//下部的y坐标-right corner
int nWidthEllipse,//椭圆的高度
int nHeightEllipse //椭圆的宽度
);

公共静态System.Drawing.Region GetRoundedRegion(int controlWidth,int controlHeight)
{
返回System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0,0,controlWidth-5 ,controlHeight-5,20,20));
}

这为控件提供了圆角,但是在运行了几次之后,我已将我的用户控件的倍数添加到表单中,这将导致泄漏,并且我将在用户控件上获得带有红叉的白框。



有没有更好的方法

解决方案

如果您想要真正的圆角而不只是透明的技巧,则可以使用以下示例:

  private int radius = 20; 
[DefaultValue(20)]
public int半径
{
get {返回半径; }
set
{
radius = value;
this.RecreateRegion();
}
}
private GraphicsPath GetRoundRectagle(矩形边界,整数半径)
{
GraphicsPath path = new GraphicsPath();
path.AddArc(bounds.X,bounds.Y,radius,radius,180,90);
path.AddArc(bounds.X + bounds.Width-半径,bounds.Y,半径,半径,270,90);
path.AddArc(bounds.X + bounds.Width-半径,bounds.Y + bounds.Height-半径,
radius,radius,0,90);
path.AddArc(bounds.X,bounds.Y + bounds.Height-半径,半径,半径90、90);
path.CloseAllFigures();
返回路径;
}
private void RecreateRegion()
{
var bounds = ClientRectangle;
bounds.Width--; bounds.Height--;
using(var path = GetRoundRectagle(bounds,this.Radius))
this.Region = new Region(path);
this.Invalidate();
}
受保护的覆盖无效OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
this.RecreateRegion();
}

屏幕截图为:





此方法与透明设置之间的区别:




  • 设置圆形区域,该控件确实具有圆角,尽管透明,您仍可以看到圆形部分后面的内容,您将看到表格的背景。

  • 设置圆形区域,当您单击移除的圆形部分时,单击通过该区域并到达后面,但是如果您使用透明技巧,则单击透明区域将由控件处理。 li>


您可以使用这两个选项中的任何一个。



下载



您可以根据需要设置透明性或设置区域。在此处下载代码或克隆存储库:




I am trying to have a User Control that has rounded corners. It doesnt have a fixed size but it usually doesnt have a width much more than 120 pixels.

I need the User Control and its contents(a label and a table) to have rounded edges and look like a round box.

I have used this code.

[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));
    } 

This gives the control rounded corners but after it has been running afew times and i have added multiples of my User Control to the form it will cause a leak and i will get the whitebox with a red cross on my user controls.

Is there a better way of doing this?

解决方案

If you want really round corner and not only transparent trick you can use this example:

private int radius=20;
[DefaultValue(20)]
public int Radius
{
    get { return radius; }
    set
    {
        radius = value;
        this.RecreateRegion();
    }
}
private GraphicsPath GetRoundRectagle(Rectangle bounds, int radius)
{
    GraphicsPath path = new GraphicsPath();
    path.AddArc(bounds.X, bounds.Y, radius, radius, 180, 90);
    path.AddArc(bounds.X + bounds.Width - radius, bounds.Y, radius, radius, 270, 90);
    path.AddArc(bounds.X + bounds.Width - radius, bounds.Y + bounds.Height - radius, 
                radius, radius, 0, 90);
    path.AddArc(bounds.X, bounds.Y + bounds.Height - radius, radius, radius, 90, 90);
    path.CloseAllFigures();
    return path;
}
private void RecreateRegion()
{
    var bounds = ClientRectangle;
    bounds.Width--; bounds.Height--;
    using (var path = GetRoundRectagle(bounds, this.Radius))
        this.Region = new Region(path);
    this.Invalidate();
}
protected override void OnSizeChanged(EventArgs e)
{
    base.OnSizeChanged(e);
    this.RecreateRegion();
}

And screenshot will be:

The difference between this approach and making transparent:

  • Setting round region, the control has really round corners and you can see what is behind the round part despite when it is transparent, you will see background of form.
  • Setting round region, when you click on removed rounded part, click pass through the region and reaches behind, but if you use transparency trick click on transparent region will handle by the control.

You can use any of these 2 options. Making transparent or setting region based on your requirement.

Download

You can download the code or clone the repository here:

这篇关于如何创建带有圆角的用户控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆