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

痞子三分冷 提交于 2021-02-16 05:28:32

问题


I was wondering how to make a class for textboxes with rounded corners in c#(visual studio). 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
    }
}

回答1:


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.

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.



来源:https://stackoverflow.com/questions/58613713/how-to-make-a-textbox-with-rounded-corner-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!