C#/WinForm給控制項加入hint文字

來源:互聯網
上載者:User

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

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

 

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

 

然後在程式裡這樣調用即可。實現超簡單… (本文章無源碼,需要使用直接拷貝如上代碼即可)

 

 

PS:最後推薦一個很強大的,在CodeProject上看到的。

http://www.codeproject.com/Articles/15954/C-TextBox-with-Outlook-2007-style-prompt

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.