1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace TomWinform.CustomerControl
  12. {
  13. public partial class BorderTextBox : TextBox
  14. {
  15. //设置Rect消息
  16. private const int EM_SETRECT = ;
  17. //获取Rect消息
  18. private const int EM_GETRECT = ;
  19. //粘贴消息
  20. private const int WM_PASTE = 0x0302;
  21.  
  22. private Color borderColor = Color.Black;
  23. private float leftBorderSize = ;
  24. private float rightBorderSize = ;
  25. private float topBorderSize = ;
  26. private float bottomBorderSize = ;
  27. private Padding textPadding = new Padding();
  28. private bool allowReturn = false;
  29.  
  30. [System.Runtime.InteropServices.DllImport("user32.dll")]
  31. static extern IntPtr GetWindowDC(IntPtr hWnd);
  32. [System.Runtime.InteropServices.DllImport("user32.dll")]
  33. static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
  34. [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessageA")]
  35. private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);
  36. [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessageA")]
  37. private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, ref Rectangle lParam);
  38.  
  39. public BorderTextBox()
  40. {
  41. InitializeComponent();
  42. }
  43.  
  44. //画边框
  45. private void DrawBorder(IntPtr hDC)
  46. {
  47. Graphics g = Graphics.FromHdc(hDC);
  48.  
  49. #region 左边框
  50. if (leftBorderSize > )
  51. {
  52. Pen penLeft = new Pen(borderColor, leftBorderSize);
  53. Point[] pointLeft = new Point[];
  54. pointLeft[] = new Point(, );
  55. pointLeft[] = new Point(, this.Width - );
  56. g.DrawLine(penLeft, pointLeft[], pointLeft[]);
  57. }
  58. #endregion
  59.  
  60. #region 右边框
  61. if (rightBorderSize > )
  62. {
  63. Pen penRight = new Pen(borderColor, rightBorderSize);
  64. Point[] pointRight = new Point[];
  65. pointRight[] = new Point(this.Width - , );
  66. pointRight[] = new Point(this.Width - , this.Height - );
  67. g.DrawLine(penRight, pointRight[], pointRight[]);
  68. }
  69. #endregion
  70.  
  71. #region 上边框
  72. if (topBorderSize > )
  73. {
  74. Pen penTop = new Pen(borderColor, topBorderSize);
  75. Point[] pointTop = new Point[];
  76. pointTop[] = new Point(, );
  77. pointTop[] = new Point(this.Width - , );
  78. g.DrawLine(penTop, pointTop[], pointTop[]);
  79. }
  80. #endregion
  81.  
  82. #region 下边框
  83. if (bottomBorderSize > )
  84. {
  85. Pen penBottom = new Pen(borderColor, bottomBorderSize);
  86. Point[] pointBottom = new Point[];
  87. pointBottom[] = new Point(, this.Height - );
  88. pointBottom[] = new Point(this.Width - , this.Height - );
  89. g.DrawLine(penBottom, pointBottom[], pointBottom[]);
  90. }
  91. #endregion
  92. }
  93.  
  94. public void SetTextDispLayout()
  95. {
  96. if (Text == "")
  97. return;
  98. //当允许多行和禁止会车时,Paddin有效
  99. if (this.Multiline && (!this.WordWrap))
  100. {
  101. Rectangle rect = new Rectangle();
  102. SendMessage(this.Handle, EM_GETRECT, (IntPtr), ref rect);
  103. //SizeF size = CreateGraphics().MeasureString(Text, Font);
  104. //rect.Y = (int)(Height - size.Height) / 2 + TextPadding.Top;
  105. rect.Y = textPadding.Top;
  106. rect.X = textPadding.Left;
  107. rect.Height = Height;
  108. rect.Width = Width - textPadding.Right - textPadding.Left;
  109. SendMessage(this.Handle, EM_SETRECT, IntPtr.Zero, ref rect);
  110. }
  111. }
  112.  
  113. protected override void OnPaint(PaintEventArgs pe)
  114. {
  115. base.OnPaint(pe);
  116. }
  117.  
  118. protected override void WndProc(ref Message m)
  119. {
  120. //string str = "";
  121. //bool flag = false;
  122. //int i = 0;
  123. //if (m.Msg == 0x0204)
  124. // i++;
  125. //if (!AllowReturn
  126. // && m.Msg == WM_PASTE
  127. // && System.Windows.Forms.Clipboard.ContainsText())
  128. //{
  129. // str = System.Windows.Forms.Clipboard.GetText();
  130. // System.Windows.Forms.Clipboard.Clear();
  131. // string nstr = str.Replace(char.ConvertFromUtf32((int)Keys.Return), "").Replace(char.ConvertFromUtf32((int)Keys.LineFeed), "");
  132. // System.Windows.Forms.Clipboard.SetText(nstr);
  133. // if (str.Length > 0) flag = true;
  134. //}
  135.  
  136. base.WndProc(ref m);
  137. if (m.Msg == 0xf || m.Msg == 0x133)
  138. {
  139. IntPtr hDC = GetWindowDC(m.HWnd);
  140. if (hDC.ToInt32() == )
  141. return;
  142.  
  143. DrawBorder(hDC);
  144.  
  145. //返回结果
  146. m.Result = IntPtr.Zero;
  147. //释放
  148. ReleaseDC(m.HWnd, hDC);
  149. }
  150.  
  151. //if (flag)
  152. //{
  153. // flag = false;
  154. // System.Windows.Forms.Clipboard.SetText(str);
  155. // str = "";
  156. //}
  157. }
  158.  
  159. #region 属性
  160. [Description("边框颜色"), Category("自定义属性")]
  161. public Color BorderColor
  162. {
  163. get { return borderColor; }
  164. set { borderColor = value; this.Invalidate(); }
  165. }
  166. [Description("左边框宽度"), Category("自定义属性")]
  167. public float LeftBorderSize
  168. {
  169. get { return leftBorderSize; }
  170. set { leftBorderSize = value; this.Invalidate(); }
  171. }
  172. [Description("右边框宽度"), Category("自定义属性")]
  173. public float RightBorderSize
  174. {
  175. get { return rightBorderSize; }
  176. set { rightBorderSize = value; this.Invalidate(); }
  177. }
  178. [Description("上边框宽度"), Category("自定义属性")]
  179. public float TopBorderSize
  180. {
  181. get { return topBorderSize; }
  182. set { topBorderSize = value; this.Invalidate(); }
  183. }
  184. [Description("下边框宽度"), Category("自定义属性")]
  185. public float BottomBorderSize
  186. {
  187. get { return bottomBorderSize; }
  188. set { bottomBorderSize = value; this.Invalidate(); }
  189. }
  190. [/*DisplayName("內邊距")*/Description("文本内边距,当允许多行和禁止回车时有效"), Category("自定义属性")]
  191. public Padding TextPadding
  192. {
  193. get { return textPadding; }
  194. set { textPadding = value; SetTextDispLayout(); }
  195. }
  196. [/*DisplayName("允許回車")*/Description("是否允许回车"), Category("自定义属性")]
  197. public bool AllowReturn
  198. {
  199. get { return allowReturn; }
  200. set { allowReturn = value;this.Invalidate(); }
  201. }
  202. #endregion
  203.  
  204. #region 事件
  205. protected override void OnKeyPress(KeyPressEventArgs e)
  206. {
  207. //如果不允许回车 屏蔽回车 换行键值
  208. if (!AllowReturn
  209. && ((int)e.KeyChar == (int)Keys.Return || (int)e.KeyChar == (int)Keys.LineFeed))
  210. {
  211. e.Handled = true;
  212. }
  213. base.OnKeyPress(e);
  214. }
  215. protected override void OnTextChanged(EventArgs e)
  216. {
  217. base.OnTextChanged(e);
  218.  
  219. SetTextDispLayout();
  220. }
  221. #endregion
  222.  
  223. }
  224. }

Winform 自定义文本框的更多相关文章

  1. (十六)c#Winform自定义控件-文本框哪里去了?-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  2. Xamarin Android自定义文本框

    xamarin android 自定义文本框简单的用法 关键点在于,监听EditText的内容变化,不同于java中文本内容变化去调用EditText.addTextChangedListener(m ...

  3. wxpython 支持python语法高亮的自定义文本框控件的代码

    在研发闲暇时间,把开发过程中比较重要的一些代码做个珍藏,下面的代码内容是关于wxpython 支持python语法高亮的自定义文本框控件的代码,应该是对大家也有用. import keywordimp ...

  4. WPF 自定义文本框输入法 IME 跟随光标

    本文告诉大家在 WPF 写一个自定义的文本框,如何实现让输入法跟随光标 本文非小白向,本文适合想开发自定义的文本框,从底层开始开发的文本库的伙伴.在开始之前,期望了解了文本库开发的基础知识 本文实现的 ...

  5. (三十)c#Winform自定义控件-文本框(三)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  6. (三十一)c#Winform自定义控件-文本框(四)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  7. JavaScript 自定义文本框光标——初级版

    文本框(input或textarea)的光标无法修改样式(除了通过color修改光标颜色).但笔者希望个人创建自己的网站时,文本框的光标有属于自己的风格.所以,尝试模拟文本框的光标,设计有自己风格的光 ...

  8. (二十八)c#Winform自定义控件-文本框(一)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  9. winform中文本框的一些案例

    项目中经常看到在输入金额时,会加逗号,最近在复习正则表达式,就联系下,界面如下:

随机推荐

  1. vue-cli · Failed to download repo vuejs-templates/webpack: self signed certificate in certificate chain

    vue init webpack <Project name> 报错: vue-cli · Failed to download repo vuejs-templates/webpack: ...

  2. STM32F0_HAL库驱动描述——基于F1的USART串口IT中断实现解析

    从原子F103 HAL库基础串口例程来看HAL程序结构: 从main函数开始,首先是HAL库两个函数的初始化: HAL_Init(): Stm32_Clock_Init(RCC_PLL_MUL9); ...

  3. DataNode的工作机制

    DataNode的工作机制 一个数据块在DataNode以文件的形式在磁盘上保存,分为两个文件,一个是数据本身, 一个是元数据信息(包括数据的长度,校验和,时间戳) 1.DataNode启动后,向Na ...

  4. Java IO部分面试题

    1.什么是比特(Bit),什么是字节(Byte),什么是字符(Char),它们长度是多少,各有什么区别 1. Bit最小的二进制单位 ,是计算机的操作部分 取值0或者1 2. Byte是计算机操作数据 ...

  5. php 排序和置顶功能实现

    (1)排序操作思路 一般来说都是按照发布时间排序.时间戳大的靠前,所以用倒序desc,而不是asc (2)置顶操作思路: 点击置顶时,修改数据库addtime字段值为当前时间即可.因为排序是按照时间戳 ...

  6. python课堂整理16---内置函数

    1. abs :求绝对值 print(abs(-1)) 2. all()传入一个可迭代对象,对该对象进行bool值运算,若都为True 就返回True,有一个为假,就返回False print(all ...

  7. zabbix3.4汉化

    1.管理员用户登入zabbix页面,更改语言为Chinese(zh_CN),点击Update 2.解决zabbix页面中文乱码 2.1在windows的C:\Windows\Fonts找到字体文件si ...

  8. Python装饰器 (转)

    多个装饰器执行的顺序就是从最后一个装饰器开始,执行到第一个装饰器,再执行函数本身. #多个装饰器 import time def deco01(func): def wrapper(*args, ** ...

  9. 【MySQL】java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\xB3' for column

    问题原因: 输入内容包含特殊字符,MySQL 中的列不支持. 解决方法 多数是修改 MySQL 的数据库和表结构,CHARSET 改为 utf8mb4,但本人测试还是不能传入 emoji. 后来在代码 ...

  10. spring boot 学习笔记之前言----环境搭建(如何用Eclipse配置Maven和Spring Boot)

    本篇文档来源:https://blog.csdn.net/a565649077/article/details/81042742 1.1 Eclipse准备 (1)     服务器上安装JDK和Mav ...