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 自定义文本框的更多相关文章

  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. maven多仓库配置(公司服务器与阿里云仓库)

    1. 问题描述 公司内网搭建的有maven私有服务器,但是碰到好几次只有gav没有jar的情况或者最新版本更新不及时,所以需要公司服务器和远程仓库(阿里云)相结合来满足项目需求. 2. 解决方案: m ...

  2. [Noi2002]Savage 题解

    [Noi2002]Savage 时间限制: 5 Sec  内存限制: 64 MB 题目描述 输入 第1行为一个整数N(1<=N<=15),即野人的数目. 第2行到第N+1每行为三个整数Ci ...

  3. Android开发-实现第三方APP跳转

    自己创建一个按钮: <Button android:id="@+id/btn_button" android:layout_width="fill_parent&q ...

  4. android布局几点随想

    1. 正式布局界面时,先在纸上画出整个布局,并考虑用什么布局比较适合: 2. 布局界面先做出框架,并用不同的背景颜色标记出来,确保大的布局框架式正确的: 3. 接着在每个大的布局框架内布局小的布局: ...

  5. import tensorflow 报错

    >>> import tensorflowe:\ProgramData\Anaconda3\lib\site-packages\h5py\__init__.py:36: Future ...

  6. ubuntu16.04服务器上无root权限,配置个人tensorflow环境--cuda9.0+cuDNN7+tensorflow-gpu-1.8

    本人在服务器上已经用Anconda创建好python3.5的环境,这个网上有一大堆教程.接下来是重点. 1. cuda的安装 https://developer.nvidia.com/cuda-dow ...

  7. 利用TCP协议,实现基于Socket的小聊天程序(初级版)

    TCP TCP (Transmission Control Protocol)属于传输层协议.其中TCP提供IP环境下的数据可靠传输,它提供的服务包括数据流传送.可靠性.有效流控.全双工操作和多路复用 ...

  8. python面向对象的继承-组合-02

    *面向对象(OOP)的三大特征:**# 封装.继承.多态 继承 什么是继承 继承:# 是一种关系,描述两个对象之间什么是什么的什么的关系 例如:麦兜.佩奇.猪猪侠.猪刚鬣,都是猪 为什么要使用继承 继 ...

  9. 50 行 Python 代码,带你追到女神

    今天来给大家分享一个撩妹技巧,利用 python 每天给你最心爱的人,发送微信消息,说声晚安. 废话不多说,源代码奉上 def get_news(): ... url = "http://o ...

  10. mysql协议分析2---认证包

    主人看到navicat和mysql在那嘻嘻哈哈,眉来眼去的,好不快乐,忽然也想自己写个程序,直接去访问Mysql,虽然现在已经有很多现成的中间件可以直接拿来用了,程序只要负责写sql语句就行了,但是主 ...