如何在c#中制作圆角文本框?

Reg*_*ath 4 c# winforms

我想知道如何在 c#(Visual Studio) 中为带有圆角的文本框创建一个类。有人可以帮助我吗?我在网上找到了一个代码来创建它,但无法放大(拉伸)它

using System.Windows.Forms;
using System.Drawing;
using System;

class round : TextBox
{
    [System.Runtime.InteropServices.DllImport("gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
    private static extern IntPtr CreateRoundRectRgn
    (
        int nLeftRect, // X-coordinate of upper-left corner or padding at start
        int nTopRect,// Y-coordinate of upper-left corner or padding at the top of the textbox
        int nRightRect, // X-coordinate of lower-right corner or Width of the object
        int nBottomRect,// Y-coordinate of lower-right corner or Height of the object
                        //RADIUS, how round do you want it to be?
        int nheightRect, //height of ellipse 
        int nweightRect //width of ellipse
    );
    protected override void OnCreateControl()
    {
        base.OnCreateControl();
        this.Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(2, 3, this.Width, this.Height, 15, 15)); //play with these values till you are happy
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

我在网上找到了一个代码来创建它,但无法放大(拉伸)它。

使用此代码,当您重建项目时,控件将被调整大小(拉伸)。

要在设计器中应用该功能而不重建项目,请重写OnResize事件而不是OnCreateControl事件。

替换这个:

protected override void OnCreateControl()
{
    base.OnCreateControl();
    this.Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(2, 3, this.Width, this.Height, 15, 15)); //play with these values till you are happy
}
Run Code Online (Sandbox Code Playgroud)

有了这个:

protected override void OnResize(EventArgs e)
{
    base.OnResize(e);
    this.Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(2, 3, this.Width, this.Height, 15, 15)); //play with these values till you are happy
}
Run Code Online (Sandbox Code Playgroud)

祝你好运。