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

    本文转载:http://www.cnblogs.com/qingci/archive/2012/10/15/2724373.html

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

    image

    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

    PS:最后推荐一个很强大的,在CodeProject上看到的。

    http://www.codeproject.com/Articles/15954/C-TextBox-with-Outlook-2007-style-prompt
  • 相关阅读:
    抓住大数据的3个核心点 走在爆款前面
    小白学数据分析--回归分析在游戏人气分析的应用探索
    小白学数据分析--回归分析在游戏人气分析的应用探索
    Linux,unix,cygwin,centeros下的tar压缩解压缩命令具体解释
    二:Java之异常处理
    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第14章节--使用Office Services开发应用程序 WOPI和新的Office Web Apps Server
    Git 的BUG小结
    android:json解析的两个工具:Gson和Jackson的使用小样例
    Cocos2d-x3.0终于版Mac以及Win系统相关环境部署
    第五十六题(最长公共子串)
  • 原文地址:https://www.cnblogs.com/51net/p/2729749.html
Copyright © 2011-2022 走看看