1、新建项目添加WatermarkTextBox类:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel; namespace TextBoxWatermark
{
[ToolboxBitmap(typeof(TextBox))]
public class WatermarkTextBox : TextBox
{
private string _emptyTextTip;
private Color _emptyTextTipColor = Color.DarkGray;
private const int WM_PAINT = 0xF; public WatermarkTextBox()
: base()
{
} [DefaultValue("")]
public string EmptyTextTip
{
get { return _emptyTextTip; }
set
{
_emptyTextTip = value;
base.Invalidate();
}
} [DefaultValue(typeof(Color), "DarkGray")]
public Color EmptyTextTipColor
{
get { return _emptyTextTipColor; }
set
{
_emptyTextTipColor = value;
base.Invalidate();
}
} protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_PAINT)
{
WmPaint(ref m);
}
} private void WmPaint(ref Message m)
{
using (Graphics graphics = Graphics.FromHwnd(base.Handle))
{
if (Text.Length ==
&& !string.IsNullOrEmpty(_emptyTextTip)
&& !Focused)
{
TextFormatFlags format =
TextFormatFlags.EndEllipsis |
TextFormatFlags.VerticalCenter; if (RightToLeft == RightToLeft.Yes)
{
format |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
} TextRenderer.DrawText(
graphics,
_emptyTextTip,
Font,
base.ClientRectangle,
_emptyTextTipColor,
format);
}
}
}
}
}

2、在Form中实例化控件并添加:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms; namespace TextBoxWatermark
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); TextBoxWatermark.WatermarkTextBox tbName = new WatermarkTextBox();
tbName.EmptyTextTip = "请输入你的账号";
tbName.Location = new System.Drawing.Point(, );
tbName.Size = new System.Drawing.Size(, );
tbName.Name = "tbName";
tbName.TabStop = false;
//tbName.TabIndex = 0;
this.Controls.Add(tbName); TextBoxWatermark.WatermarkTextBox tbPassword = new WatermarkTextBox();
tbPassword.EmptyTextTip = "请输入你的密码";
tbPassword.Location = new System.Drawing.Point(, );
tbPassword.Size = new System.Drawing.Size(, );
tbPassword.Name = "tbPassword";
tbPassword.TabStop = false;
//tbName.TabIndex = 0;
this.Controls.Add(tbPassword);
}
}
}

C# WinForm TextBox添加水印效果的更多相关文章

  1. c#: TextBox添加水印效果(PlaceHolderText)

    基于他人代码修改,不闪,以做备忘. 与SendMessage EM_SETCUEBANNER消息相比,它能改变字体绘制颜色,EM_SETCUEBANNER只限定了DimGray颜色,太深 //与Sen ...

  2. WPF文本框密码框添加水印效果

    WPF文本框密码框添加水印效果 来源: 阅读:559 时间:2014-12-31 分享: 0 按照惯例,先看下效果 文本框水印 文本框水印相对简单,不需要重写模板,仅仅需要一个VisualBrush ...

  3. WPF 之 文本框及密码框添加水印效果

    1.文本框添加水印效果 文本框水印相对简单,不需要重写模板,仅仅需要一个 VisualBrush 和触发器验证一下Text是否为空即可. <TextBox Name="txtSerac ...

  4. winform textbox 的自动实现功能

    好久没写博客了,主要是太懒了,之前因为做bs的比较多现在想转cs端了,虽然现在做cs也一年了,可接触的东西太过零碎了,以至于感觉这一年好像什么都没有学到.估计是因为学了之后没有记录,不扎实,然后又忘记 ...

  5. WinForm窗体淡入效果界面的简单实现方法

    WinForm窗体淡入效果主要使用到控件的Opacity属性 首先在WinForm窗体中拖入一个Timer控件,然后再Timer控件的Tick事件添加如下代码: private void timer1 ...

  6. WPF的TextBox水印效果详解

    一种自以为是的方式: 本来只是想简单的做个水印效果,在文本框内容为空的时候提示用户输入,这种需求挺常见.网上一搜 都是丢给你你一大段xaml代码.用c#代码实现我是不倾向了 既然用wpf就得Xaml啊 ...

  7. Java 图片添加水印效果

    package com.xiaowu.drawwater.demo; import java.awt.AlphaComposite; import java.awt.Graphics2D; impor ...

  8. WinForm - 窗体淡入效果界面的简单实现方法

    WinForm窗体淡入效果主要使用到控件的Opacity属性 首先在WinForm窗体中拖入一个Timer控件,然后再Timer控件的Tick事件添加如下代码: private void timer1 ...

  9. Winform Textbox控件字体垂直居中

    项目中遇到要求Textbox内的字体垂直居中的问题,在网上找一直没有理想的解决方案.后来发现可以通过设置控件的字体来达到预期的效果. 默认的Textbox的Font属性为 “宋体, 9pt”,效果如下 ...

随机推荐

  1. HDU 4586 A - Play the Dice 找规律

    A - Play the DiceTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...

  2. [AngularJS] New in Angular 1.3 - Performance Boost with debugInfoEnabled

    By default, Angular provides a lot of debug information on the DOM that's only necessary for tools l ...

  3. iOS开发——UI_swift篇&UITableView实现索引功能

    UITableView实现索引功能     关于UItableView的索引在平时项目中所见不多,最多的就是跟联系人有关的界面,虽然如此,但是作为一个swift开发的程序必须知道的一个技术点,所以今天 ...

  4. 【ZZ】Python入门神图

    http://mp.weixin.qq.com/s?__biz=MzA3OTIxNTA0MA==&mid=401383338&idx=1&sn=73009cce06d58656 ...

  5. 关于使用NotificationComat导致android2.3及以下版本无法显示自定义布局的解决方法.

    大伙都知道 android-support-v4为我们提供了很多兼容的解决方案, 其中就有关于通知栏的. NotificationCompat, 顺利成章操刀显示通知. eg: Intent inte ...

  6. Http Message Converters with the Spring Framework--转载

    原文:http://www.baeldung.com/spring-httpmessageconverter-rest 1. Overview This article describes how t ...

  7. CentOS/Linux安装VNCserver

    VNC全称是Virtual Network Computing,属于远程控制类软件.其优点是支持跨操作系统的远程图形化控制.在日常工作中,服务器常常是存在机房,不可能每次需要图形界面操作就跑到机房,因 ...

  8. java 操作sqllite的数据库

    介绍 sqllite是一个小型数据库,不依赖于数据库服务器,操作它可以像操作本地的文本文件一样.在Android中是用来存储数据到本地的,java中可能也会有用到sqllite需要. 详细 sqlli ...

  9. SqlServer之游标深入

    原创文章,转载必需注明出处:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/introduce-for-sqlserver-curs ...

  10. mysql由于外键关联无法删除数据

    在mysql中删除一张表时候,出现 Error No. 1451 Cannot delete or update a parent row: a foreign key constraint fail ...