Using Win32API to realize textbox watermark effect

Source: Internet
Author: User

Demo Effect:


(1) Create a Setwatertext static class
Two references to add:using System.Runtime.InteropServices; Using System.Windows.Forms;

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.InteropServices;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceWatermark Effect { Public Static classSetwatertext {[DllImport ("user32.dll", CharSet =CharSet.Auto)]Private Static externInt32 SendMessage (IntPtr hWnd,intmsg,intWParam, [MarshalAs (UNMANAGEDTYPE.LPWSTR)]stringLParam); [DllImport ("user32.dll")]        Private Static extern BOOLSendMessage (INTPTR hwnd,intMsgintWParam, StringBuilder lParam); [DllImport ("user32.dll")]        Private Static extern BOOLGetcomboboxinfo (INTPTR hwnd,refcomboboxinfo PCBI); [StructLayout (layoutkind.sequential)]Private structComboboxinfo { Public intcbsize;  PublicRECT RcItem;  PublicRECT Rcbutton;  PublicIntPtr Statebutton;  PublicIntPtr Hwndcombo;  PublicIntPtr Hwnditem;  PublicIntPtr hwndlist; } [StructLayout (layoutkind.sequential)]Private structRECT { Public intLeft ;  Public inttop;  Public intRight ;  Public intBottom; }        Private Const intEm_setcuebanner =0x1501; Private Const intEm_getcuebanner =0x1502;  Public Static voidSetcuetext (Control control,stringtext) {            if(Control isComboBox) {Comboboxinfo Info=Getcomboboxinfo (Control); SendMessage (Info.hwnditem, Em_setcuebanner,0, text); }            Else{SendMessage (control. Handle, Em_setcuebanner,0, text); }        }        Private Staticcomboboxinfo Getcomboboxinfo (Control control) {Comboboxinfo Info=NewComboboxinfo (); //A ComboBox is made up of three controls, a button, a list and textbox; //We want the textboxInfo.cbsize =marshal.sizeof (info); Getcomboboxinfo (Control. Handle,refinfo); returninfo; }         Public Static stringGetcuetext (Control control) {StringBuilder builder=NewStringBuilder (); if(Control isComboBox) {Comboboxinfo Info=NewComboboxinfo (); //A ComboBox is made up of both controls, a list and textbox; //We want the textboxInfo.cbsize =marshal.sizeof (info); Getcomboboxinfo (Control. Handle,refinfo); SendMessage (Info.hwnditem, Em_getcuebanner,0, builder); }            Else{SendMessage (control. Handle, Em_getcuebanner,0, builder); }            returnBuilder.        ToString (); }        //Setwatertext.setcuetext (TextBox1, "FDSADF")    }}

(2) Call can

Private void button1_click (object  sender, EventArgs e)        {            " please fill in User name " );             " please fill in the password " );        }

Using Win32API to realize textbox watermark effect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.