本文实例展示了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. 查看数据库中没有进行comment的字段

    为落实数据库规范,既每个表字段都需要有comment注释,所以需要过滤生产表中没有comment的字段,搜索出还不错的过滤语句 show full columns from table where C ...

  2. :before :after

    CSS 有两个说不上常用的伪类 :before 和 :after,偶尔会被人用来添加些自定义格式什么的,但是它们的功用不仅于此.前几天发现了 Creative Link Effects 这个非常有意思 ...

  3. TOMCAT运行一段时间后网页无响应或连不上,TOMCAT无错误日志

    解决方法:修改 tomcat 的 java options 参数1)增加java options参数-Xmn384m-XX:MaxPermSize=128m-XX:+UseConcMarkSweepG ...

  4. HTTP2试用小记

    原文:https://www.clarencep.com/2016/11/17/upgrade-nginx-to-support-http2/ 这两天把公司的网站升级到了全站https. 顺便瞄到了H ...

  5. Socket通信代码(原理)

    1.运行环境:NetBeans IDE 6.0.1 2.说明:先运行服务器端,再运行客户端. 3.服务器端代码: 新建java类Test import java.net.*; import java. ...

  6. CentOS7 下 安装 supervisor以及使用

    CentOS7 下 安装 supervisor 以及使用 手动安装 [注] linux环境必须安装 python 1.获取supervisor包:[https://pypi.python.org/py ...

  7. Singleton Design Pattern

    The Singleton pattern is one of the simplest design patterns, which restricts the instantiation of a ...

  8. xcode 第三方插件遇到问题

    xcode 第三方安装的路径 ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins 

  9. Razor视图添加命名空间

    在.cshtml文件添加@using MyNamespace,只是在页面添加引用这样编译不通过,还需要在view文件夹的web.config添加引用,找到<namespaces>添加< ...

  10. ASP.NET项目部署到Linux服务器出现服务器错误

    在Linux系统中安装了Mono和Apache作为Web服务器,使用Visual Studio开发的ASP.NET Web应用或者API应用,在部署到Linux服务器后出现服务器错误,其中一个原因是由 ...