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”,效果如下 ...
随机推荐
- SlideLayout
https://github.com/rey5137/SlideLayout
- pt-online-schema-change原理解析 博客相关需要阅读
xiaoboluo768 http://www.lai18.com/user/481193.html 都说pt-toolkit工具集中的pt-online-schema-change可以在线不锁 ...
- 在线服务之socket编程科普
简介 本篇文章是介绍一个典型的在线C++服务的最底层socket管理是如何实现的. 文章会从一个最简单的利用socket编程基础API的一个小程序开始,逐步引入现在典型的select,epoll机制, ...
- 如何扩大VMware虚拟机的硬盘磁盘空间大小
首先,在虚拟机配置界面通过界面配置,直接扩大虚拟机硬盘大小: 而后,登陆虚拟机,在windows磁盘管理,更多操作中直接“重新扫描磁盘”,操作系统自动找到了多出来的磁盘空间: 最后,在老磁盘分区上通过 ...
- AngularJS - 定时器 倒计时例子
<body> <div ng-app="myApp"> <div ng-controller="firstController"& ...
- textarea 中的换行符问题
下面是我对这个问题的解决过程,最后算是完全搞懂了,真是阴沟里险些翻船 1.必须知道textarea中的换行符是 \n (个人检测发现按回车键是\n,好像在linux下是\r\n) 2.用nl2br之 ...
- javascript类,对象,实例
在JS中,创建对象(Create Object)并不完全是我们时常说的创建类对象,JS中的对象强调的是一种复合类型一.由一对大括号括起来var emptyObj = {}; var myObj = { ...
- 关于php配置文件
一:配置文件(php.ini)在 PHP 启动时被读取.对于服务器模块版本的 PHP,仅在 web 服务器启动时读取一次.对于 CGI 和 CLI 版本,每次调用都会读取. 二:Note that t ...
- spring源码分析之spring-web web模块分析
0 概述 spring-web的web模块是更高一层的抽象,它封装了快速开发spring-web需要的基础组件.其结构如下: 1. 初始化Initializer部分 1.1 Servlet3.0 的 ...
- Partitioning, Shuffle and sort
Partitioning, Shuffle and sort what happened? - Partitioning Partitioning is the process of determi ...