ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Winform中TextBox文字垂直居中显示

2022-06-07 19:32:01  阅读:204  来源: 互联网

标签:居中 Top System base VerticalAlignment rc using Winform TextBox


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,
        }
    }
}

原文:https://blog.csdn.net/mazhiyuan1981/article/details/124350065

标签:居中,Top,System,base,VerticalAlignment,rc,using,Winform,TextBox
来源: https://www.cnblogs.com/RCJL/p/16353053.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有