using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace LYFZ.ComponentLibrary { public partial class TextBoxEx : UserControl { #region 声明 private Bitmap _TextBoxBackImg = LYFZ.ComponentLibrary.Properties.Resources.Textbox_bule; private State state = State.Default; private bool _Isico = false; private Bitmap _Ico; private Padding _IcoPadding = new Padding(3, 3, 0, 0); //枚鼠标状态 private enum State { Normal = 1, MouseOver = 2, MouseDown = 3, Disable = 4, Default = 5 } #endregion public TextBoxEx() { InitializeComponent(); this.SetStyle(ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.DoubleBuffer, true); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.SetStyle(ControlStyles.StandardDoubleClick, false); this.SetStyle(ControlStyles.Selectable, true); this.BackColor = Color.Transparent; SetStyle(ControlStyles.DoubleBuffer, true); SetStyle(ControlStyles.AllPaintingInWmPaint, false); SetStyle(ControlStyles.ResizeRedraw, true); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.Paint += new PaintEventHandler(TextBoxEx_Paint); } void BaseText_SizeChanged(object sender, EventArgs e) { setTextBoxExSize(); } /// /// 设置大小 /// void setTextBoxExSize() { if (BaseText.Multiline) { BaseText.Height = this.Height - 6; this.BaseText.Width = this.Width - 6; } else { this.Height = BaseText.Height + 6; this.BaseText.Width = this.Width - 6; } } #region 属性 [Category("控件扩展属性"), Description("输入最大字符数")] public int MaxLength { get { return BaseText.MaxLength; } set { BaseText.MaxLength = value; Invalidate(false); } } [Browsable(true),Category("控件扩展属性"), Description("与控件关联的文本"),DefaultValue("")] public override string Text { get { return BaseText.Text; } set { BaseText.Text = value; Invalidate(false); } } [Category("控件扩展属性"), Description("将控件设为密码显示")] public bool IsPass { get { return BaseText.UseSystemPasswordChar; } set { BaseText.UseSystemPasswordChar = value; Invalidate(false); } } [Category("控件扩展属性"), Description("密码显示字符")] public char PassChar { get { return BaseText.PasswordChar; } set { BaseText.PasswordChar = value; Invalidate(false); } } [Category("控件扩展属性"), Description("将控件设为多行文本显示")] public bool Multiline { get { return BaseText.Multiline; } set { BaseText.Multiline = value; if (value) { BaseText.Height = this.Height - 6; } else { base.Height = 22; BaseText.Height = 16; this.Invalidate(); } } } [Category("控件扩展属性"), Description("设置控件中文本字体")] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public override Font Font { get { return BaseText.Font; } set { BaseText.Font = value; Invalidate(); } } [Category("控件扩展属性"), Description("将控件设为只读")] public bool ReadOnly { get { return BaseText.ReadOnly; } set { BaseText.ReadOnly = value; Invalidate(false); } } [Category("控件扩展属性"), Description("多行文本的编辑行")] public String[] lines { get { return BaseText.Lines; } set { BaseText.Lines = value; Invalidate(false); } } [Category("控件扩展属性"), Description("是否显示图标")] public bool Isico { get { return _Isico; } set { _Isico = value; if (value) { if (_Ico != null) { BaseText.Location = new Point(_IcoPadding.Left + _Ico.Width, 3); BaseText.Width = BaseText.Width - _IcoPadding.Left - _Ico.Width; } else { BaseText.Location = new Point(25, 3); BaseText.Width = BaseText.Width - 25; } } this.Invalidate(); } } [Category("控件扩展属性"), Description("图标文件")] public Bitmap Ico { get { return _Ico; } set { _Ico = value; Invalidate(false); } } [Category("控件扩展属性"), Description("控件内部间距,图标文件")] public Padding IcoPadding { get { return _IcoPadding; } set { _IcoPadding = value; this.Invalidate(); } } [Category("控件扩展属性"), Description("滚动条显示设置"), DefaultValue(typeof(ScrollBars), "None")] public ScrollBars ScrollBars { get { return BaseText.ScrollBars; } set { BaseText.ScrollBars = value; Invalidate(false); } } public new bool Enabled { get { return base.Enabled; } set { base.Enabled = value; if (!base.Enabled) { state = State.Disable; } else { state = State.Default; } Invalidate(false); } } #region 文本框边框颜色 [DllImport("user32.dll")] private static extern IntPtr GetWindowDC(IntPtr hwnd); [DllImport("user32.dll")] private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc); private Color _borderColor = Color.Black; private int _borderWidth = 1; // // 摘要: // 获取或设置控件的边框颜色。 // // 返回结果: // 控件的边框颜色 System.Drawing.Color。默认为 System.Drawing.Color.Black // 属性的值。 [Description("组件的边框颜色。"), Category("Appearance")] public Color BorderColor { get { return _borderColor; } set { _borderColor = value; this.Invalidate(); } } // // 摘要: // 获取或设置控件的边框宽度。 // // 返回结果: // 控件的边框宽度 int。默认为 1 // 属性的值。 [Description("组件的边框宽度。"), Category("Appearance")] public int BorderWidth { get { return _borderWidth; } set { _borderWidth = value; this.Invalidate(); } } private void TextBoxEx_Paint(object sender, PaintEventArgs e) { if (this.BorderStyle == BorderStyle.FixedSingle) { IntPtr hDC = GetWindowDC(this.Handle); Graphics g = Graphics.FromHdc(hDC); ControlPaint.DrawBorder( g, new Rectangle(0, 0, this.Width, this.Height), _borderColor, _borderWidth, ButtonBorderStyle.Solid, _borderColor, _borderWidth, ButtonBorderStyle.Solid, _borderColor, _borderWidth, ButtonBorderStyle.Solid, _borderColor, _borderWidth, ButtonBorderStyle.Solid); g.Dispose(); ReleaseDC(Handle, hDC); } } #endregion #endregion #region 委托 [Category("控件扩展事件"),Description("点击图标时触发的事件")] public event EventHandler IcoOnclick; #endregion #region 方法 protected override void OnPaint(PaintEventArgs e) { Rectangle rc = this.ClientRectangle; Graphics g = e.Graphics; ImageDrawRect.DrawRect(g, _TextBoxBackImg, rc, Rectangle.FromLTRB(10, 10, 10, 10), (int)state, 5); // ImageDrawRect.DrawRect(g, _TextBoxBackImg, rc, (int)state, 5); if (_Isico) { if (_Ico != null) { g.DrawImage(_Ico, new Rectangle(_IcoPadding.Left, _IcoPadding.Top, 16, 16), new Rectangle(0, 0, Ico.Width, Ico.Height), GraphicsUnit.Pixel); } } base.OnPaint(e); } /// /// 禁止调整大小 /// /// protected override void OnResize(EventArgs e) { if (this.Height > 22) { Multiline = true; } else { this.Height = 22; Multiline = false; } setTextBoxExSize(); if (Isico && Ico != null) { this.BaseText.Width = this.Width - 3 - this.IcoPadding.Left - 16; } else { this.BaseText.Width = this.Width - 6; } } //protected override void OnFontChanged(EventArgs e) //{ // if (Multiline) // { // this.BaseText.Height = this.Height - 8; // } // else // { // this.Height = 210; // this.BaseText.Height = 140; // } // base.OnFontChanged(e); //} private void NotifyIcoOnclick() { if (IcoOnclick != null) { IcoOnclick(this, EventArgs.Empty); } } public void AppendText(string ss) { BaseText.AppendText(ss); } private void BaseText_MouseEnter(object sender, EventArgs e) { state = State.MouseOver; this.Invalidate(); } private void BaseText_MouseLeave(object sender, EventArgs e) { state = State.Default; this.Invalidate(); } private void TextBoxEx_MouseUp(object sender, MouseEventArgs e) { if (_Ico != null) { if (new Rectangle(_IcoPadding.Left, _IcoPadding.Top, _Ico.Width, _Ico.Height).Contains(e.X, e.Y)) { NotifyIcoOnclick(); } } } private void TextBoxEx_MouseEnter(object sender, EventArgs e) { state = State.MouseOver; this.Invalidate(); } private void TextBoxEx_MouseLeave(object sender, EventArgs e) { state = State.Default; this.Invalidate(); } #region 定义文本框值更改后触发事件 //定义delegate public delegate void TextBoxEx_TextChanged(object sender, EventArgs e); //用event 关键字声明事件对象 /// /// /// [Category("控件扩展事件"), Description("文本框值修改时")] public event TextBoxEx_TextChanged EventTextBoxEx_TextChanged; #endregion private void BaseText_TextChanged(object sender, EventArgs e) { if(this.EventTextBoxEx_TextChanged!=null) EventTextBoxEx_TextChanged(sender, e); } #endregion } }