WinForm给控件加入hint文字
本文代码主要是参考别人的,仅为个人记录,方面后续使用~
效果图:

主要代码在一个Win32Utility类中,代码如下:
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; //本文地址:http://www.cnblogs.com/Interkey/p/HintText.html public static void SetCueText(Control control, string text)
{
if (control is ComboBox)
{
COMBOBOXINFO info = GetComboBoxInfo(control);
SendMessage(info.hwndItem, EM_SETCUEBANNER, , text);
}
else
{
SendMessage(control.Handle, EM_SETCUEBANNER, , 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, , builder);
}
else
{
SendMessage(control.Handle, EM_GETCUEBANNER, , builder);
}
return builder.ToString();
}
}
本文地址:http://www.cnblogs.com/Interkey/p/HintText.html
使用时,如以下代码即可,只不过仅限在少数控件上(主要用在TextBox上,当然也可以用在ComboBox上,不过有限制)
Win32Utility.SetCueText(this.tb_UserName, "请输入用户名");
Win32Utility.SetCueText(this.tb_Password, "请输入密码");
Win32Utility.SetCueText(this.tb_ConfigPassword, "请输入确认密码");
Win32Utility.SetCueText(this.tb_Email, "请输入邮箱"); //Win32Utility.SetCueText(this.richTextBox1, "123456");//设置其他控件无效 //this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;//在这种样式下无效
Win32Utility.SetCueText(this.comboBox1, "请选择");
以上即可实现效果,不过在此提供了相应代码下载;
本文参考了以下文章:
C#/WinForm给控件加入hint文字
C# TextBox with Outlook 2007-style prompt
WinForm给控件加入hint文字的更多相关文章
- CSharpGL(26)在opengl中实现控件布局/渲染文字
CSharpGL(26)在opengl中实现控件布局/渲染文字 效果图 如图所示,可以将文字.坐标轴固定在窗口的一角. 下载 CSharpGL已在GitHub开源,欢迎对OpenGL有兴趣的同学加入( ...
- winform窗体控件(全)
回顾跟补充下除了昨天那常用6个其他的winform窗体控件作用 1:Button:按钮 (1)AutoSize:如果是True的情况下,内容将会撑开:False的话会另起一行 (2)Enabled: ...
- winform基本控件----按钮
这次来引用一个我们上课时候老师给的一个实验内容,来说一下winform程序设计中的按钮控件的使用.下面是我们老师给的实验内容. 实验目的: 掌握Winform的开发环境. 掌握窗体的创建和基本方法. ...
- DevExpress Winform 常用控件
Ø 前言 DevExpress 控件的功能比较强大,是全球知名控件开发公司,对于开发 B/S 或 C/S 都非常出色,可以实现很炫且功能强大的效果. DevExpress Winform 常用控件是 ...
- 在DevExpress程序中使用Winform分页控件直接录入数据并保存
一般情况下,我们都倾向于使用一个组织比较好的独立界面来录入或者展示相关的数据,这样处理比较规范,也方便显示比较复杂的数据.不过在一些情况下,我们也可能需要直接在GridView表格上直接录入或者修改数 ...
- C#实现WinForm DataGridView控件支持叠加数据绑定
我们都知道WinForm DataGridView控件支持数据绑定,使用方法很简单,只需将DataSource属性指定到相应的数据源即可,但需注意数据源必须支持IListSource类型,这里说的是支 ...
- 在Image控件中绘制文字
//Canvas 在Image控件中绘制文字 procedure TForm1.Button1Click(Sender: TObject);begin image1.Canvas.Font.Size ...
- C# WinForm实现控件拖动实例介绍
主要是设计控件的MouseDown.MouseLeave.MouseMove事件.一步步来吧:1.定义一个枚举类型,描述光标状态 private enum EnumMousePointPosition ...
- Winform DevExpress控件库(一) DevExpress控件库的安装与新建第一个DevExpress项目
前言:因为这段时间要接触到DevExpress控件库,而我本身甚至对winform的控件都了解甚少,所以处在学习中,写下博客主要是为了方便后期的回顾,当然也可以给一些新人第一次接触时做为学习的参考,以 ...
随机推荐
- dolby逝世:纪念一下
杜比公司的成立快50年了(1965),想想中国1965年在干啥.中国怎么可能有. 小科普一下,dolby的成功有3个时间点和技术,第一次是在英国开发了dolby B降噪技术,是用在早期的卡带降噪 ...
- css 单位转换
如今 css 的单位越来越多了,px, em, rem, 微信的小程序又出来个 rpx 可以用 less 自动生成需要的单位 但当你只是想把一个已有的页面转换成小程序时,可能更需要一个 px -> ...
- 在GridView中使用radioButoon
在GridView中使用radioButoon 方法一: <input type="radio" id='radioSelectFeed' name="radioD ...
- IOS RSA 加密方式
采用RSA加密方式,主要是生成公钥和私钥,公钥用来加密,私钥用来解密,至于其中如何实现的,网上有很多原理. 参见如下: https://github.com/jslim89/RSA-objc PS: ...
- Spring AOP Schema aop:config、tx:advice
Spring AOP Schema aop:config.tx:advice 一. 利用aop:config标签实现AOP 首先看个例子,如下 接口代码: package com.lei. ...
- [转]BloomFilter——大规模数据处理利器
Bloom Filter是由Bloom在1970年提出的一种多哈希函数映射的快速查找算法.通常应用在一些需要快速判断某个元素是否属于集合,但是并不严格要求100%正确的场合. 一. 实例 为了说明Bl ...
- Ubuntu apt命令
http://www.tecmint.com/useful-basic-commands-of-apt-get-and-apt-cache-for-package-management/ apt-ca ...
- JavaScript封装Ajax(类JQuery中$.ajax()方法)
ajax.js (function(exports, document, undefined){ "use strict"; function Ajax(){ if(!(this ...
- HDU 3791 二叉搜索树
二叉搜索树 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- Reactor模式与Proactor模式
该文章总结了网上资源对这两种模式的描述 原文地址:http://www.cnblogs.com/dawen/archive/2011/05/18/2050358.html 1.标准定义 两种I/O多路 ...