本文实例展示了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();
}
}
}

  

测试效果如下图所示:

Winform水印的更多相关文章

  1. Winform 水印TextBox

    方法一: public partial class WaterTextBox : TextBox { private readonly Label lblwaterText = new Label() ...

  2. 逆天通用水印支持Winform,WPF,Web,WP,Win10。支持位置选择(9个位置 ==》[X])

    常用技能:http://www.cnblogs.com/dunitian/p/4822808.html#skill 逆天博客:http://dnt.dkil.net 逆天通用水印扩展篇~新增剪贴板系列 ...

  3. [WinForm]为TextBox设置水印文字

    关键代码: using System; using System.Runtime.InteropServices; using System.Windows.Forms; namespace WinF ...

  4. winform工具1-图片去除水印

    效果图: 思路: 1.获取图片 2.处理水印 3.保存处理的图片 代码: 获取图片: private void button1_Click(object sender, EventArgs e) { ...

  5. winform的水印TextBox

    public partial class WaterTextBox : TextBox { private readonly Label lblwaterText = new Label(); pub ...

  6. WinForm中自定义搜索框(水印、清空按钮、加载中图标)

    public partial class CustomSearchBar : TextBox { private readonly Label lblwaterText = new Label(); ...

  7. (七十五)c#Winform自定义控件-控件水印组件

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  8. C# Winform 带水印提示输入框

    using System; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms ...

  9. 水印第三版 ~ 变态水印(这次用Magick.NET来实现,附需求分析和源码)

    技能 汇总:http://www.cnblogs.com/dunitian/p/4822808.html#skill 以前的水印,只是简单走起,用的是原生态的方法.现在各种变态水印,于是就不再用原生态 ...

随机推荐

  1. Linux安装FTP服务

    转自:http://blog.csdn.net/huangbiao86/article/details/6641952 ftp服务器 1.  在Linux和其他机器之间共享文件(在linux下安装ft ...

  2. web 调用WCF 每次都要重新编译才能正常使用 终于解决了

    项目中不知从何时起出现了个奇怪的问题,每次运行web都要全部重新编译解决方案,否则单独编译web,总是会出现WCF 调用失败的情况(什么无法激活服务,什么协议对地址不可用啊,七七八八的问题),因为这个 ...

  3. HTTP协议用的TCP但是只建立单向连接

    作者:IronTech链接:https://www.zhihu.com/question/20085992/answer/71742030来源:知乎著作权归作者所有,转载请联系作者获得授权. 下面的解 ...

  4. 初步了解Canvas

      <--------一些理论储备: ------------------------------------------------------------------------------ ...

  5. 【JavaScript】之【Object】

    见代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  6. linux一些常用命令

    本人是linux小白,因项目接触linux不久,收集了一些常用的linux命令,希望对后来者有用. --列出/var/data目录下按A-Z字母排序的前100个文件ls /var/data |sort ...

  7. meta头部标签意义作用!

    1.<meta name="viewport" id="viewport" content="width=device-width, initi ...

  8. Burp Suite 常用功能 0x01 扫描后台

    扫描后台: 代理→转到intruder 在链接后面添加变量,变量=后台地址t GET /§后台§ xt 注意: 配置完成点击菜单栏Intruder → start attack 开始扫描.

  9. POJ 1274 裸二分图匹配

    题意:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶,告诉每头奶牛愿意产奶的牛棚编号,求出最多能分配到的牛栏的数量. 分析:直接二分图匹配: #include<stdio.h> #includ ...

  10. pylot是一款开源的web性能测试工具

    pylot是一款开源的web性能测试工具,http://www.pylot.org/ 参考文档:http://www.pylot.org/gettingstarted.html很容易上手 使用分为以下 ...