陈程的技术博客

  • 关于作者
全栈软件工程师
一个专注于技术研究创新的程序员
  1. 首页
  2. .NET
  3. 正文

DreamSkin自定义美化控件-搜索控件

2016年7月29日 577点热度 0人点赞 0条评论

如图所示。

 

这个搜索框的颜色是可以按照用户个人的需求进行自由配置。

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);
             }

         }
     }

 

标签: C# winform
最后更新:2021年4月1日

博主

全栈工程师,侧重项目技术解决方案规划和开发

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

分类
  • .NET (65)
  • docker (3)
  • linux (12)
  • python (20)
  • web (14)
  • 小程序 (4)
  • 数据库 (2)
  • 未分类 (4)
  • 杂七杂八 (10)
标签聚合
DevExpress nginx C# winform centos js linux python
最新 热点 随机
最新 热点 随机
.NET开发手册标准参考 招募兼职前端开发 Centos安装dotnet6环境 VS上切换分支,vs编译运行出现bug,A fatal error was encountered彻底解决方案 用C#封装一个线程安全的缓存器,达到目标定时定量更新入库 C#通过特性的方式去校验指定数据是否为空
linux常用命令 winfrom 解决PictureBox加载图片后不释放内存问题 最简单的一种方法解决 Selenium上传文件 一个异常重启winform程序 DevExpress控件-GridControl控件中添加一列复选框并进行相关操作 使用NW.js把B/S应用做成跨平台桌面应用

COPYRIGHT © 2021 陈程的技术博客. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS