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


今天突然來了一個這樣的需求,需要在C#的編輯框上加入一個Hint水印效果,類似如下圖:

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

 

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

另外一直想研究一下Win32API,不知道誰有沒有好的資料推薦一下。

做了這么久程序猿對系統的內核還是未入門階段,真心慚愧!!!

大俠們有資料就推薦一下哦, 3Q

本人從事網站開發,WP/Win8開發。目前想和一些有志向的朋友一同研究

一下WP/Win8這些新事物,一起玩轉論壇,一起合作出軟件,一起學習,一起賺Money.

有意向者聯系QQ:61797528

海內存知己,天涯若比鄰

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM