原理:ide
1、在控件的後臺代碼中, 添加布爾類型的屬性CanFocus函數
2、在控件的構造函數中, 註冊Enter事件的處理方法. 並在處理方法中,根據CanFocus屬性的值來決定是否能夠丟失焦點, 若是能夠則調用Windows消息的發送類.字體
3、在處理方法中,調用User32.dll類庫, 發送window消息.this
示例代碼:spa
//Windows消息的發送方法3d
//WMMessageHepler.cscode
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Windows.Forms; namespace YXT.Common { public class WMMessageHepler { private const int WM_KILLFOCUS = 0x0008; //發送失去焦點的Window消息 public static void SendBlurMsg(IntPtr hWnd) { PostMessage(hWnd, WM_KILLFOCUS, 0, 0); } [DllImport("user32.dll")] private static extern void PostMessage(IntPtr hWnd, int msg, int wParam, int lParam); [DllImport("User32.dll", EntryPoint = "SendMessage")] private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam); } }
//自定義控件的關鍵代碼orm
//WatermarkTextBox.csblog
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using YXT.Common; namespace YXT.Client.Controls { [ToolboxBitmap(typeof(TextBox))] public class WatermarkTextBox : TextBox { public WatermarkTextBox() { this.BorderStyle = BorderStyle.FixedSingle; this.Enter += new EventHandler(WatermarkTextBox_Enter); } void WatermarkTextBox_Enter(object sender, EventArgs e) { if (this.SetBlur) { this.OnBlur(); } } private Color _borderColor = Color.Red; private string _watermarkTitle; private Color _watermarkColor = Color.DarkGray; private bool _setBlur = false; /// <summary> /// 控件是否失去焦點 /// </summary> public bool SetBlur { get { return this._setBlur; } set { this._setBlur = value; Invalidate(); } } /// <summary> /// 水印字體提示 /// </summary> public string WatermarkTitle { get { return _watermarkTitle; } set { _watermarkTitle = value; Invalidate(); } } /// <summary> /// 邊框顏色 /// </summary> public Color BorderColor { get { return _borderColor; } set { _borderColor = value; Invalidate(); } } /// <summary> /// 水印顏色 /// </summary> public Color WatermarkColor { get { return _watermarkColor; } set { _watermarkColor = value; Invalidate(); } } private void OnBlur() { WMMessageHepler.SendBlurMsg(this.Handle); } protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == 0xf || m.Msg == 0x14 || m.Msg == 0x85) { if (this.BorderStyle == BorderStyle.FixedSingle) { using (Graphics g = Graphics.FromHwnd(this.Handle)) { using (var pen = new Pen(_borderColor)) { g.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1); WmPaint(ref m); } } } } } private void WmPaint(ref Message m) { using (Graphics graphics = Graphics.FromHwnd(base.Handle)) { if (Text.Length == 0 && !string.IsNullOrEmpty(_watermarkTitle) && !Focused) { TextFormatFlags format = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter; if (RightToLeft == RightToLeft.Yes) { format |= TextFormatFlags.RightToLeft | TextFormatFlags.Right; } TextRenderer.DrawText( graphics, _watermarkTitle, Font, base.ClientRectangle, _watermarkColor, format); } } } } }
//使用示例事件
//方法使用示例
ControlsEdintor(string controlsName) { //控件編輯時 修改樣式 foreach (var value in this.Controls) { var textBox = value as WatermarkTextBox; if (controlsName == "編輯") { if (textBox != null) { textBox.BorderStyle = BorderStyle.FixedSingle; textBox.ReadOnly = false; textBox.SetBlur = false; } } else { if (textBox != null) { textBox.BorderStyle = BorderStyle.None; textBox.ReadOnly = true; textBox.SetBlur = true; } } } }