C# winform 如何让TextBox文本内容垂直居中?

如题,C# winform 如何让TextBox文本内容垂直居中?
初一看感觉这个问题很简单,但实际比较复杂。winform的TextBox控件本身是不提供这个属性的。

本文提供一个可靠可用的重写控件给大家使用。
相信本文能够给一些需要此控件的人带去很大的方便。

本文提供的控件是我原创首发的。你在网络其它地方找没有的。除非别人转载了我的文章。

C# winform 如何让TextBox文本内容垂直居中?_第1张图片

本文主要提供以下主要功能:
1.控件提供了VerticalAlign属性,他提供了垂直对齐的三个方向:居上、居中、居下。
2.只允许单行文本。即不允许输入多行内容。屏蔽了通过复制粘贴,以及回车换行弄出多行文本的操作。

下图的枚举提供了该控件的垂直对齐的三个方向:

C# winform 如何让TextBox文本内容垂直居中?_第2张图片

全文代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Kintai.Cim.UI.UserControls.Common
{
    /// 
    /// 可垂直对齐的单行文本框
    /// 
    /// marc
    public class AlignableTextBox : TextBox
    {
        private VerticalAlignment _verticalAlignment = VerticalAlignment.Center;
        /// 
        /// 垂直对齐方向。默认是垂直居中
        /// 
        [Browsable(true)]
        [Category("Kintai")]
        [Description("垂直对齐方向。默认是垂直居中")]
        public VerticalAlignment VerticalAlign
        {
            get
            {
                return this._verticalAlignment;
            }
            set
            {
                this._verticalAlignment = value;

                this.Align();
            }
        }

        /// 
        public AlignableTextBox()
        {
            this.Multiline = true;
            this.WordWrap = false;
        }

        /// 
        protected override void OnClientSizeChanged(EventArgs e)
        {
            base.OnClientSizeChanged(e);

            this.Align();
        }

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

            this.Refresh();
        }

        /// 
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);

            //13表示回车
            if (e.KeyChar == 13)
            {
                e.Handled = true;
                return;
            }
        }

        /// 
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            //0x0302是粘贴消息
            if (m.Msg == 0x0302)
            {
                this.Text = Clipboard.GetText(TextDataFormat.Text);

                //拦截此消息
                m.Result = IntPtr.Zero;
                return;
            }

            //若此消息不是粘贴消息,则交给其基类去处理
            base.WndProc(ref m);
        }

        /// 
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value.Split(Environment.NewLine).First();
            }
        }

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

        private const int EM_GETRECT = 0xB2;
        private const int EM_SETRECTNP = 0xB4;

        /// 
        /// 对齐
        /// 
        private void Align()
        {
            TextBox text = this;
            if (text.Multiline == false)
            {
                return;
            }

            Rectangle rc = new Rectangle();
            SendMessage(text.Handle, EM_GETRECT, IntPtr.Zero, ref rc);
            //文字的大小
            Size size = TextRenderer.MeasureText(" ", text.Font);

            if (VerticalAlign == VerticalAlignment.Top)
            {
                rc.Y = rc.Top - ((rc.Bottom - rc.Top) - size.Height) / 2;
            }
            else if (VerticalAlign == VerticalAlignment.Center)
            {
                rc.Y = ((rc.Bottom - rc.Top) - size.Height) / 2;
            }
            else if (VerticalAlign == VerticalAlignment.Bottom)
            {
                //rc.Y = rc.Height - size.Height - 2;
                rc.Y = rc.Top + ((rc.Bottom - rc.Top) - size.Height) / 2 - 2;
            }

            SendMessage(text.Handle, EM_SETRECTNP, IntPtr.Zero, ref rc);

            text.Refresh();
        }

        /// 
        /// 表示垂直对齐的方向
        /// 
        /// marc
        public enum VerticalAlignment
        {
            /// 
            /// 居上
            /// 
            Top,
            /// 
            /// 垂直居中
            /// 
            Center,
            /// 
            /// 居下
            /// 
            Bottom,
        }
    }
}

祝您用餐愉快。

你可能感兴趣的:(Winform,Textbox,垂直居中)