C# WinForm TextBox添加水印效果
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添加水印效果的更多相关文章
- c#: TextBox添加水印效果(PlaceHolderText)
基于他人代码修改,不闪,以做备忘. 与SendMessage EM_SETCUEBANNER消息相比,它能改变字体绘制颜色,EM_SETCUEBANNER只限定了DimGray颜色,太深 //与Sen ...
- WPF文本框密码框添加水印效果
WPF文本框密码框添加水印效果 来源: 阅读:559 时间:2014-12-31 分享: 0 按照惯例,先看下效果 文本框水印 文本框水印相对简单,不需要重写模板,仅仅需要一个VisualBrush ...
- WPF 之 文本框及密码框添加水印效果
1.文本框添加水印效果 文本框水印相对简单,不需要重写模板,仅仅需要一个 VisualBrush 和触发器验证一下Text是否为空即可. <TextBox Name="txtSerac ...
- winform textbox 的自动实现功能
好久没写博客了,主要是太懒了,之前因为做bs的比较多现在想转cs端了,虽然现在做cs也一年了,可接触的东西太过零碎了,以至于感觉这一年好像什么都没有学到.估计是因为学了之后没有记录,不扎实,然后又忘记 ...
- WinForm窗体淡入效果界面的简单实现方法
WinForm窗体淡入效果主要使用到控件的Opacity属性 首先在WinForm窗体中拖入一个Timer控件,然后再Timer控件的Tick事件添加如下代码: private void timer1 ...
- WPF的TextBox水印效果详解
一种自以为是的方式: 本来只是想简单的做个水印效果,在文本框内容为空的时候提示用户输入,这种需求挺常见.网上一搜 都是丢给你你一大段xaml代码.用c#代码实现我是不倾向了 既然用wpf就得Xaml啊 ...
- Java 图片添加水印效果
package com.xiaowu.drawwater.demo; import java.awt.AlphaComposite; import java.awt.Graphics2D; impor ...
- WinForm - 窗体淡入效果界面的简单实现方法
WinForm窗体淡入效果主要使用到控件的Opacity属性 首先在WinForm窗体中拖入一个Timer控件,然后再Timer控件的Tick事件添加如下代码: private void timer1 ...
- Winform Textbox控件字体垂直居中
项目中遇到要求Textbox内的字体垂直居中的问题,在网上找一直没有理想的解决方案.后来发现可以通过设置控件的字体来达到预期的效果. 默认的Textbox的Font属性为 “宋体, 9pt”,效果如下 ...
随机推荐
- VB连接Mysql数据库
当然机器装有mysql数据库 然后下载安装Mysql,odbc驱动 须要加入ado'菜单"project"->"引用" 找 Microsoft Activ ...
- Javascript Design Patterns - Js Class
JavaScript is a class-less language, however classes can be simulated using functions. eg: // A car ...
- Codeforces Round #326 (Div. 2) B. Duff in Love 分解质因数
B. Duff in Love Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/588/proble ...
- Codeforces gym 100685 E. Epic Fail of a Genie 贪心
E. Epic Fail of a GenieTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685 ...
- 1020. Tree Traversals (25)
the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1020 and the ...
- Lastest Version Carprog Full V7.28 update and EEPROM reading
Carprog Full has recently launched the newest V7.28 (with all software activated and all 21items Ada ...
- 安卓Design包之TabLayout控件的使用
转自: 安卓Design包之TabLayout控件的简单使用 Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的Android ...
- JAVA实现DES加密
DES加密介绍 DES是一种对称加密算法,所谓对称加密算法即:加密和解密使用相同密钥的算法.DES加密算法出自IBM的研究,后来被美国政府正式采用,之后开始广泛流传,但是近些年使用越来越少 ...
- C#中,为什么结构体也可以设置构造函数?
结构体派生自ValueType,ValueType派生自Object,可访问Object的方法.结构体是一种缩小版的类.结构体不能继承.结构体总是有一个无参数的默认构造函数,不允许替换.结构体可指定字 ...
- LeetCode 343
Integer Break Given a positive integer n, break it into the sum of at least two positive integers an ...