zoukankan      html  css  js  c++  java
  • WinForm给控件加入hint文字

    本文代码主要是参考别人的,仅为个人记录,方面后续使用~

    效果图:

    HintText

    主要代码在一个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, 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();
            }
        }

    本文地址: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

  • 相关阅读:
    24 Scrapy爬虫的基本使用
    22 Scrapy框架简介
    21 Scrapy框架的安装
    19 正则表达式的基本知识
    18 “中国大学排名定向爬虫”实例介绍
    17 基于bs4库的HTML内容查找方法
    16 信息标记形式及信息提取的一般方法
    python中with as语句的用法
    Firefox安装Charles配置https后无法上网解决方案(转载)
    Windows下(Win10)Charles从下载安装到证书设置和浏览器抓包测试,亲测有效!(转载)
  • 原文地址:https://www.cnblogs.com/Interkey/p/HintText.html
Copyright © 2011-2022 走看看