【问题标题】:how to make a textbox with rounded corner in c#?如何在c#中制作带圆角的文本框?
【发布时间】:2021-05-03 19:43:56
【问题描述】:

我想知道如何在 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
    }
}

【问题讨论】:

  • 圆角是圆角。您应该尝试将在按钮中找到的技术应用于文本框。因为真的没有什么可以帮助 - 你没有展示你的尝试并描述一个问题
  • 更简单地使用无边框文本框并从容器的 Paint 事件中绘制圆度。
  • 不值得麻烦。

标签: c# winforms


【解决方案1】:

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

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

要在设计器中应用它而不重建项目,请覆盖 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
}

用这个:

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
}

祝你好运。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 2013-04-16
    相关资源
    最近更新 更多