C# TextBox 文本居中 可設置Margin

原生的TextBox不支持文本的垂直居中及Padding功能,今天晚上抽時間搞了一下,此代碼主要爲增長這些功能。與你們共享。ide

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;函數

namespace TextBoxEx
{
    public partial class TextBoxEx : TextBox
    {
        //設置Rect消息
        private const int EM_SETRECT = 179;
        //獲取Rect消息
        private const int EM_GETRECT = 178;
        //private const int WM_GETTEXT = 0x000d;
        //private const int WM_COPY = 0x0301;
        //粘貼消息
        private const int WM_PASTE = 0x0302;
        //繪製消息
        private const int WM_PAINT = 0xF;
        //控件顏色編輯消息
        private const int WM_CTLCOLOREDIT = 0x0133;
        //private const int WM_CONTEXTMENU = 0x007B;
        //private const int WM_RBUTTONDOWN = 0x0204;佈局

        public TextBoxEx()
        {
            InitializeComponent();
            //this.SetStyle(ControlStyles.UserPaint
            //    | ControlStyles.DoubleBuffer
            //    | ControlStyles.ResizeRedraw  //調整大小時重繪
            //    | ControlStyles.AllPaintingInWmPaint // 禁止擦除背景.
            //    | ControlStyles.OptimizedDoubleBuffer // 雙緩衝
            //    | ControlStyles.SupportsTransparentBackColor //透明效果
            //    , true);
            //多行顯示 只有多行顯示才能設置Rect有效
            this.Multiline = true;
            //不容許回國
            AllowReturn = false;
            //關閉默認的邊框
            BorderStyle = System.Windows.Forms.BorderStyle.None;
            //禁止折行
            WordWrap = false;字體

            _textMargin = new Padding(1);
        }this

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            SetTextDispLayout();
        }spa

        protected override void OnTextAlignChanged(EventArgs e)
        {
            base.OnTextAlignChanged(e);
            SetTextDispLayout();
        }orm

        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);
        }ip

        /// <summary>
        /// 繪製消息進行繪製邊框
        /// </summary>
        /// <param name="m"></param>
        private void WmPaint(ref System.Windows.Forms.Message m)
        {rem

            if (ClientRectangle.Width > 0 && ClientRectangle.Height > 0)
            {
                Rectangle clientRect = this.ClientRectangle;get

                Color lineColor = borderRenderStyle.LineColor;
                if (Focused)
                    lineColor = borderRenderStyle.ActiveLineColor;
                using (Graphics g = this.CreateGraphics())
                {
                    if (clientRect.Width > 0 && clientRect.Height > 0)
                    {
                        using (Pen pen = new Pen(lineColor, borderRenderStyle.LineWidth))
                        {
                            pen.DashStyle = borderRenderStyle.LineDashStyle;
                            if (borderRenderStyle.ShowLeftLine)
                            {
                                g.DrawLine(pen, clientRect.Location, new Point(clientRect.Left, clientRect.Bottom));
                            }

                            if (borderRenderStyle.ShowTopLine)
                            {
                                g.DrawLine(pen, clientRect.Location, new Point(clientRect.Right, clientRect.Top));
                            }

                            if (borderRenderStyle.ShowRightLine)
                            {
                                g.DrawLine(pen, new Point(clientRect.Right - 1, clientRect.Top), new Point(clientRect.Right - 1, clientRect.Bottom));
                            }

                            if (borderRenderStyle.ShowBottomLine)
                            {
                                g.DrawLine(pen, new Point(clientRect.Left, clientRect.Bottom - 1), new Point(clientRect.Right, clientRect.Bottom - 1));

                            }
                        }
                    }
                }
            }

 

        }

        /// <summary>
        /// 窗體處理消息主函數 處理粘貼及繪製消息
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref System.Windows.Forms.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 (flag)
            {
                flag = false;
                System.Windows.Forms.Clipboard.SetText(str);
                str = "";
            }

            if (m.Msg == WM_PAINT || m.Msg == WM_CTLCOLOREDIT)
            {
                WmPaint(ref m);
            }
        }


        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);

        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, ref Rectangle lParam);

        /// <summary>
        /// 尺寸變化時從新設置字體的顯示位置居中
        /// </summary>
        /// <param name="e"></param>
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            SetTextDispLayout();
        }

        /// <summary>
        /// 設置文本顯示佈局位置
        /// </summary>
        public void SetTextDispLayout()
        {
            if (Text == "")
                return;
            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 + TextMargin.Top;
            rect.X = 1 + TextMargin.Left;
            rect.Height = Height - 2;
            rect.Width = Width - TextMargin.Right - TextMargin.Left - 2;
            SendMessage(this.Handle, EM_SETRECT, IntPtr.Zero, ref rect);
        }

        /// <summary>
        /// 邊框樣式
        /// </summary>
        /// <remarks>獲取或設置邊框樣式.</remarks>
        [Category("Appearance"),
         Description("邊框樣式"),
         DefaultValue(null)]
        public virtual TTextBoxBorderRenderStyle BorderRenderStyle
        {
            get { return borderRenderStyle; }
            set { borderRenderStyle = value; }
        }
        private TTextBoxBorderRenderStyle borderRenderStyle = new TTextBoxBorderRenderStyle();

        /// <summary>
        /// 是否容許有回車
        /// </summary>
        public bool AllowReturn { get; set; }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);

            SetTextDispLayout();
        }

        private Padding _textMargin;
        /// <summary>
        /// Text Padding值
        /// </summary>
        public Padding TextMargin { get { return _textMargin; } set { _textMargin = value; SetTextDispLayout(); } }
    }

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class TTextBoxBorderRenderStyle
    {
        /// <summary>
        /// 邊線顏色
        /// </summary>
        /// <remarks>獲取或設置邊線顏色</remarks>
        [Category("Appearance"),
         Description("獲取或設置邊線顏色"),
         DefaultValue(typeof(Color), "Gray")]
        public virtual Color LineColor
        {
            get { return gridLineColor; }
            set { gridLineColor = value; }
        }
        private Color gridLineColor = Color.LightGray;

        /// <summary>
        /// 激活狀態時的邊線顏色
        /// </summary>
        /// <remarks>獲取或設置激活狀態時的邊線顏色.</remarks>
        [Category("Appearance"),
         Description("激活狀態時的邊線顏色"),
         DefaultValue(typeof(Color), "RoyalBlue")]
        public virtual Color ActiveLineColor
        {
            get { return activeGridLineColor; }
            set { activeGridLineColor = value; }
        }
        private Color activeGridLineColor = Color.RoyalBlue;

        [Category("Appearance"),
         Description("線寬度"),
         DefaultValue(1)]
        public virtual float LineWidth
        {
            get { return lineWidth; }
            set { lineWidth = value; }
        }
        private float lineWidth = 1;

        /// <summary>
        ///線樣式
        /// </summary>
        /// <remarks>獲取或設置線樣式.</remarks>
        [Category("Appearance"),
         Description("獲取或設置線樣式"),
         DefaultValue(typeof(DashStyle), "Solid")]
        public virtual DashStyle LineDashStyle
        {
            get { return lineDashStyle; }
            set { lineDashStyle = value; }
        }
        private DashStyle lineDashStyle = DashStyle.Solid;

        /// <summary>
        /// 左邊線是否顯示
        /// </summary>
        /// <remarks>獲取或設置左線是否顯示.</remarks>
        [Category("Appearance"),
         Description("左邊網格線是否顯示"),
        DefaultValue(true)
        ]
        public virtual bool ShowLeftLine
        {
            get { return showLeftLine; }
            set { showLeftLine = value; }
        }
        private bool showLeftLine = true;

        /// <summary>
        /// 上邊線是否顯示
        /// </summary>
        /// <remarks>獲取或設置上邊線是否顯示.</remarks>
        [Category("Appearance"),
         Description("上邊線是否顯示"),
        DefaultValue(true)
        ]
        public virtual bool ShowTopLine
        {
            get { return showTopLine; }
            set { showTopLine = value; }
        }
        private bool showTopLine = true;

        /// <summary>
        /// 右邊線是否顯示
        /// </summary>
        /// <remarks>獲取或設置右邊線是否顯示.</remarks>
        [Category("Appearance"),
         Description("右邊線是否顯示"),
        DefaultValue(true)
        ]
        public virtual bool ShowRightLine
        {
            get { return showRightLine; }
            set { showRightLine = value; }
        }
        private bool showRightLine = true;

        /// <summary>
        /// 底邊線是否顯
        /// </summary>
        /// <remarks>獲取或設置底邊線是否顯示.</remarks>
        [Category("Appearance"),
         Description("底邊線是否顯示"),
        DefaultValue(true)
        ]
        public virtual bool ShowBottomLine
        {
            get { return showBottomLine; }
            set { showBottomLine = value; }
        }
        private bool showBottomLine = true;

    }}

相關文章
相關標籤/搜索