C# TextBox增加Padding屬性,或者TextBox垂直居中

来源:互联网 发布:婚纱摄影电子相册软件 编辑:程序博客网 时间:2024/06/08 04:29
 public partial class PubTextBox : TextBox
    {
        //设置Rect消息
        private const int EM_SETRECT = 179;
        //获取Rect消息
        private const int EM_GETRECT = 178;
        //粘贴消息
        private const int WM_PASTE = 0x0302;
        public PubTextBox()
        {
            InitializeComponent();             
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            SetTextDispLayout();
        }

        protected override void OnTextAlignChanged(EventArgs e)
        {
            base.OnTextAlignChanged(e);
            SetTextDispLayout();
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            //如果不允许回车 屏蔽回车 换行键值
            if (!AllowReturn
                && ((int)e.KeyChar == (int)Keys.Return || (int)e.KeyChar == (int)Keys.LineFeed))
            {
                e.Handled = true;
            }
            base.OnKeyPress(e);
        }

        /// <summary>
        /// 窗体处理消息主函数 处理粘贴及绘制消息
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            string str = "";
            bool flag = false;
            int i = 0;
            if (m.Msg == 0x0204)
                i++;
            if (!AllowReturn
                && m.Msg == WM_PASTE
                && System.Windows.Forms.Clipboard.ContainsText())
            {
                str = System.Windows.Forms.Clipboard.GetText();
                System.Windows.Forms.Clipboard.Clear();
                string nstr = str.Replace(char.ConvertFromUtf32((int)Keys.Return), "").Replace(char.ConvertFromUtf32((int)Keys.LineFeed), "");
                System.Windows.Forms.Clipboard.SetText(nstr);
                if (str.Length > 0) flag = true;
            }


            base.WndProc(ref m);
            if (flag)
            {
                flag = false;
                System.Windows.Forms.Clipboard.SetText(str);
                str = "";
            }           
        }
        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);

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

        /// <summary>
        /// 尺寸变化时重新设置字体的显示位置居中
        /// </summary>
        /// <param name="e"></param>
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            SetTextDispLayout();
        }

        /// <summary>
        /// 设置文本显示布局位置
        /// </summary>
        public void SetTextDispLayout()
        {
            if (Text == "")
                return;
            //當允許多行和禁止回車時,Paddin有效
            if (this.Multiline && (!this.WordWrap))
            {
                Rectangle rect = new Rectangle();
                SendMessage(this.Handle, EM_GETRECT, (IntPtr)0, ref rect);
                //SizeF size = CreateGraphics().MeasureString(Text, Font);//垂直居中 計算文本高度
                //rect.Y = (int)(Height - size.Height) / 2 + TextPadding.Top;
                rect.Y = TextPadding.Top;
                rect.X = TextPadding.Left;
                rect.Height = Height;
                rect.Width = Width - TextPadding.Right - TextPadding.Left;
                SendMessage(this.Handle, EM_SETRECT, IntPtr.Zero, ref rect);
            }
        }    

        /// <summary>
        /// 是否允许有回车
        /// </summary>
        [CategoryAttribute("設定"), DisplayName("允許回車"), DescriptionAttribute("配合內邊使用")]
        public bool AllowReturn { get; set; }

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

            SetTextDispLayout();
        }

        private Padding _textpadding=new Padding(1);
        /// <summary>
        /// Text Padding值  當允許多行和禁止回車時,Paddin有效
        /// </summary>
        [CategoryAttribute("設定"), DisplayName("內邊距"), DescriptionAttribute(" 當允許多行和禁止回車時,Paddin有效")]

        public Padding TextPadding { get { return _textpadding; } set { _textpadding = value; SetTextDispLayout(); } }

    }



參考:http://www.cnblogs.com/jason_chen/p/6291295.html



0 0
原创粉丝点击