Winform 自定义文本框

摘要:
使用系统;使用System.Collections.Generic;使用System.ComponentModel;使用System.Data;使用System.Drawing;使用System.Linq;使用System.Text;使用System.Threading.Tasks;使用System.Windows.Forms;名称
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 = 179;
        //获取Rect消息
        private const int EM_GETRECT = 178;
        //粘贴消息
        private const int WM_PASTE = 0x0302;

        private Color borderColor = Color.Black;
        private float leftBorderSize = 1;
        private float rightBorderSize = 1;
        private float topBorderSize = 1;
        private float bottomBorderSize = 1;
        private Padding textPadding = new Padding(1);
        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 > 0)
            {
                Pen penLeft = new Pen(borderColor, leftBorderSize);
                Point[] pointLeft = new Point[2];
                pointLeft[0] = new Point(0, 0);
                pointLeft[1] = new Point(0, this.Width - 1);
                g.DrawLine(penLeft, pointLeft[0], pointLeft[1]);
            }
            #endregion

            #region 右边框
            if (rightBorderSize > 0)
            {
                Pen penRight = new Pen(borderColor, rightBorderSize);
                Point[] pointRight = new Point[2];
                pointRight[0] = new Point(this.Width - 1, 0);
                pointRight[1] = new Point(this.Width - 1, this.Height - 1);
                g.DrawLine(penRight, pointRight[0], pointRight[1]);
            }
            #endregion

            #region 上边框
            if (topBorderSize > 0)
            {
                Pen penTop = new Pen(borderColor, topBorderSize);
                Point[] pointTop = new Point[2];
                pointTop[0] = new Point(0, 0);
                pointTop[1] = new Point(this.Width - 1, 0);
                g.DrawLine(penTop, pointTop[0], pointTop[1]);
            }
            #endregion

            #region 下边框
            if (bottomBorderSize > 0)
            {
                Pen penBottom = new Pen(borderColor, bottomBorderSize);
                Point[] pointBottom = new Point[2];
                pointBottom[0] = new Point(0, this.Height - 1);
                pointBottom[1] = new Point(this.Width - 1, this.Height - 1);
                g.DrawLine(penBottom, pointBottom[0], pointBottom[1]);
            }
            #endregion
        }

        public void SetTextDispLayout()
        {
            if (Text == "")
                return;
            //当允许多行和禁止会车时,Paddin有效
            if (this.Multiline && (!this.WordWrap))
            {
                Rectangle rect = new Rectangle();
                SendMessage(this.Handle, EM_GETRECT, (IntPtr)0, 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() == 0)
                    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 自定义文本框》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇WAMP环境搭建查看Mysql正在执行的事务、锁、等待下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

ios 视频列表处理---分解ZFPlayer

1.视频播放器添加到containerView的机制与一个普通播放器页面的不同 普通视频播放页面可以直接添加一个播放器,按照正常逻辑播放、暂停、切换等操作,而视频列表的做法是 用户触发播放动作 当点击一个cell上的播放按钮时,首先判断当前是否有其他cell在播放视频,有则停止播放并移除播放器, 反之,会判断是否存在有效的承载控件,即container...

Winform开发常用控件之DataGridView的简单数据绑定——代码绑定DataSet、DataTable、IList、SqlDataReader

前文介绍了Winform为DataGridView提供的数据自动绑定功能,下面介绍一下采用代码的数据绑定 1、用DataSet和DataTable为DataGridView提供数据源 先上代码 private void Form1_Load(objectsender, EventArgs e) { String st...

Winform开发框架之对话框样式同化

早在《Winform分页控件重大更新,并实现普通版、DotNetBar、DevExpress三大版本整合更新(附各种例子源码)》随笔中,就集成了三种界面样式的分页控件(普通版、基于DotNetBar样式和基于DevExpress样式三种),后来也就形成了我三种不同的Winform开发框架界面样式。 1)基于传统经典模式的界面,采用了OutLookBar工具...

iphone上如何绘制柱状图(转载,整理)

曾经在cocoachina上看到过绘制的立体的柱状图,效果非常不错,下面是链接, http://www.cocoachina.com/bbs/read.php?tid-9462-toread-1.html NTChartView.h #import <Foundation/Foundation.h> @interface NTChartV...

C# WinForm下DataGridView绘制背景图

昨天一个朋友突然问我如何在C#下给DataGridView绘制背景图,以前使用一些第三方控件时,看见它们有这个功能,只是我还没有过这种需求,于是便动手试了下。 最先想到的是BackgroundImage,这两天正在做B/S的界面,还觉得要说做界面方便,还得说CSS,从这点上来说,WPF或者Silverlight还真不赖,只可惜的是现在C/S的用武之地越来越...

winform DataGridView控件的打印

借用网上的代码,应该是完整的打印过程了。有打印,预览,设置三个按钮 代码: public partial class Example : Form { //打印文檔 PrintDocument pdDocument = new PrintDocument(); //打印格式設置頁面 PageSetupDialog dlgPageSetup = new Pa...