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

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

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

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

本文主要提供以下主要功能:
1.控件提供了VerticalAlign属性,他提供了垂直对齐的三个方向:居上、居中、居下。
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
{
    /// <summary>
    /// 可垂直对齐的单行文本框
    /// </summary>
    /// <creator>marc</creator>
    public class AlignableTextBox : TextBox
    {
        private VerticalAlignment _verticalAlignment = VerticalAlignment.Center;
        /// <summary>
        /// 垂直对齐方向。默认是垂直居中
        /// </summary>
        [Browsable(true)]
        [Category("Kintai")]
        [Description("垂直对齐方向。默认是垂直居中")]
        public VerticalAlignment VerticalAlign
        {
            get
            {
                return this._verticalAlignment;
            }
            set
            {
                this._verticalAlignment = value;

                this.Align();
            }
        }

        /// <inheritdoc cref="AlignableTextBox"/>
        public AlignableTextBox()
        {
            this.Multiline = true;
            this.WordWrap = false;
        }

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

            this.Align();
        }

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

            this.Refresh();
        }

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

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

        /// <inheritdoc/>
        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);
        }

        /// <inheritdoc/>
        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;

        /// <summary>
        /// 对齐
        /// </summary>
        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();
        }

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

祝您用餐愉快。

  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值