本文实例展示了WinForm实现为TextBox设置水印文字功能,非常实用的技巧,分享给大家供大家参考。

关键代码如下

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms; namespace WinFormUtilHelpV2
{
/// <summary>
/// 基于.NET 2.0的TextBox工具类
/// </summary>
public static class TextBoxToolV2
{
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); /// <summary>
/// 为TextBox设置水印文字
/// </summary>
/// <param name="textBox">TextBox</param>
/// <param name="watermark">水印文字</param>
public static void SetWatermark(this TextBox textBox, string watermark)
{
SendMessage(textBox.Handle, EM_SETCUEBANNER, 0, watermark);
}
/// <summary>
/// 清除水印文字
/// </summary>
/// <param name="textBox">TextBox</param>
public static void ClearWatermark(this TextBox textBox)
{
SendMessage(textBox.Handle, EM_SETCUEBANNER, 0, string.Empty);
}
}
}

  

 

测试代码如下:

using System;
using System.Windows.Forms;
using WinFormUtilHelpV2; namespace WinFormUtilHelpV2Test
{
public partial class WinTextBoxToolV2Test : Form
{
public WinTextBoxToolV2Test()
{
InitializeComponent();
} private void WinTextBoxToolV2Test_Load(object sender, EventArgs e)
{
textBox1.SetWatermark("请输入用户名称....");
textBox2.SetWatermark("请输入用户密码....");
} private void button1_Click(object sender, EventArgs e)
{
textBox1.ClearWatermark();
textBox2.ClearWatermark();
}
}
}

  

测试效果如下图所示:

最新文章

  1. Kooboo CMS技术文档之二:Kooboo CMS的安装步骤
  2. docker help
  3. Odoo Many2many 指定默认分组过滤
  4. Grunt - grunt-contrib-connect
  5. 使用oss批量上传图片
  6. 《JS权威指南学习总结--6.1原型》
  7. SQL Server 日志传送[转载]
  8. Activiti工作流学习笔记
  9. Word自定义多级列表样式
  10. jquery.axios无刷新机制删除
  11. mysql INSERT ... ON DUPLICATE KEY UPDATE语句
  12. 用Python进行人脸识别
  13. ios开发UI篇--UIButton
  14. Windows环境下,从零开始搭建Nodejs+Express+Ejs框架(二)---安装Express,ejs
  15. 标头“Vary:Accept-Encoding”指定方法[转]
  16. Spring Boot gradle 集成servlet/jsp 教程及示例
  17. 链表求和12 &#183; Add Two Numbers
  18. .net core系列之《对AOP思想的理解及使用AspectCore实现自定义日志拦截》
  19. bootstrap datetimepicker 日期插件超详细使用方法
  20. 前端 and css小技巧

热门文章

  1. 快速了解IOC的几种姿势
  2. xml配置文件的读写
  3. UIkit框架之uiUIapplication
  4. android手动修改density(dpi)的方法
  5. 使用js实现input输入框的增加
  6. 获取Ca证书相关与服务器信息
  7. HTML5日期输入类型(date)
  8. 如何用js定义数组,用js来拼接json字段
  9. Cocos2d-x win7 + vs2010 配置图文详解
  10. eclipse控制台中文乱码解决方法