如何在C#中制作带有圆角的文本框? [英] how to make a textbox with rounded corner in c#?

查看:67
本文介绍了如何在C#中制作带有圆角的文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在c#(Visual Studio)中为带有圆角的文本框创建类.谁能帮我.我在网上找到了创建代码,但无法放大(拉伸)

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

推荐答案

我在网上找到了创建代码,但是无法放大(拉伸)它.

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.

要在设计器中应用它而不重建项目,请覆盖 OnResize 事件而不是 OnCreateControl 事件.

To apply that in the designer without rebuilding the project, override the OnResize event instead of the OnCreateControl event.

替换此:

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
}

与此:

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
}

祝你好运.

这篇关于如何在C#中制作带有圆角的文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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