重写WinForm的ButtonEditEx组件,自定义控件,实现输入框内自定义图片icon并绑定图片的点击事件

领导提了一个需求:

说能不能自定义右边这三个点的样式,比如我想放张图片上去,点击图片就能触发点击事件;

我寻思这玩意要是放在html中写不就是分分钟的事,但是这是C#,是WinForm,我网上一通搜,结果一个教程没搜到。无奈只能尝试重写这个组件,自定义一个组件。

 首先还是先创建一个窗体,不需要窗体,只需要代码就可以了:

创建好窗体之后,下面是我的代码,可以直接复制稍微修改一下


using DevExpress.Utils.Design;
using DevExpress.Utils.Svg;
using DevExpress.XtraSpreadsheet.Utils.Trees;
using Svg;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;

namespace SZZDWMS.View
{
    public partial class CustomTextBoxWithImage : ControlLibrary.ButtonEditEx
    {
        private SvgDocument svgDocument;
        // 构造函数
        public CustomTextBoxWithImage()
        {
        
            InitializeComponent();
            this.Paint += CustomTextBoxWithImage_Paint;
            this.MouseEnter += CustomTextBoxWithImage_MouseEnter;
            this.MouseLeave += CustomTextBoxWithImage_MouseLeave;
            this.MouseMove += CustomTextBoxWithImage_MouseMove;
        }

        public void refshView(String SvgName) {
            string projectDirectory = AppDomain.CurrentDomain.BaseDirectory;
            string fileName = SvgName;
            string filePath = Path.Combine(projectDirectory, fileName);
            svgDocument = SvgDocument.Open(filePath);
            svgDocument.Width = 20;
            svgDocument.Height = this.Height;
            svgDocument.Color = new SvgColourServer(Color.Red);
        }
    
        // 初始化组件
        private void InitializeComponent()
        {
            svgDocument = new SvgDocument();
            svgDocument.Width = 20;
            svgDocument.Height = this.Height;
            svgDocument.Color = new SvgColourServer(Color.Red);
            // 进行必要的初始化
        }

        private bool isMouseOver = false;

        private void CustomTextBoxWithImage_MouseEnter(object sender, EventArgs e)
        {
            // 获取鼠标相对于控件的位置
            Point mouseLocation = PointToClient(MousePosition);
            // 获取图像区域
            System.Drawing.Rectangle imageBounds = GetRightButtonBounds();
            // 检查鼠标位置是否在图像区域内
            if (imageBounds.Contains(mouseLocation))
            {
                isMouseOver = true; // 鼠标悬停在图像上
                Invalidate(); // 使控件无效,触发重绘
            }
        }

        private void CustomTextBoxWithImage_MouseLeave(object sender, EventArgs e)
        {
            isMouseOver = false;
            this.Invalidate(); // 使控件无效,触发重绘
        }
        private void CustomTextBoxWithImage_MouseMove(object sender, MouseEventArgs e)
        {
            // 获取鼠标相对于控件的位置
            Point mouseLocation = e.Location;
            // 获取图像区域
            System.Drawing.Rectangle imageBounds = GetRightButtonBounds();
            // 检查鼠标位置是否在图像区域内
            if (imageBounds.Contains(mouseLocation))
            {
                if (!isMouseOver)
                {
                    isMouseOver = true; // 鼠标悬停在图像上
                    Invalidate(); // 使控件无效,触发重绘
                }
            }
            else
            {
                if (isMouseOver)
                {
                    isMouseOver = false; // 鼠标移出图像区域
                    Invalidate(); // 使控件无效,触发重绘
                }
            }
        }



        // 绘制事件处理程序
        private void CustomTextBoxWithImage_Paint(object sender, PaintEventArgs e)
        {
            Color backgroundColor = isMouseOver ? Color.LightGray : Color.White;
            using (SolidBrush backgroundBrush = new SolidBrush(backgroundColor))
            {
                e.Graphics.FillRectangle(backgroundBrush, GetRightButtonBounds());
            }
            SvgDocument svgDocument1 = svgDocument;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.DrawImage(svgDocument1.Draw(), GetRightButtonBounds());
        }

     

        // 获取右侧按钮的区域
        private System.Drawing.Rectangle GetRightButtonBounds()
        {
            // 假设右侧按钮的宽度为固定值,右对齐
            int buttonWidth = 20;
            int buttonHeight = this.Height - 2; // 减去上下间距
            int buttonX = this.Width - buttonWidth-1;
            int buttonY = 1; // 上边界留出1个像素的间距
            return new System.Drawing.Rectangle(buttonX, buttonY, buttonWidth, buttonHeight);
        }

    }
}

这点代码写完之后我们到工具箱就能看到自定义的组件了,图片要放在bin/debug文件夹下,初始画的时候直接new一个空的就可以了;

这里看到这个自定义组件了。直接拖进来之后  要在窗体的初始化页面做一些处理我们要更换掉这个组件的图片,和为这个图片绑定点击事件

 然后启动,我们就看到了样式

是可以输入 文字,我们来点击一下小图片试一下

是可以触发点击事件,并且只有图片可以触发,

 绝的可以用的麻烦点个关注点个赞

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值