前提 入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。 開源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control 如果覺得寫的還行,請點個 star 支持一下吧 歡迎前來交流探討: 企鵝群568015492 目錄 ...
前提
入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。
開源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
如果覺得寫的還行,請點個 star 支持一下吧
目錄
https://www.cnblogs.com/bfyx/p/11364884.html
準備工作
終於到文本框了,文本框將包含原文本框擴展,透明文本框,數字輸入文本框,帶邊框文本框
本文將講解帶邊框文本框,可選彈出鍵盤樣式,繼承自控制項基類UCControlBase
同時用到了無焦點窗體和鍵盤,如果你還沒有瞭解,請前往查看
開始
添加用戶控制項,命名UCTextBoxEx,繼承自UCControlBase
屬性
1 private bool m_isShowClearBtn = true; 2 int m_intSelectionStart = 0; 3 int m_intSelectionLength = 0; 4 /// <summary> 5 /// 功能描述:是否顯示清理按鈕 6 /// 作 者:HZH 7 /// 創建日期:2019-02-28 16:13:52 8 /// </summary> 9 [Description("是否顯示清理按鈕"), Category("自定義")] 10 public bool IsShowClearBtn 11 { 12 get { return m_isShowClearBtn; } 13 set 14 { 15 m_isShowClearBtn = value; 16 if (value) 17 { 18 btnClear.Visible = !(txtInput.Text == "\r\n") && !string.IsNullOrEmpty(txtInput.Text); 19 } 20 else 21 { 22 btnClear.Visible = false; 23 } 24 } 25 } 26 27 private bool m_isShowSearchBtn = false; 28 /// <summary> 29 /// 是否顯示查詢按鈕 30 /// </summary> 31 32 [Description("是否顯示查詢按鈕"), Category("自定義")] 33 public bool IsShowSearchBtn 34 { 35 get { return m_isShowSearchBtn; } 36 set 37 { 38 m_isShowSearchBtn = value; 39 btnSearch.Visible = value; 40 } 41 } 42 43 [Description("是否顯示鍵盤"), Category("自定義")] 44 public bool IsShowKeyboard 45 { 46 get 47 { 48 return btnKeybord.Visible; 49 } 50 set 51 { 52 btnKeybord.Visible = value; 53 } 54 } 55 [Description("字體"), Category("自定義")] 56 public new Font Font 57 { 58 get 59 { 60 return this.txtInput.Font; 61 } 62 set 63 { 64 this.txtInput.Font = value; 65 } 66 } 67 68 [Description("輸入類型"), Category("自定義")] 69 public TextInputType InputType 70 { 71 get { return txtInput.InputType; } 72 set { txtInput.InputType = value; } 73 } 74 75 /// <summary> 76 /// 水印文字 77 /// </summary> 78 [Description("水印文字"), Category("自定義")] 79 public string PromptText 80 { 81 get 82 { 83 return this.txtInput.PromptText; 84 } 85 set 86 { 87 this.txtInput.PromptText = value; 88 } 89 } 90 91 [Description("水印字體"), Category("自定義")] 92 public Font PromptFont 93 { 94 get 95 { 96 return this.txtInput.PromptFont; 97 } 98 set 99 { 100 this.txtInput.PromptFont = value; 101 } 102 } 103 104 [Description("水印顏色"), Category("自定義")] 105 public Color PromptColor 106 { 107 get 108 { 109 return this.txtInput.PromptColor; 110 } 111 set 112 { 113 this.txtInput.PromptColor = value; 114 } 115 } 116 117 /// <summary> 118 /// 獲取或設置一個值,該值指示當輸入類型InputType=Regex時,使用的正則表達式。 119 /// </summary> 120 [Description("獲取或設置一個值,該值指示當輸入類型InputType=Regex時,使用的正則表達式。")] 121 public string RegexPattern 122 { 123 get 124 { 125 return this.txtInput.RegexPattern; 126 } 127 set 128 { 129 this.txtInput.RegexPattern = value; 130 } 131 } 132 /// <summary> 133 /// 當InputType為數字類型時,能輸入的最大值 134 /// </summary> 135 [Description("當InputType為數字類型時,能輸入的最大值。")] 136 public decimal MaxValue 137 { 138 get 139 { 140 return this.txtInput.MaxValue; 141 } 142 set 143 { 144 this.txtInput.MaxValue = value; 145 } 146 } 147 /// <summary> 148 /// 當InputType為數字類型時,能輸入的最小值 149 /// </summary> 150 [Description("當InputType為數字類型時,能輸入的最小值。")] 151 public decimal MinValue 152 { 153 get 154 { 155 return this.txtInput.MinValue; 156 } 157 set 158 { 159 this.txtInput.MinValue = value; 160 } 161 } 162 /// <summary> 163 /// 當InputType為數字類型時,能輸入的最小值 164 /// </summary> 165 [Description("當InputType為數字類型時,小數位數。")] 166 public int DecLength 167 { 168 get 169 { 170 return this.txtInput.DecLength; 171 } 172 set 173 { 174 this.txtInput.DecLength = value; 175 } 176 } 177 178 private KeyBoardType keyBoardType = KeyBoardType.UCKeyBorderAll_EN; 179 [Description("鍵盤打開樣式"), Category("自定義")] 180 public KeyBoardType KeyBoardType 181 { 182 get { return keyBoardType; } 183 set { keyBoardType = value; } 184 } 185 [Description("查詢按鈕點擊事件"), Category("自定義")] 186 public event EventHandler SearchClick; 187 188 [Description("文本改變事件"), Category("自定義")] 189 public new event EventHandler TextChanged; 190 [Description("鍵盤按鈕點擊事件"), Category("自定義")] 191 public event EventHandler KeyboardClick; 192 193 [Description("文本"), Category("自定義")] 194 public string InputText 195 { 196 get 197 { 198 return txtInput.Text; 199 } 200 set 201 { 202 txtInput.Text = value; 203 } 204 } 205 206 private bool isFocusColor = true; 207 [Description("獲取焦點是否變色"), Category("自定義")] 208 public bool IsFocusColor 209 { 210 get { return isFocusColor; } 211 set { isFocusColor = value; } 212 } 213 private Color _FillColor; 214 public new Color FillColor 215 { 216 get 217 { 218 return _FillColor; 219 } 220 set 221 { 222 _FillColor = value; 223 base.FillColor = value; 224 this.txtInput.BackColor = value; 225 } 226 }
一些事件
1 void UCTextBoxEx_SizeChanged(object sender, EventArgs e) 2 { 3 this.txtInput.Location = new Point(this.txtInput.Location.X, (this.Height - txtInput.Height) / 2); 4 } 5 6 7 private void txtInput_TextChanged(object sender, EventArgs e) 8 { 9 if (m_isShowClearBtn) 10 { 11 btnClear.Visible = !(txtInput.Text == "\r\n") && !string.IsNullOrEmpty(txtInput.Text); 12 } 13 if (TextChanged != null) 14 { 15 TextChanged(sender, e); 16 } 17 } 18 19 private void btnClear_MouseDown(object sender, MouseEventArgs e) 20 { 21 txtInput.Clear(); 22 txtInput.Focus(); 23 } 24 25 private void btnSearch_MouseDown(object sender, MouseEventArgs e) 26 { 27 if (SearchClick != null) 28 { 29 SearchClick(sender, e); 30 } 31 } 32 Forms.FrmAnchor m_frmAnchor; 33 private void btnKeybord_MouseDown(object sender, MouseEventArgs e) 34 { 35 m_intSelectionStart = this.txtInput.SelectionStart; 36 m_intSelectionLength = this.txtInput.SelectionLength; 37 this.FindForm().ActiveControl = this; 38 this.FindForm().ActiveControl = this.txtInput; 39 switch (keyBoardType) 40 { 41 case KeyBoardType.UCKeyBorderAll_EN: 42 if (m_frmAnchor == null) 43 { 44 if (m_frmAnchor == null) 45 { 46 UCKeyBorderAll key = new UCKeyBorderAll(); 47 key.CharType = KeyBorderCharType.CHAR; 48 key.RetractClike += (a, b) => 49 { 50 m_frmAnchor.Hide(); 51 }; 52 m_frmAnchor = new Forms.FrmAnchor(this, key); 53 m_frmAnchor.VisibleChanged += (a, b) => 54 { 55 if (m_frmAnchor.Visible) 56 { 57 this.txtInput.SelectionStart = m_intSelectionStart; 58 this.txtInput.SelectionLength = m_intSelectionLength; 59 } 60 }; 61 } 62 } 63 break; 64 case KeyBoardType.UCKeyBorderAll_Num: 65 66 if (m_frmAnchor == null) 67 { 68 UCKeyBorderAll key = new UCKeyBorderAll(); 69 key.CharType = KeyBorderCharType.NUMBER; 70 key.RetractClike += (a, b) => 71 { 72 m_frmAnchor.Hide(); 73 }; 74 m_frmAnchor = new Forms.FrmAnchor(this, key); 75 m_frmAnchor.VisibleChanged += (a, b) => 76 { 77 if (m_frmAnchor.Visible) 78 { 79 this.txtInput.SelectionStart = m_intSelectionStart; 80 this.txtInput.SelectionLength = m_intSelectionLength; 81 } 82 }; 83 } 84 85 break; 86 case KeyBoardType.UCKeyBorderNum: 87 if (m_frmAnchor == null) 88 { 89 UCKeyBorderNum key = new UCKeyBorderNum(); 90 m_frmAnchor = new Forms.FrmAnchor(this, key); 91 m_frmAnchor.VisibleChanged += (a, b) => 92 { 93 if (m_frmAnchor.Visible) 94 { 95 this.txtInput.SelectionStart = m_intSelectionStart; 96 this.txtInput.SelectionLength = m_intSelectionLength; 97 } 98 }; 99 } 100 break; 101 case HZH_Controls.Controls.KeyBoardType.UCKeyBorderHand: 102 103 m_frmAnchor = new Forms.FrmAnchor(this, new Size(504, 361)); 104 m_frmAnchor.VisibleChanged += m_frmAnchor_VisibleChanged; 105 m_frmAnchor.Disposed += m_frmAnchor_Disposed; 106 Panel p = new Panel(); 107 p.Dock = DockStyle.Fill; 108 p.Name = "keyborder"; 109 m_frmAnchor.Controls.Add(p); 110 111 UCBtnExt btnDelete = new UCBtnExt(); 112 btnDelete.Name = "btnDelete"; 113 btnDelete.Size = new Size(80, 28); 114 btnDelete.FillColor = Color.White; 115 btnDelete.IsRadius = false; 116 btnDelete.ConerRadius = 1; 117 btnDelete.IsShowRect = true; 118 btnDelete.RectColor = Color.FromArgb(189, 197, 203); 119 btnDelete.Location = new Point(198, 332); 120 btnDelete.BtnFont = new System.Drawing.Font("微軟雅黑", 8); 121 btnDelete.BtnText = "刪除"; 122 btnDelete.BtnClick += (a, b) => 123 { 124 SendKeys.Send("{BACKSPACE}"); 125 }; 126 m_frmAnchor.Controls.Add(btnDelete); 127 btnDelete.BringToFront(); 128 129 UCBtnExt btnEnter = new UCBtnExt(); 130 btnEnter.Name = "btnEnter"; 131 btnEnter.Size = new Size(82, 28); 132 btnEnter.FillColor = Color.White; 133 btnEnter.IsRadius = false; 134 btnEnter.ConerRadius = 1; 135 btnEnter.IsShowRect = true; 136 btnEnter.RectColor = Color.FromArgb(189, 197, 203); 137 btnEnter.Location = new Point(278, 332); 138 btnEnter.BtnFont = new System.Drawing.Font("微軟雅黑", 8); 139 btnEnter.BtnText = "確定"; 140 btnEnter.BtnClick += (a, b) => 141 { 142 SendKeys.Send("{ENTER}"); 143 m_frmAnchor.Hide(); 144 }; 145 m_frmAnchor.Controls.Add(btnEnter); 146 btnEnter.BringToFront(); 147 m_frmAnchor.VisibleChanged += (a, b) => 148 { 149 if (m_frmAnchor.Visible) 150 { 151 this.txtInput.SelectionStart = m_intSelectionStart; 152 this.txtInput.SelectionLength = m_intSelectionLength; 153 } 154 }; 155 break; 156 } 157 if (!m_frmAnchor.Visible) 158 m_frmAnchor.Show(this.FindForm()); 159 if (KeyboardClick != null) 160 { 161 KeyboardClick(sender, e); 162 } 163 } 164 165 void m_frmAnchor_Disposed(object sender, EventArgs e) 166 { 167 if (m_HandAppWin != IntPtr.Zero) 168 { 169 if (m_HandPWin != null && !m_HandPWin.HasExited) 170 m_HandPWin.Kill(); 171 m_HandPWin = null; 172 m_HandAppWin = IntPtr.Zero; 173 } 174 } 175 176 177 IntPtr m_HandAppWin; 178 Process m_HandPWin = null; 179 string m_HandExeName = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "HandInput\\handinput.exe"); 180 181 void m_frmAnchor_VisibleChanged(object sender, EventArgs e) 182 { 183 if (m_frmAnchor.Visible) 184 { 185 var lstP = Process.GetProcessesByName("handinput"); 186 if (lstP.Length > 0) 187 { 188 foreach (var item in lstP) 189 { 190 item.Kill(); 191 } 192 } 193 m_HandAppWin = IntPtr.Zero; 194 195 if (m_HandPWin == null) 196 { 197 m_HandPWin = null; 198 199 m_HandPWin = System.Diagnostics.Process.Start(this.m_HandExeName); 200 m_HandPWin.WaitForInputIdle(); 201 } 202 while (m_HandPWin.MainWindowHandle == IntPtr.Zero) 203 { 204 Thread.Sleep(10); 205 } 206 m_HandAppWin = m_HandPWin.MainWindowHandle; 207 Control p = m_frmAnchor.Controls.Find("keyborder", false)[0]; 208 SetParent(m_HandAppWin, p.Handle); 209 ControlHelper.SetForegroundWindow(this.FindForm().Handle); 210 MoveWindow(m_HandAppWin, -111, -41, 626, 412, true); 211 } 212 else 213 { 214 if (m_HandAppWin != IntPtr.Zero) 215 { 216 if (m_HandPWin != null