更改 Windows 窗体中的文本框边框样式 - C#

use*_*417 1 c# textbox border winforms

我有一个文本框,它是方形的,现在我想将该方形转换为椭圆形,我正在使用 win 表单应用程序

谁能告诉我对此的任何想法

ali*_*ari 5

您可以使用SetWindowRgnAPI 函数来更改窗口的形状。正如您在此处看到的,该函数获取三个参数:

  1. 窗口句柄:这可以是您的文本框句柄,您可以通过Handle属性获取它。
  2. 窗口 RGN :您可以通过调用CreateRoundRectRgn (或您可以在此处找到的其他 RGN 创建函数)来创建它
  3. 一个布尔值来确定 Redraw:最好是 true。

您可以TextBox使用OnHandleCreatedMethod 中的此函数从子类化并创建椭圆形 TextBox。该类可以是这样的:

class OvalTextBox : TextBox
{
    [DllImport("user32.dll")]
    static extern int SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool bRedraw);

    [DllImport("gdi32.dll")]
    static extern IntPtr CreateRoundRectRgn(int x1, int y1, int x2, int y2, int cx, int cy);

    public OvalTextBox()
    {
        base.BorderStyle = System.Windows.Forms.BorderStyle.None;
    }

    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);
        SetWindowRgn(this.Handle, CreateRoundRectRgn(0, 0, this.Width, this.Height, 20, 20), true);
    }
}
Run Code Online (Sandbox Code Playgroud)