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