简体   繁体   English

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

[英]how to make a textbox with rounded corner in c#?

I was wondering how to make a class for textboxes with rounded corners in c#(visual studio).我想知道如何在 c#(visual studio) 中为带有圆角的文本框制作 class。 Could anyone please help me.谁能帮帮我。 I found a code online to create it but not able to enlarge(stretch) it我在网上找到了一个代码来创建它,但无法放大(拉伸)它

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
    }
}

I found a code online to create it but not able to enlarge(stretch) it.我在网上找到了一个代码来创建它,但无法放大(拉伸)它。

With this code, the control will be resized (stretched) when you rebuild the project.使用此代码,当您重建项目时,控件将调整大小(拉伸)。

To apply that in the designer without rebuilding the project, override the OnResize event instead of the OnCreateControl event.要在设计器中应用它而不重建项目,请覆盖OnResize事件而不是OnCreateControl事件。

Replace this:替换这个:

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
}

with this:有了这个:

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
}

Good luck.祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM