
如图所示。
这个搜索框的颜色是可以按照用户个人的需求进行自由配置。
SearchListText 的属性如下:
| 属性名称 | 属性说明 |
| SkinBackColor | 搜索背景颜色和搜索框颜色 |
| SkinSearchButtonWidth | 搜索按钮长度 |
| SkinSearchButtonText | 搜索按钮文字 |
| SkinSearchButtonTextColor | 搜索按钮文字颜色 |
| SkinSearchButtonTextHoverColor | 鼠标移动到搜索按钮文字颜色 |
| SkinWaterText | 水印文字 |
| SkinText | 文本框中的文本内容 |
事件:
| 事件名称 | 事件说明 |
| SkinButtonClickEvent | 点击在按钮事件 |
控件源码:
/// <summary>
/// 搜索背景颜色和搜索框颜色
/// </summary>
private Color skinBackColor = Color.FromArgb(29, 179, 108);
/// <summary>
/// 搜索背景颜色和搜索框颜色
/// </summary>
[Description("搜索背景颜色和搜索框颜色")]
[Category("Skin")]
public Color SkinBackColor
{
get { return skinBackColor; }
set { skinBackColor = value; }
}
/// <summary>
/// 搜索按钮长度
/// </summary>
private int skinSearchButtonWidth = 90;
/// <summary>
/// 搜索按钮长度
/// </summary>
[Description("搜索按钮长度")]
[Category("Skin")]
public int SkinSearchButtonWidth
{
get { return skinSearchButtonWidth; }
set { skinSearchButtonWidth = value; }
}
/// <summary>
/// 搜索按钮文字
/// </summary>
private string skinSearchButtonText = "搜索";
/// <summary>
/// 搜索按钮文字
/// </summary>
[Description("搜索按钮文字")]
[Category("Skin")]
public string SkinSearchButtonText
{
get { return skinSearchButtonText; }
set { skinSearchButtonText = value; }
}
/// <summary>
/// 搜索按钮文字颜色
/// </summary>
private Color skinSearchButtonTextColor = Color.FromArgb(255, 230, 193);
/// <summary>
/// 搜索按钮文字颜色
/// </summary>
[Description("搜索按钮文字颜色")]
[Category("Skin")]
public Color SkinSearchButtonTextColor
{
get { return skinSearchButtonTextColor; }
set { skinSearchButtonTextColor = value; }
}
/// <summary>
/// 鼠标移动到搜索按钮文字颜色
/// </summary>
private Color skinSearchButtonTextHoverColor = Color.FromArgb(255, 255, 255);
/// <summary>
/// 鼠标移动到搜索按钮文字颜色
/// </summary>
[Description("鼠标移动到搜索按钮文字颜色")]
[Category("Skin")]
public Color SkinSearchButtonTextHoverColor
{
get { return skinSearchButtonTextHoverColor; }
set { skinSearchButtonTextHoverColor = value; }
}
/// <summary>
/// 水印文字
/// </summary>
private string skinWaterText = String.Empty;
/// <summary>
/// 水印文字
/// </summary>
[Description("水印文字")]
[Category("Skin")]
public string SkinWaterText
{
get { return skinWaterText; }
set { skinWaterText = value; }
}
/// <summary>
/// 文本框中的文本内容
/// </summary>
private string skinText;
/// <summary>
/// 文本框中的文本内容
/// </summary>
[Description("文本框中的文本内容")]
[Category("Skin")]
public string SkinText
{
get { return this.tbSearch.Text; }
set { this.tbSearch.Text = value; }
}
public delegate void SkinButtonClickDelegate(object sender, EventArgs e);
[Description("点击在按钮事件")]
public event SkinButtonClickDelegate SkinButtonClickEvent;
private Color SaveColor;
public SearchListText()
{
InitializeComponent();
}
private void SearchListText_Paint(object sender, PaintEventArgs e)
{
Bitmap localBitmap = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
//创建位图实例
Graphics bitmapGraphics = Graphics.FromImage(localBitmap);
bitmapGraphics.Clear(this.BackColor);
bitmapGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Draw(bitmapGraphics);
Graphics g = e.Graphics;
g.DrawImage(localBitmap, 0, 0); //在窗体的画布中绘画出内存中的图像
bitmapGraphics.Dispose();
localBitmap.Dispose();
}
public void Draw(Graphics g)
{
Brush brush = new SolidBrush(skinBackColor);
g.DrawRectangle(new Pen(brush), 0, 0, this.Width, this.Height - 1);
//Button的大小
Rectangle btnRect = new Rectangle(this.Width - SkinSearchButtonWidth, 0, SkinSearchButtonWidth, this.Height);
g.DrawRectangle(new Pen(brush), btnRect);
g.FillRectangle(brush, btnRect);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
Brush writeBrush = new SolidBrush(SkinSearchButtonTextColor);
g.DrawString(SkinSearchButtonText, this.Font, writeBrush, btnRect, sf);
g.Dispose();
}
private void SearchListText_Load(object sender, EventArgs e)
{
tbSearch.SetWatermark(SkinWaterText);
SaveColor = SkinSearchButtonTextColor;
}
private void SearchListText_MouseHover(object sender, EventArgs e)
{
SkinSearchButtonTextColor = SkinSearchButtonTextHoverColor;
this.Refresh();
}
private void SearchListText_MouseLeave(object sender, EventArgs e)
{
SkinSearchButtonTextColor = SaveColor;
this.Refresh();
}
private void SearchListText_MouseClick(object sender, MouseEventArgs e)
{
//判断是否点击在Button上
Rectangle btnRect = new Rectangle(this.Width - SkinSearchButtonWidth, 0, SkinSearchButtonWidth, this.Height);
if (btnRect.Contains(e.Location))
{
//将自定义事件绑定到控件事件上
if (SkinButtonClickEvent != null)
{
SkinButtonClickEvent(sender, e);
}
}
}
文章评论