Winform 自定义文本框
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace TomWinform.CustomerControl
{
public partial class BorderTextBox : TextBox
{
//设置Rect消息
private const int EM_SETRECT = ;
//获取Rect消息
private const int EM_GETRECT = ;
//粘贴消息
private const int WM_PASTE = 0x0302; private Color borderColor = Color.Black;
private float leftBorderSize = ;
private float rightBorderSize = ;
private float topBorderSize = ;
private float bottomBorderSize = ;
private Padding textPadding = new Padding();
private bool allowReturn = false; [System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr GetWindowDC(IntPtr hWnd);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessageA")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessageA")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, ref Rectangle lParam); public BorderTextBox()
{
InitializeComponent();
} //画边框
private void DrawBorder(IntPtr hDC)
{
Graphics g = Graphics.FromHdc(hDC); #region 左边框
if (leftBorderSize > )
{
Pen penLeft = new Pen(borderColor, leftBorderSize);
Point[] pointLeft = new Point[];
pointLeft[] = new Point(, );
pointLeft[] = new Point(, this.Width - );
g.DrawLine(penLeft, pointLeft[], pointLeft[]);
}
#endregion #region 右边框
if (rightBorderSize > )
{
Pen penRight = new Pen(borderColor, rightBorderSize);
Point[] pointRight = new Point[];
pointRight[] = new Point(this.Width - , );
pointRight[] = new Point(this.Width - , this.Height - );
g.DrawLine(penRight, pointRight[], pointRight[]);
}
#endregion #region 上边框
if (topBorderSize > )
{
Pen penTop = new Pen(borderColor, topBorderSize);
Point[] pointTop = new Point[];
pointTop[] = new Point(, );
pointTop[] = new Point(this.Width - , );
g.DrawLine(penTop, pointTop[], pointTop[]);
}
#endregion #region 下边框
if (bottomBorderSize > )
{
Pen penBottom = new Pen(borderColor, bottomBorderSize);
Point[] pointBottom = new Point[];
pointBottom[] = new Point(, this.Height - );
pointBottom[] = new Point(this.Width - , this.Height - );
g.DrawLine(penBottom, pointBottom[], pointBottom[]);
}
#endregion
} public void SetTextDispLayout()
{
if (Text == "")
return;
//当允许多行和禁止会车时,Paddin有效
if (this.Multiline && (!this.WordWrap))
{
Rectangle rect = new Rectangle();
SendMessage(this.Handle, EM_GETRECT, (IntPtr), ref rect);
//SizeF size = CreateGraphics().MeasureString(Text, Font);
//rect.Y = (int)(Height - size.Height) / 2 + TextPadding.Top;
rect.Y = textPadding.Top;
rect.X = textPadding.Left;
rect.Height = Height;
rect.Width = Width - textPadding.Right - textPadding.Left;
SendMessage(this.Handle, EM_SETRECT, IntPtr.Zero, ref rect);
}
} protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
} protected override void WndProc(ref Message m)
{
//string str = "";
//bool flag = false;
//int i = 0;
//if (m.Msg == 0x0204)
// i++;
//if (!AllowReturn
// && m.Msg == WM_PASTE
// && System.Windows.Forms.Clipboard.ContainsText())
//{
// str = System.Windows.Forms.Clipboard.GetText();
// System.Windows.Forms.Clipboard.Clear();
// string nstr = str.Replace(char.ConvertFromUtf32((int)Keys.Return), "").Replace(char.ConvertFromUtf32((int)Keys.LineFeed), "");
// System.Windows.Forms.Clipboard.SetText(nstr);
// if (str.Length > 0) flag = true;
//} base.WndProc(ref m);
if (m.Msg == 0xf || m.Msg == 0x133)
{
IntPtr hDC = GetWindowDC(m.HWnd);
if (hDC.ToInt32() == )
return; DrawBorder(hDC); //返回结果
m.Result = IntPtr.Zero;
//释放
ReleaseDC(m.HWnd, hDC);
} //if (flag)
//{
// flag = false;
// System.Windows.Forms.Clipboard.SetText(str);
// str = "";
//}
} #region 属性
[Description("边框颜色"), Category("自定义属性")]
public Color BorderColor
{
get { return borderColor; }
set { borderColor = value; this.Invalidate(); }
}
[Description("左边框宽度"), Category("自定义属性")]
public float LeftBorderSize
{
get { return leftBorderSize; }
set { leftBorderSize = value; this.Invalidate(); }
}
[Description("右边框宽度"), Category("自定义属性")]
public float RightBorderSize
{
get { return rightBorderSize; }
set { rightBorderSize = value; this.Invalidate(); }
}
[Description("上边框宽度"), Category("自定义属性")]
public float TopBorderSize
{
get { return topBorderSize; }
set { topBorderSize = value; this.Invalidate(); }
}
[Description("下边框宽度"), Category("自定义属性")]
public float BottomBorderSize
{
get { return bottomBorderSize; }
set { bottomBorderSize = value; this.Invalidate(); }
}
[/*DisplayName("內邊距")*/Description("文本内边距,当允许多行和禁止回车时有效"), Category("自定义属性")]
public Padding TextPadding
{
get { return textPadding; }
set { textPadding = value; SetTextDispLayout(); }
}
[/*DisplayName("允許回車")*/Description("是否允许回车"), Category("自定义属性")]
public bool AllowReturn
{
get { return allowReturn; }
set { allowReturn = value;this.Invalidate(); }
}
#endregion #region 事件
protected override void OnKeyPress(KeyPressEventArgs e)
{
//如果不允许回车 屏蔽回车 换行键值
if (!AllowReturn
&& ((int)e.KeyChar == (int)Keys.Return || (int)e.KeyChar == (int)Keys.LineFeed))
{
e.Handled = true;
}
base.OnKeyPress(e);
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e); SetTextDispLayout();
}
#endregion }
}
Winform 自定义文本框的更多相关文章
- (十六)c#Winform自定义控件-文本框哪里去了?-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- Xamarin Android自定义文本框
xamarin android 自定义文本框简单的用法 关键点在于,监听EditText的内容变化,不同于java中文本内容变化去调用EditText.addTextChangedListener(m ...
- wxpython 支持python语法高亮的自定义文本框控件的代码
在研发闲暇时间,把开发过程中比较重要的一些代码做个珍藏,下面的代码内容是关于wxpython 支持python语法高亮的自定义文本框控件的代码,应该是对大家也有用. import keywordimp ...
- WPF 自定义文本框输入法 IME 跟随光标
本文告诉大家在 WPF 写一个自定义的文本框,如何实现让输入法跟随光标 本文非小白向,本文适合想开发自定义的文本框,从底层开始开发的文本库的伙伴.在开始之前,期望了解了文本库开发的基础知识 本文实现的 ...
- (三十)c#Winform自定义控件-文本框(三)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (三十一)c#Winform自定义控件-文本框(四)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- JavaScript 自定义文本框光标——初级版
文本框(input或textarea)的光标无法修改样式(除了通过color修改光标颜色).但笔者希望个人创建自己的网站时,文本框的光标有属于自己的风格.所以,尝试模拟文本框的光标,设计有自己风格的光 ...
- (二十八)c#Winform自定义控件-文本框(一)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- winform中文本框的一些案例
项目中经常看到在输入金额时,会加逗号,最近在复习正则表达式,就联系下,界面如下:
随机推荐
- MS SQL SERVER数据导入MySQL
1.sql server导出到xls,再导入到mysql中.亲测,单表数据量到百万以后,导出异常,可能由其它原因导致,没细纠.此种方式需要来回倒腾数据,稍繁琐. 2.采用kettle第三方的ETL工具 ...
- 9.22考试 crf的军训 题解
做这道题时由于第一道题太水了,第一反应是NOIP T2级别的题,需要拿上70~100的分,然后就开始分析,当然最后事实证明我错了…… 这道题当时首先联想到了 NOIP2016愤怒的小鸟 当然,数据范围 ...
- Asp.net之实现自定义跨域
跨域是指在浏览器的同源策略下导致前端和接口部署在不同域下导致无法直接访问的问题. 针对跨域有多种解决方案常见的有: JSNOP: 可参考Jquery实现,缺点是需要后端支持: Access-Con ...
- POJ2533&&SP1799 The Bottom of a Graph(tarjan+缩点)
POJ2553 SP1799 我们知道单独一个强连通分量中的所有点是满足题目要求的 但如果它连出去到了其他点那里,要么成为新的强连通分量,要么失去原有的符合题目要求的性质 所以只需tarjan缩点求出 ...
- 个人用户永久免费,可自动升级版Excel插件,使用VSTO开发,Excel催化剂安装过程详解及安装失败解决方法
因Excel催化剂用了VSTO的开发技术,并且为了最好的用户体验,用了Clickonce的布署方式(无需人工干预自动更新,让用户使用如浏览器访问网站一般,永远是最新的内容和功能).对安装过程有一定的难 ...
- [leetcode] 21. Merge Two Sorted Lists (Easy)
合并链表 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class ...
- web页面保存图片到本地
web页生成分享海报功能踩坑经验 https://blog.csdn.net/candy_home/article/details/78424642 https://www.jianshu.com/p ...
- APP爬虫(2)把小姐姐的图片down下来
APP爬虫(1)想学新语言,又没有动力,怎么办? 书接上文.使用appium在帖子列表界面模拟上划动作,捕捉不到列表的规律.上划结束后,列表只能获取到屏幕呈现的部分,而且下标还是从0开始的. 根据酸奶 ...
- 在Linux上安装JDK8-教程
xl_echo编辑整理.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.--这才是真正的堪称强大!! --- > 这里使用的服务 ...
- python课堂整理10---局部变量与全局变量
一.局部变量与全局变量 1. 没有缩进,顶头写的变量为全局变量 2. 在子程序里定义的变量为局部变量 3. 只有函数能把变量私有化 name = 'lhf' #全局变量 def change_name ...