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 ryControls; using ryControls.Skin; using System.Drawing.Drawing2D; namespace ryControls { [DefaultEvent("OnSearch")] public partial class rySearch : UserControl { /// /// 在搜索时激发 /// [Description("在搜索时激发")] public event EventHandler OnSearch; /// /// 文本变化时激发 /// [Description("文本变化时激发")] public new event EventHandler OnTextChanged; /// /// 文本变化时激发 /// [Description("文本变化时激发")] public new event EventHandler TextChanged; /// /// 双击时激发 /// [Description("双击时激发")] public new event EventHandler DoubleClick; /// /// /// public rySearch() { InitializeComponent(); txtSearch.Font = Font; txtSearch.ImeMode = ImeMode; base.GotFocus += RySearch_GotFocus; this.SetStyle( ControlStyles.AllPaintingInWmPaint | //忽略擦出的消息,减少闪烁。 ControlStyles.OptimizedDoubleBuffer |//在缓冲区上绘制,不直接绘制到屏幕上,减少闪烁。 ControlStyles.ResizeRedraw | //控件大小发生变化时,重绘。 ControlStyles.SupportsTransparentBackColor, true);//支持透明背景颜色 } private void RySearch_GotFocus(object sender, EventArgs e) { txtSearch.Focus(); txtSearch.Select(); txtSearch.SelectionLength = 0; } /// /// /// public new void Select() { //txtSearch.Select(); txtSearch.Focus(); } private string _EmptyText = ""; /// /// 文本为空时的显示效果 /// private void EmptyShow() { if (_text == "") { if (!txtSearch.Focused) { txtSearch.Font = Font; txtSearch.ForeColor = Color.FromArgb(175, 185, 200); isProcUse = true; txtSearch.Text = EmptyText; isProcUse = false; txtSearch.Select(0, 1); txtSearch.Select(0, 0); } else { HaveTextShow(); txtSearch.Text = ""; } } } /// /// 文本不选中 /// public void TextUnSelected() { txtSearch.Select(0, 0); } /// /// 有内容时的显示效果 /// private void HaveTextShow() { txtSearch.Font = Font; txtSearch.ForeColor = Color.Black; } /// /// 当文本框为空时,显示的内容。 /// [Description("当文本框为空时,显示的内容。")] public string EmptyText { get { return _EmptyText; } set { _EmptyText = value; EmptyShow(); } } private Font _font = new Font("宋体",9); /// /// 字体 /// public override Font Font { get { return _font; } set { _font = value; txtSearch.Font = value; btnSearch.Font = value; } } private bool _UseDefSkin = true; /// ///优先使用默认皮肤 /// [Description("优先使用默认皮肤")] [DefaultValue(typeof(bool), "true")] public bool UseDefSkin { get { return _UseDefSkin; } set { _UseDefSkin = value; btnSearch.UseDefSkin = _UseDefSkin; base.Invalidate(); } } /// /// 背景色是否渐变 /// [DefaultValue(false)] [Description("背景色是否渐变")] public bool ColorGradient { get { return btnSearch.ColorGradient; } set { if (value != btnSearch.ColorGradient) { btnSearch.ColorGradient = value; } } } private Color _baseColor = Color.FromArgb(12, 125, 182);//基颜色 /// ///按钮基础背景色 /// [Description("按钮基础背景色")] [DefaultValue(typeof(Color), "12, 125, 182")] public Color BaseColor { get { //if (SkinCommon.UseDefSkin && UseDefSkin) //{ // return SkinCommon.ButtonSkin.BackColor; //} return _baseColor; } set { _baseColor = value; btnSearch.BaseColor = _baseColor; //if (SkinCommon.UseDefSkin && UseDefSkin) //{ // btnSearch.BaseColor = SkinCommon.ButtonSkin.BackColor; //} //else //{ // btnSearch.BaseColor = _baseColor; //} } } /// ///按钮圆角部分背景颜色 /// [Description("按钮圆角部分背景颜色")] public Color ButtonRoundBackColor { get { return panel1.BackColor; } set { panel1.BackColor = value; } } private string _text = ""; /// /// /// public override string Text { get { return _text; } set { _text = value; if(_text=="") { EmptyShow(); } else { HaveTextShow(); txtSearch.Text = _text; //txtSearch.Select(0, 1); //txtSearch.Select(_text.Length, 0); } } } /// /// 重新设置大小 /// public void ResizeSize() { txtSearch.Location = new Point(3, 1); txtSearch.Top = (Height - txtSearch.Height) / 2; //Height = txtSearch.Height+2; //btnSearch.Height = Height; //btnSearch.Width = Height; //btnSearch.Location = new Point(Width - btnSearch.Width, 0); panel1.Width = panel1.Height; txtSearch.Width = Width - panel1.Width-3; } private void RySearch_Load(object sender, EventArgs e) { ResizeSize(); //EmptyShow(); } private void RySearch_FontChanged(object sender, EventArgs e) { ResizeSize(); } private void TxtSearch_Enter(object sender, EventArgs e) { HaveTextShow(); if (Text == "") { txtSearch.Text = ""; } } private bool isProcUse = false; private void TxtSearch_Leave(object sender, EventArgs e) { if (Text == "") { EmptyShow(); } else { txtSearch.Font = Font; } } private void TxtSearch_TextChanged(object sender, EventArgs e) { if (isProcUse) { return; } Text = txtSearch.Text; OnTextChanged?.Invoke(this, new EventArgs()); TextChanged?.Invoke(this, new EventArgs()); } /// /// 点击搜索按钮 /// public void PerformClick() { OnSearch?.Invoke(this, new EventArgs()); } private void BtnSearch_Click(object sender, EventArgs e) { OnSearch?.Invoke(this, new EventArgs()); } private void TxtSearch_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { btnSearch.PerformClick(); } //if(KeyDown!=null) OnKeyDown(e); } private void RySearch_Resize(object sender, EventArgs e) { ResizeSize(); } private void RySearch_Paint(object sender, PaintEventArgs e) { e.Graphics.DrawRectangle(new Pen(SkinHelp.DefalutBorderColor), new Rectangle(0, 0, this.Width - 1, this.Height - 1)); } private void TxtSearch_DoubleClick(object sender, EventArgs e) { DoubleClick?.Invoke(this, new EventArgs()); } private void RySearch_ImeModeChanged(object sender, EventArgs e) { txtSearch.ImeMode = base.ImeMode; } private void RySearch_Enter(object sender, EventArgs e) { txtSearch.Focus(); } private void TxtSearch_Click(object sender, EventArgs e) { HaveTextShow(); if (Text == "") { txtSearch.Text = ""; } } private void RySearch_Leave(object sender, EventArgs e) { EmptyShow(); } } }