简体   繁体   English

.NET Windows窗体鼠标为所有现有的PictureBox控件输入效果

[英].NET Windows Forms mouse enter effect for ALL existing PictureBox controls

I'm new here, and this is my first post here. 我是新来的,这是我在这里的第一篇文章。 My apologies if I don't follow the standards on creating a new question here on SO. 如果我不遵循在SO上提出新问题的标准,我深表歉意。

I've been racking my brains for the past couple of hours to try to program a custom class which would allow me to add a new property to existing PictureBox controls, allowing me to set a Color to my custom property, which would result in setting a border with the selected color as the user hovers the control. 在过去的几个小时中,我一直在绞尽脑汁,尝试编写一个自定义类,这将使我可以向现有的PictureBox控件中添加新属性,从而使Color可以设置为我的自定义属性,当用户将控件悬停时,带有选定颜色的边框。

Below is the code I've written so far: 以下是我到目前为止编写的代码:

[ProvideProperty("HoverColor", typeof(PictureBox))]
public class PictureBoxHover : Component, IExtenderProvider
{
    private readonly Dictionary<IntPtr, Color> _hoverColors;
    public PictureBoxHover()
    {
        _hoverColors = new Dictionary<IntPtr, Color>();
    }
    public bool CanExtend(object extendee)
    {
        return (extendee is PictureBox);
    }
    public Color GetHoverColor(PictureBox picb)
    {
        Color color;
        if (_hoverColors.TryGetValue(picb.Handle, out color))
            return color;
        return Color.Empty;
    }
    public void SetHoverColor(PictureBox picb, Color color)
    {
        Color hoverColor;
        _hoverColors[picb.Handle] = color;
    }
}

The above code adds the custom property "HoverColor" to all existing PictureBox controls in my solution - just as I need. 上面的代码根据需要将自定义属性“ HoverColor”添加到解决方案中所有现有的PictureBox控件中。 All I need to do now is somehow have it draw a border around my PictureBoxes with the set color when the user hovers over the control. 我现在要做的就是以某种方式在用户将鼠标悬停在控件上时以设置的颜色在PictureBox周围绘制边框。

I DON'T want my class to inherit the PictureBox or Control classes, as that would require having to change all my PictureBoxes from the ordinaly PictureBox to my custom PictureBox - which is why I instead want to 'append' this custom property and functionality to the ordinary PictureBox control. 我不想让我的类继承PictureBox或Control类,因为那将需要将我所有的PictureBox从原来的PictureBox更改为我的自定义PictureBox,这就是为什么我想将此“自定义”属性和功能“附加”到普通的PictureBox控件。

Any help would be greatly appreciated =) 任何帮助将不胜感激=)

Maybe you could achieve your goal simpler by using custom user control instead of creating extended properties for picture box. 通过使用自定义用户控件而不是为图片框创建扩展属性,也许可以更轻松地实现目标。 Below is a simple sample: 下面是一个简单的示例:

public class FramedPictureBox : UserControl
{
    private readonly PictureBox _pictureBox;

    public FramedPictureBox()
    {
        const int FRAME_SIZE = 3;
        _pictureBox = new PictureBox
                          {
                              Left = FRAME_SIZE,
                              Top = FRAME_SIZE,
                              Width = Width - 2*FRAME_SIZE,
                              Height = Height - 2*FRAME_SIZE,
                              Anchor = AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Top
                          };

        _pictureBox.MouseEnter += OnPictureBoxMouseEnter;
        _pictureBox.MouseLeave += OnPictureBoxMouseLeave;

        Controls.Add(_pictureBox);
    }

    protected override void OnLoad(EventArgs e)
    {
        var image = new Bitmap(_pictureBox.Width, _pictureBox.Height);
        var graphics = Graphics.FromImage(image);
        graphics.Clear(Color.White);

        _pictureBox.Image = image;

        base.OnLoad(e);
    }

    private void OnPictureBoxMouseEnter(object sender, EventArgs e)
    {
        BackColor = Color.Red;
    }

    private void OnPictureBoxMouseLeave(object sender, EventArgs e)
    {
        BackColor = SystemColors.Control;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM