文本框居中

 VB.NET 实现文本框垂直居中-CSDN博客

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


namespace WindowsFormsApplication2
{
    public partial class TextBoxEx : TextBox
    {
        private struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        };




        [DllImport("user32.dll ", CharSet = CharSet.Auto)]
        private static extern int SendMessage(IntPtr hwnd,int wMsg,int wParam,ref int lParam);
        [DllImport("user32.dll ", CharSet = CharSet.Auto)]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, ref RECT lParam);


        
        public TextBoxEx()
        {
        }




        private bool m_VerticalMiddle;


        public bool VerticalMiddle
        {
            get { return m_VerticalMiddle; }
            set { m_VerticalMiddle = value; }
        }


        RECT rc;
        int nLines;


        private void TextBoxEx_Layout(object sender, LayoutEventArgs e)
        {
            //SetVCenterText();
        }


        private void TextBoxEx_TextChanged(object sender, EventArgs e)
        {
            //long i=SendMessage(Handle,,0,0);
            long i;
            if (nLines!=i)
            {
                //SetVCenterText();
                nLines = Lines.Length;
            }
        }


        private void SetVCenterText()
        {
            if (!this.Multiline)
            {
                return;
            }


            SizeF sz;


            Graphics g = this.CreateGraphics();


            sz = g.MeasureString(Text, Font, this.Size);


            if (sz.Height==0)
            {
                sz = g.MeasureString("H", Font, this.Size);
            }


            RECT rect = rc;
            rect.Top =Convert.ToInt32( (this.ClientRectangle.Height - sz.Height)/ 2 - 2);
            rect.Bottom = this.ClientRectangle.Bottom;
            rect.Left = 1; //this.ClientRectangle.Left;
            rect.Right = this.ClientRectangle.Right - 1;


            if (rect.Top<1)
            {
                return;
            }


            SendMessage(Handle,EM_SETRECTNP,0&,rect)
        }
        //private Const EM_GETRECT = &HB2;
        //private Const EM_SETRECTNP = &HB4;
        //private Const EM_GETLINECOUNT = &HBA;
        //private const int  WM_PAINT  = &HF;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值