C#&Winform中textBox提示文字的实现

文章目录

      • 先创建一个类
      • 具体实现

先创建一个类

首先在你的项目中新建一个类,写入如下代码:

	//自行补齐命名空间
	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(); 
        }
    }

具体实现

当你存在上面的那个类后,实现textBox提示文字就非常简单了
使用方法:

  • 在窗体加载时直接调用该类下的方法即可

            Win32Utility.SetCueText(this.txtUserId, "  请输入用户名");
            Win32Utility.SetCueText(this.txtUserPwd, "  请输入密码");

你可能感兴趣的:(Winform)