• 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

  • 相关阅读:
    Windows下升级Zabbix Agent
    mariadb+haproxy实现负载均衡(一)
    mariadb数据库galera下添加新的服务器节点
    1044/1045
    mariadb 离线安装
    CentSO7.6下部署Maridb Galera Cluster 实践记录(一)
    Word 远程调用失败:异常来自 HRESULT:0x800706BE
    CentSO7.6下部署Maridb Galera Cluster 实践记录(二)
    数据结构之双向链表-c语言实现
    数据结构之单链表-c语言实现
  • 原文地址:https://www.cnblogs.com/RCJL/p/16353053.html
Copyright © 2020-2023  润新知