using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing; namespace NewMyFormDesigner { public partial class TextBoxEx : TextBox { private Color borderColor = Color.Black; // 设置默认的边框颜色 private static int WM_NCPAINT = 0x0085; // WM_NCPAINT message private static int WM_ERASEBKGND = 0x0014; // WM_ERASEBKGND message private static int WM_PAINT = 0x000F; // WM_PAINT message public TextBoxEx() { // This call is required by the Windows.Forms Form Designer. InitializeComponent(); // TODO: Add any initialization after the InitializeComponent call this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.BackColor = Color.Transparent; this.SetStyle(ControlStyles.UserPaint, false); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.DoubleBuffer, true); } public void DrawBorder(ref Message message, int width, int height) { if (message.Msg == WM_NCPAINT || message.Msg == WM_ERASEBKGND || message.Msg == WM_PAINT) { using (Graphics graphics = Graphics.FromHwnd(base.Handle)) { Rectangle rectangle = new Rectangle(0, 0, width, height); ControlPaint.DrawBorder(graphics, rectangle, Color.White, 0, ButtonBorderStyle.None, Color.White, 0, ButtonBorderStyle.None, Color.White, 0, ButtonBorderStyle.None, BorderColor, 1, ButtonBorderStyle.Solid); } } } [Description("设置边框颜色")] public Color BorderColor { get { return borderColor; } set { borderColor = value; } } protected override void WndProc(ref Message m) { base.WndProc(ref m); DrawBorder(ref m, this.Width, this.Height); } } }