前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#/WinForm给控件加入hint文字

C#/WinForm给控件加入hint文字

作者头像
跟着阿笨一起玩NET
发布2018-09-18 15:41:21
1.3K0
发布2018-09-18 15:41:21
举报
文章被收录于专栏:跟着阿笨一起玩NET

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

image
image

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

申明一个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;

        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();         }

    }

--------------------------------------------------------------------------------------------------------------------------

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

image
image

-------------------------------------------------------

另外一直想研究一下Win32API,不知道谁有没有好的资料推荐一下。

做了这么久程序猿对系统的内核还是未入门阶段,真心惭愧!!!

大侠们有资料就推荐一下哦, 3Q

本人从事网站开发,WP/Win8开发。目前想和一些有志向的朋友一同研究

一下WP/Win8这些新事物,一起玩转论坛,一起合作出软件,一起学习,一起赚Money.

有意向者联系QQ:61797528

海内存知己,天涯若比邻

-------------------------------------------------------

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2012-10-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档