作者: 乔克斯
查看: 12097|回复: 22
打印 上一主题 下一主题

[源码] 【水印文本框】C#WinForm给控件加入水印文字

[复制链接]
跳转到指定楼层
楼主
乔克斯 发表于 2014-9-20 00:42:09 | 只看该作者 |只看大图 回帖奖励 |正序浏览 |阅读模式
查看: 12097|回复: 22
C#WinForm给控件加入水印文字

说明与效果图:
今天突然来了一个这样的需求,需要在C#的编辑框上加入一个Hint水印效果,类似如下图:

以前在手机上(wp)上做过类似的效果。参考silverlight toolkit 的searchTextBox。
现在要在winform下制作,开始我还以为应该有啥啥属性可以一键搞定,结果目测了一下,没有什么属性,于是乎百度了一下,网上说用win32API来做,这倒挺神奇的,参考别人做了如下列子。
申明一个Win32Utility类,静态的。

代码如下:
[C#] 纯文本查看 复制代码
public static class Win32Utility
{

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern Int32 SendMessage(IntPtr hWnd, int msg,
int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);

[DllImport("user32.dll")]
private static extern bool SendMessage(IntPtr hwnd, int msg, int wParam, StringBuilder lParam);

[DllImport("user32.dll")]
private static extern bool GetComboBoxInfo(IntPtr hwnd, ref COMBOBOXINFO pcbi);

[StructLayout(LayoutKind.Sequential)]
private struct COMBOBOXINFO
{
public int cbSize;
public RECT rcItem;
public RECT rcButton;
public IntPtr stateButton;
public IntPtr hwndCombo;
public IntPtr hwndItem;
public IntPtr hwndList;
}

[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}

private const int EM_SETCUEBANNER = 0x1501;
private const int EM_GETCUEBANNER = 0x1502;

public static void SetCueText(Control control, string text)
{
if (control is ComboBox)
{
COMBOBOXINFO info = GetComboBoxInfo(control);
SendMessage(info.hwndItem, EM_SETCUEBANNER, 0, text);
}
else
{
SendMessage(control.Handle, EM_SETCUEBANNER, 0, text);
}
}

private static COMBOBOXINFO GetComboBoxInfo(Control control)
{
COMBOBOXINFO info = new COMBOBOXINFO();
//a combobox is made up of three controls, a button, a list and textbox;
//we want the textbox
info.cbSize = Marshal.SizeOf(info);
GetComboBoxInfo(control.Handle, ref info);
return info;
}

public static string GetCueText(Control control)
{
StringBuilder builder = new StringBuilder();
if (control is ComboBox)
{
COMBOBOXINFO info = new COMBOBOXINFO();
//a combobox is made up of two controls, a list and textbox;
//we want the textbox
info.cbSize = Marshal.SizeOf(info);
GetComboBoxInfo(control.Handle, ref info);
SendMessage(info.hwndItem, EM_GETCUEBANNER, 0, builder);
}
else
{
SendMessage(control.Handle, EM_GETCUEBANNER, 0, builder);
}
return builder.ToString();
}

}

然后在程序里这样调用即可。实现超简单… (本文章无源码,需要使用直接拷贝如上代码即可)

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏9 转播转播
回复 论坛版权

使用道具 举报

23#
Summer大大 发表于 2017-3-1 19:19:14 | 只看该作者
[C#] 纯文本查看 复制代码
public String WaterText { get; set; }
        private void textBox1_Enter(object sender, EventArgs e)
        {
            if (textBox1.Text == WaterText || textBox1.Text == "") 
            {
                textBox1.Text = "";
                textBox1.ForeColor = Color.Black;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            WaterText = "要设置的水印文字";
            textBox1.Text = WaterText;
            textBox1.ForeColor = Color.Gray;
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox1.Text == WaterText)
            {
                textBox1.Text = WaterText;
                textBox1.ForeColor = Color.Gray;
            }
        }
22#
chn0000000 发表于 2017-2-21 21:51:31 | 只看该作者
复合物后和人格让我会更好
21#
小口田 发表于 2017-2-21 13:06:56 | 只看该作者
感谢分享。
回复

使用道具 举报

20#
esf5021314 发表于 2016-10-21 15:30:28 | 只看该作者
谢谢楼猪分享,楼猪辛苦了~
19#
iamlonelystar 发表于 2016-10-21 15:15:24 | 只看该作者
好文章!谢谢楼主
18#
iamlonelystar 发表于 2016-10-21 15:14:52 | 只看该作者
好文章!谢谢楼主
17#
yinghao2005 发表于 2016-10-10 14:26:16 | 只看该作者
强学习了...
回复

使用道具 举报

16#
梦玄庭 发表于 2016-9-25 17:48:28 | 只看该作者
试了,没效果啊,需要注意什么吗?textbox的属性?
15#
bruce301 发表于 2016-9-14 16:26:17 | 只看该作者
测试了不行,有什么要注意的吗?
您需要登录后才可以回帖 登录 | 加入CSkin博客

本版积分规则

QQ|申请友链|小黑屋|手机版|Archiver|CSkin ( 粤ICP备13070794号

Powered by Discuz! X3.2  © 2001-2013 Comsenz Inc.  Designed by ARTERY.cn
GMT+8, 2024-5-19 07:14, Processed in 0.588972 second(s), 35 queries , Gzip On.

快速回复 返回顶部 返回列表