123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482 |
- using LYFZ.ComponentLibrary.Properties;
- using LYFZ.OtherExpansion.SkinClass;
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Windows.Forms;
- namespace LYFZ.OtherExpansion.SkinControl
- {
- public class SkinTextBox : UserControl
- {
- private bool flag;
- private Cursor _cursor = Cursors.IBeam;
- private ControlState _mouseState;
- private ControlState _iconMouseState;
- private Bitmap mouseback;
- private Bitmap normlback;
- private bool _iconIsButton;
- private Image _icon;
- private IContainer components = null;
- private WaterTextBox BaseText;
- public event EventHandler IconClick;
- [Category("Skin"), Description("悬浮时背景框。")]
- public Bitmap MouseBack
- {
- get
- {
- return this.mouseback;
- }
- set
- {
- if (this.mouseback != value)
- {
- this.mouseback = value;
- base.Invalidate();
- }
- }
- }
- [Category("Skin"), Description("正常状态时背景框。")]
- public Bitmap NormlBack
- {
- get
- {
- return this.normlback;
- }
- set
- {
- if (this.normlback != value)
- {
- this.normlback = value;
- base.Invalidate();
- }
- }
- }
- [Category("Skin"), Description("指定可以在编辑控件中输入的最大字符数。")]
- public int MaxLength
- {
- get
- {
- return this.BaseText.MaxLength;
- }
- set
- {
- this.BaseText.MaxLength = value;
- }
- }
- [Category("Skin"), Description("控件编辑控件的文本是否能够跨越多行。")]
- public bool Multiline
- {
- get
- {
- return this.BaseText.Multiline;
- }
- set
- {
- this.BaseText.Multiline = value;
- }
- }
- [Category("Skin"), Description("指示将为单行编辑控件的密码输入显示的字符。")]
- public char IsPasswordChat
- {
- get
- {
- return this.BaseText.PasswordChar;
- }
- set
- {
- this.BaseText.PasswordChar = value;
- }
- }
- [Category("Skin"), Description("控制能否更改编辑控件中的文本。")]
- public bool ReadOnly
- {
- get
- {
- return this.BaseText.ReadOnly;
- }
- set
- {
- this.BaseText.ReadOnly = value;
- }
- }
- [Category("Skin"), Description("指示编辑控件中的文本是否以默认的密码字符显示。")]
- public bool IsSystemPasswordChar
- {
- get
- {
- return this.BaseText.UseSystemPasswordChar;
- }
- set
- {
- this.BaseText.UseSystemPasswordChar = value;
- }
- }
- [Category("Skin"), Description("指示多行编辑控件是否自动换行。")]
- public bool WordWrap
- {
- get
- {
- return this.BaseText.WordWrap;
- }
- set
- {
- this.BaseText.WordWrap = value;
- }
- }
- [Category("Skin"), Description("用于显示控件中文本的字体。")]
- public override Font Font
- {
- get
- {
- return this.BaseText.Font;
- }
- set
- {
- this.BaseText.Font = value;
- base.Font = value;
- }
- }
- [Category("Skin"), Description("此组件的前景色,用于显示文本。")]
- public override Color ForeColor
- {
- get
- {
- return this.BaseText.ForeColor;
- }
- set
- {
- this.BaseText.ForeColor = value;
- base.ForeColor = value;
- }
- }
- [Category("Skin"), Description("多行编辑中的文本行,作为字符串值的数组。")]
- public string[] Lines
- {
- get
- {
- return this.BaseText.Lines;
- }
- set
- {
- this.BaseText.Lines = value;
- }
- }
- [Category("Skin"), Description("指示对于多行编辑控件,将为此控件显示哪些滚动条。")]
- public ScrollBars ScrollBars
- {
- get
- {
- return this.BaseText.ScrollBars;
- }
- set
- {
- this.BaseText.ScrollBars = value;
- }
- }
- [Category("Skin"), Description("指示应该如何对齐编辑控件的文本。")]
- public HorizontalAlignment TextAlign
- {
- get
- {
- return this.BaseText.TextAlign;
- }
- set
- {
- this.BaseText.TextAlign = value;
- }
- }
- [Category("Skin"), Description("文本框的图标")]
- public Image Icon
- {
- get
- {
- return this._icon;
- }
- set
- {
- if (this._icon != value)
- {
- this._icon = value;
- base.Invalidate(this.IconRect);
- this.PositionTextBox();
- }
- }
- }
- [Category("Skin"), Description("文本框的图标是否是按钮")]
- public bool IconIsButton
- {
- get
- {
- return this._iconIsButton;
- }
- set
- {
- this._iconIsButton = value;
- }
- }
- [Category("Skin"), Description("水印文字")]
- public string WaterText
- {
- get
- {
- return this.BaseText.WaterText;
- }
- set
- {
- this.BaseText.WaterText = value;
- }
- }
- [Category("Skin"), Description("水印颜色")]
- public Color WaterColor
- {
- get
- {
- return this.BaseText.WaterColor;
- }
- set
- {
- this.BaseText.WaterColor = value;
- }
- }
- [Browsable(true), Category("Skin"), Description("与控件关联的文本")]
- public override string Text
- {
- get
- {
- return this.BaseText.Text;
- }
- set
- {
- this.BaseText.Text = value;
- }
- }
- public override Cursor Cursor
- {
- get
- {
- return this._cursor;
- }
- set
- {
- this._cursor = value;
- }
- }
- protected ControlState MouseState
- {
- get
- {
- return this._mouseState;
- }
- set
- {
- this._mouseState = value;
- base.Invalidate();
- }
- }
- protected ControlState IconMouseState
- {
- get
- {
- return this._iconMouseState;
- }
- set
- {
- this._iconMouseState = value;
- base.Invalidate();
- }
- }
- protected Rectangle IconRect
- {
- get
- {
- return new Rectangle(base.Width - 23, 3, 20, 22);
- }
- }
- public SkinTextBox()
- {
- this.InitializeComponent();
- this.Init();
- this.InitEvents();
- this.BackColor = Color.Transparent;
- }
- public void Init()
- {
- base.SetStyle(ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer, true);
- base.UpdateStyles();
- }
- private void InitEvents()
- {
- this.BaseText.MouseEnter += new EventHandler(this.BaseText_MouseEnter);
- this.BaseText.MouseLeave += new EventHandler(this.BaseText_MouseLeave);
- this.BaseText.KeyDown += new KeyEventHandler(this.BaseText_KeyDown);
- this.BaseText.KeyPress += new KeyPressEventHandler(this.BaseText_KeyPress);
- this.BaseText.KeyUp += new KeyEventHandler(this.BaseText_KeyUp);
-
- }
- protected virtual void PositionTextBox()
- {
- if (this._icon != null && !this.flag)
- {
- base.Padding = new Padding(base.Padding.Left, base.Padding.Top, base.Padding.Right + 23, base.Padding.Bottom);
- this.flag = true;
- return;
- }
- if (this._icon == null && this.flag)
- {
- base.Padding = new Padding(base.Padding.Left, base.Padding.Top, base.Padding.Right - 23, base.Padding.Bottom);
- this.flag = false;
- }
- }
- public void AppendText(string str)
- {
- this.BaseText.AppendText(str);
- }
- private void OnIconClick()
- {
- if (this.IconClick != null)
- {
- this.IconClick(this, EventArgs.Empty);
- }
- }
- protected void BaseText_MouseLeave(object sender, EventArgs e)
- {
- this.MouseState = ControlState.Normal;
- }
- private void BaseText_MouseEnter(object sender, EventArgs e)
- {
- this.MouseState = ControlState.Hover;
- }
- private void BaseText_KeyUp(object sender, KeyEventArgs e)
- {
- this.OnKeyUp(e);
- }
- private void BaseText_KeyPress(object sender, KeyPressEventArgs e)
- {
- this.OnKeyPress(e);
- }
- private void BaseText_KeyDown(object sender, KeyEventArgs e)
- {
- this.OnKeyDown(e);
- }
- protected override void OnSizeChanged(EventArgs e)
- {
- base.OnSizeChanged(e);
- if (base.Height > 28)
- {
- this.BaseText.Multiline = true;
- return;
- }
- this.BaseText.Multiline = false;
- }
- protected override void OnPaint(PaintEventArgs e)
- {
- base.OnPaint(e);
- Graphics g = e.Graphics;
- g.SmoothingMode = SmoothingMode.AntiAlias;
- Bitmap mouse = (this.MouseBack == null) ? Resources.frameBorderEffect_mouseDownDraw : this.MouseBack;
- Bitmap norml = (this.NormlBack == null) ? Resources.frameBorderEffect_normalDraw : this.NormlBack;
- Bitmap btm = (this._mouseState == ControlState.Hover) ? mouse : norml;
- if (btm != null)
- {
- DrawHelper.RendererBackground(g, base.ClientRectangle, btm, true);
- }
- if (this._icon != null)
- {
- Rectangle iconRect = this.IconRect;
- if (this._iconMouseState == ControlState.Pressed)
- {
- iconRect.X++;
- iconRect.Y++;
- }
- g.DrawImage(this._icon, iconRect, 0, 0, this._icon.Width, this._icon.Height, GraphicsUnit.Pixel);
- }
- }
- protected override void OnMouseEnter(EventArgs e)
- {
- base.OnMouseEnter(e);
- this.MouseState = ControlState.Hover;
- }
- protected override void OnMouseMove(MouseEventArgs e)
- {
- base.OnMouseMove(e);
- if (this._icon == null || !this.IconRect.Contains(e.Location))
- {
- this.Cursor = Cursors.IBeam;
- return;
- }
- if (this._iconIsButton)
- {
- this.Cursor = Cursors.Hand;
- return;
- }
- this.Cursor = Cursors.IBeam;
- }
- protected override void OnMouseDown(MouseEventArgs e)
- {
- base.OnMouseDown(e);
- if (this._icon != null && this._iconIsButton && e.Button == MouseButtons.Left && this.IconRect.Contains(e.Location))
- {
- this.IconMouseState = ControlState.Pressed;
- }
- }
- protected override void OnMouseUp(MouseEventArgs e)
- {
- base.OnMouseUp(e);
- if (this._icon != null && this._iconIsButton)
- {
- this.IconMouseState = ControlState.Hover;
- if (e.Button == MouseButtons.Left && this.IconRect.Contains(e.Location))
- {
- this.OnIconClick();
- }
- }
- }
- protected override void OnMouseLeave(EventArgs e)
- {
- base.OnMouseLeave(e);
- this.MouseState = ControlState.Normal;
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing && this.components != null)
- {
- this.components.Dispose();
- }
- base.Dispose(disposing);
- }
- private void InitializeComponent()
- {
- this.BaseText = new LYFZ.OtherExpansion.SkinControl.WaterTextBox();
- this.SuspendLayout();
- //
- // BaseText
- //
- this.BaseText.BorderStyle = System.Windows.Forms.BorderStyle.None;
- this.BaseText.Dock = System.Windows.Forms.DockStyle.Fill;
- this.BaseText.Font = new System.Drawing.Font("微软雅黑", 9.75F);
- this.BaseText.Location = new System.Drawing.Point(5, 5);
- this.BaseText.Margin = new System.Windows.Forms.Padding(0);
- this.BaseText.Name = "BaseText";
- this.BaseText.Size = new System.Drawing.Size(175, 18);
- this.BaseText.TabIndex = 1;
- this.BaseText.WaterColor = System.Drawing.Color.DarkGray;
- this.BaseText.WaterText = "";
- //
- // SkinTextBox
- //
- this.BackColor = System.Drawing.Color.Black;
- this.Controls.Add(this.BaseText);
- this.Cursor = System.Windows.Forms.Cursors.IBeam;
- this.DoubleBuffered = true;
- this.Margin = new System.Windows.Forms.Padding(0);
- this.MinimumSize = new System.Drawing.Size(0, 28);
- this.Name = "SkinTextBox";
- this.Padding = new System.Windows.Forms.Padding(5);
- this.Size = new System.Drawing.Size(185, 28);
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- }
- }
|