直接上源码
public class WaterTextBox : TextBox { //private const int EM_SETCUEBANNER = 0x1501; //[DllImport("user32.dll", CharSet = CharSet.Auto)] //private static extern Int32 SendMessage // (IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam); string waterText = string.Empty; [Category("外观")] [Description("设置水印文本")] [DefaultValue(typeof(String), "提示文字")] public string WaterText { get { return waterText; } set { waterText = value; this.Invalidate(); } } public WaterTextBox() { // base.SetStyle( //ControlStyles.UserPaint | // 控件将自行绘制,而不是通过操作系统来绘制 //ControlStyles.OptimizedDoubleBuffer | // 该控件首先在缓冲区中绘制,而不是直接绘制到屏幕上,这样可以减少闪烁 //ControlStyles.AllPaintingInWmPaint | // 控件将忽略 WM_ERASEBKGND 窗口消息以减少闪烁 //ControlStyles.ResizeRedraw | // 在调整控件大小时重绘控件 //ControlStyles.SupportsTransparentBackColor, // 控件接受 alpha 组件小于 255 的 BackColor 以模拟透明 //true); // 设置以上值为 true // base.UpdateStyles(); base.SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.BorderStyle = BorderStyle.FixedSingle; //this.BorderStyle = Forms.BorderStyle.None; this.SuspendLayout(); this.Font = new System.Drawing.Font("微软雅黑", 9f); this.ResumeLayout(false); this.WaterText = "水印文字"; this.AutoSize = false; this.Height = 18; HelpText = "选择"; HelpWidth = 40; } [DllImport("kernel32.dll", CharSet = CharSet.Auto)] static extern IntPtr LoadLibrary(string lpFileName); /// <summary> /// 支持透明背景 /// </summary> protected override CreateParams CreateParams { get { if (BackColor != Color.Transparent) return base.CreateParams; CreateParams prams = base.CreateParams; if (LoadLibrary("msftedit.dll") != IntPtr.Zero) { prams.ExStyle |= 0x020; prams.ClassName = "RICHEDIT50W"; } return prams; } } public int HelpWidth { get; set; } protected override void WndProc(ref Message m) { base.WndProc(ref m); DrawBorder(ref m); if (m.Msg == 0xF) { WmPaint(ref m); } } Point mPoint = Point.Empty; protected override void OnMouseMove(MouseEventArgs e) { mPoint = e.Location; base.OnMouseMove(e); } // public override Cursor Cursor //{ // get // { // var rect = ClientRectangle; // rect.X = ClientRectangle.Width - HelpWidth; // rect.Width = HelpWidth; // if (rect.Contains(mPoint)) // return Cursors.Hand; // return base.Cursor; // } // set // { // base.Cursor = value; // } //} public Color BorderColor { get { return borderColor; } set { borderColor = value; } } private Color borderColor = Color.Orange; // default border color is 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 event Action<Point> OnDrawText; public bool IsSelect { get; set; } public string HelpText { get; set; } StringFormat sf = new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center }; [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 void DrawBorder(ref Message message) { if (message.Msg == WM_NCPAINT || message.Msg == WM_ERASEBKGND || message.Msg == WM_PAINT) { using (Graphics graphics = Graphics.FromHwnd(this.Handle)) { if (this.BorderStyle == BorderStyle.FixedSingle) { Rectangle rectangle = this.ClientRectangle; ControlPaint.DrawBorder(graphics, rectangle, borderColor, ButtonBorderStyle.Solid); graphics.Dispose(); message.Result = (IntPtr)1; } //SetWindowRgn(this.Handle, CreateRoundRectRgn(0, 0, this.Width + 1, Height + 1, 5, 5), true); } } } protected override void OnPaintBackground(PaintEventArgs e) { base.OnPaintBackground(e); e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; using (GraphicsPath path = new GraphicsPath()) { path.AddArc(0, 0, Height - 1, Height - 1, 90, 180); path.AddArc(Width - Height, 0, Height - 1, Height - 1, 270, 180); path.CloseFigure(); e.Graphics.FillPath(Brushes.Orange, path); using (Pen pen = new Pen(Color.Green)) { e.Graphics.DrawPath(pen, path); } } } private void WmPaint(ref Message m) { using (Graphics graphics = Graphics.FromHwnd(base.Handle)) { if (string.IsNullOrEmpty(Text)) { TextFormatFlags format = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter; if (Multiline) { format = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter | TextFormatFlags.Left; } if (RightToLeft == RightToLeft.Yes) { format |= TextFormatFlags.RightToLeft | TextFormatFlags.Right; } TextRenderer.DrawText( graphics, WaterText, Font, ClientRectangle, Color.Gray, format); } //if (IsSelect) //{ // var rect = ClientRectangle; // rect.X = ClientRectangle.Width - HelpWidth; // rect.Width = HelpWidth; // graphics.FillRectangle(Brushes.Goldenrod, rect); // using (var brush = new SolidBrush(Color.White)) // graphics.DrawString(HelpText, Font, brush, rect, sf); // if(OnDrawText!=null) // { // OnDrawText(rect.Location); // } //} } } }
把这个放到项目中,然后重新生成项目,最后直接在工具箱上拖拽WaterTextBox就可以用