Winform 输入提示 水印 TextBox ComboBox
2015-01-04 11:57
585 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Windows.Forms; public static class TextBoxTool { 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> /// 为ComboBox设置提示文字 /// </summary> /// <param name="textBox">ComboBox</param> /// <param name="watermark">提示文字内容</param> public static void SetWatermark(this ComboBox textBox, string watermark) { COMBOBOXINFO info = GetComboBoxInfo(textBox); SendMessage(info.hwndItem, EM_SETCUEBANNER, 0, watermark); //SendMessage(textBox.Handle, EM_SETCUEBANNER, 0, watermark); } /// <summary> /// 清除提示文字 /// </summary> /// <param name="textBox">ComboBox</param> public static void ClearWatermark(this ComboBox textBox) { SendMessage(textBox.Handle, EM_SETCUEBANNER, 0, string.Empty); } [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(); } }
上述静态类方法完成之后,调用就简单了,直接在窗体加载的时候添加上需要提示水印的相关控件代码即可:
textBox1.SetWatermark("请输入...");//TextBox
comboBox1.SetWatermark("请选择...");//ComboBox
PS:网上搜罗&组合而成......
相关文章推荐
- Winform界面设计之-TextBox水印提示
- winform textBox输入提示
- 在winform中向textbox输入内容时下面有提示信息,效果如图所示:
- winform ComboBox/TextBox自动提示
- 黑马程序员--学习winForm概念、简单的windows程序、TextBox、ComboBox及用法
- C#之winform基础 ,form1显示form2中textbox中输入的文本,窗体间传值
- winform中如何在TextBox中只能输入数字(可以带小数点)
- C# WINFORM TEXTBOX水印效果
- winform中限制textbox中仅能输入数字及小数点
- C#中TextBox水印提示的简单实现
- 黑马程序员—关于winform中如何令textbox随输入的文本内容的长度而实时改变宽度
- 《我的编程笔记》C#篇-comboBox与textBox搜索提示功能
- Winform TextBox中只能输入数字的几种常用方法(C#)
- comboBox输入自动提示 自动关联(仿ie地址栏)C#
- vs2015 c#通过ComboBox选择查询类别并在TextBox输入条件进行查询
- net2.0提供WinForm中TextBox智能提示,和补全的支持
- winform 中TextBox只能输入数字
- winform中控制TextBox中只能输入数字
- WinForm 限制TextBox只能输入数字和小数点
- C#之WinForm基础 textbox控件变高,由单行输入改为多行输入