簡體   English   中英

在TextBox C#中更改邊框顏色

[英]Change border color in TextBox C#

我有以下代碼:

public class OurTextBox : TextBox
{
    public OurTextBox()
        : base()
    {
        this.SetStyle(ControlStyles.UserPaint, true);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
         base.OnPaint(e);
         Pen penBorder = new Pen(Color.Gray, 1);
         Rectangle rectBorder = new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 1);
         e.Graphics.DrawRectangle(penBorder, rectBorder);
   }
}

這是完美的工作方式,但是直到獲得焦點時它才顯示文本。

有誰能夠幫助我? 怎么了?

預先感謝。

若要更改TextBox邊框顏色,可以重寫WndProc方法並處理WM_NCPAINT消息。 然后,使用GetWindowDC獲取控件的窗口設備上下文,因為我們想繪制到控件的非客戶區域。 然后進行繪制,就足以從該上下文中創建一個Graphics對象,然后繪制邊框進行控制。

若要在BorderColor屬性更改時重繪控件,可以使用RedrawWindow方法。

這是一個具有BorderColor屬性的TextBox 如果屬性值與Color.Transparent不同,則控件使用BorderColor ,而BorderStyle是其默認值Fixed3d

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class MyTextBox : TextBox {
    const int WM_NCPAINT = 0x85;
    const uint RDW_INVALIDATE = 0x1;
    const uint RDW_IUPDATENOW = 0x100;
    const uint RDW_FRAME = 0x400;
    [DllImport("user32.dll")]
    static extern IntPtr GetWindowDC(IntPtr hWnd);
    [DllImport("user32.dll")]
    static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
    [DllImport("user32.dll")]
    static extern bool RedrawWindow(IntPtr hWnd, IntPtr lprc, IntPtr hrgn, uint flags);
    Color borderColor = Color.Blue;
    public Color BorderColor {
        get { return borderColor; }
        set { borderColor = value;
            RedrawWindow(Handle, IntPtr.Zero, IntPtr.Zero,
                RDW_FRAME | RDW_IUPDATENOW | RDW_INVALIDATE);
        }
    }
    protected override void WndProc(ref Message m) {
        base.WndProc(ref m);
        if (m.Msg == WM_NCPAINT && BorderColor != Color.Transparent &&
            BorderStyle == System.Windows.Forms.BorderStyle.Fixed3D) {
            var hdc = GetWindowDC(this.Handle);
            using (var g = Graphics.FromHdcInternal(hdc))
            using (var p = new Pen(BorderColor))
                g.DrawRectangle(p, new Rectangle(0, 0, Width - 1, Height - 1));
            ReleaseDC(this.Handle, hdc);
        }
    }
    protected override void OnSizeChanged(EventArgs e) {
        base.OnSizeChanged(e);
        RedrawWindow(Handle, IntPtr.Zero, IntPtr.Zero,
               RDW_FRAME | RDW_IUPDATENOW | RDW_INVALIDATE);
    }
}

結果

這是使用不同顏色和不同狀態的結果。 如下面的圖片所示,支持邊框的所有狀態,並且可以使用任何顏色作為邊框:

在此處輸入圖片說明

下載

您可以克隆或下載工作示例:

您還必須手動繪制文本。

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    Pen penBorder = new Pen(Color.Gray, 1);
    Rectangle rectBorder = new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 1);
    e.Graphics.DrawRectangle(penBorder, rectBorder);

    Rectangle textRec = new Rectangle(e.ClipRectangle.X + 1, e.ClipRectangle.Y + 1, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 1);
    TextRenderer.DrawText(e.Graphics, Text, this.Font, textRec, this.ForeColor, this.BackColor, TextFormatFlags.Default);
}

另外,如果TextRenderer沒有給您想要的結果,您可以嘗試使用e.Graphics.DrawString()方法e.Graphics.DrawString()您總是可以通過這種方法獲得更好的結果)。

有幾種方法可以做到這一點,但都不是理想的方法。 這只是WinForms的本質。 但是,您有一些選擇。 我將總結一下:

可以通過以下方式將TextBox嵌入Panel來實現所需的一種方法。

public class BorderedTextBox : Panel 
{
    private TextBox textBox;
    private bool focusedAlways = false;
    private Color normalBorderColor = Color.Gray;
    private Color focusedBorderColor = Color.Red;

    public BorderTextBox() 
    {
        this.DoubleBuffered = true;
        this.Padding = new Padding(2);

        this.TextBox = new TextBox();
        this.TextBox.AutoSize = false;
        this.TextBox.BorderStyle = BorderStyle.None;
        this.TextBox.Dock = DockStyle.Fill;
        this.TextBox.Enter += new EventHandler(this.TextBox_Refresh);
        this.TextBox.Leave += new EventHandler(this.TextBox_Refresh);
        this.TextBox.Resize += new EventHandler(this.TextBox_Refresh);
        this.Controls.Add(this.TextBox);
    }

    private void TextBox_Refresh(object sender, EventArgs e) 
    {
        this.Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e) 
    {
        e.Graphics.Clear(SystemColors.Window);
        using (Pen borderPen = new Pen(this.TextBox.Focused || FocusedAlways ? 
            focusedBorderColor : normalBorderColor)) 
        {
            e.Graphics.DrawRectangle(borderPen, 
                new Rectangle(0, 0, this.ClientSize.Width - 1, this.ClientSize.Height - 1));
        }
        base.OnPaint(e);
    }

    public TextBox TextBox
    {
        get { return textbox; }
        set { textbox = value; }
    }

    public bool FocusedAlaways
    {
        get { return focusedAlways; }
        set { focusedAlways = value; }
    }
}

您也可以執行此操作而不覆蓋任何控件,但是上述方法更好。 當控件獲得焦點時,上面將繪制邊框。 如果要永久FocusedAlways邊框,請將FocusedAlways屬性設置為True

我希望這有幫助。

將“文本框邊框”樣式設置為“無”,然后將此代碼寫入容器形式的“繪制”事件

    private void Form1_Paint(object sender, PaintEventArgs e)
        {
System.Drawing.Rectangle rect = new Rectangle(TextBox1.Location.X, TextBox1.Location.Y, TextBox1.ClientSize.Width, TextBox1.ClientSize.Height);

                rect.Inflate(1, 1); // border thickness
                System.Windows.Forms.ControlPaint.DrawBorder(e.Graphics, rect, Color.DeepSkyBlue, ButtonBorderStyle.Solid);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM