简体   繁体   English

图片框上的鼠标事件?

[英]Mouse events on picturebox?

I have a "start" button with a custom image I have made. 我有一个带有已制作的自定义图像的“开始”按钮。 I haven't messed around with this part of C#.net, but I know a bit about VB.NET. 我没有弄错C#.net的这一部分,但是我对VB.NET有所了解。

I've seen people have something like public void picturebox_MouseDown() and whatnot, but none seems to work. 我见过人们有一些东西,比如public void picturebox_MouseDown()东西,但是似乎都没有用。 I am trying to change the image when a mouse event is given. 我正在尝试给定鼠标事件时更改图像。

MouseDown would change the image to StartButtonDown MouseDown会将图像更改为StartButtonDown

MouseUp would change the image to StartButtonUp MouseUp会将图像更改为StartButtonUp

MouseEnter would change the image to StartButtonHover MouseEnter会将图像更改为StartButtonHover

MouseLeave would change the image to StartButtonUp MouseLeave会将图像更改为StartButtonUp

Is there something specific I should do, I've google'd this for about an hour and still haven't found anything to help me. 有什么我应该做的特别的事情,我已经用Google搜索了大约一个小时,但仍然找不到任何可以帮助我的东西。

Here is something I wrote which is very similar to what you require. 这是我写的,与您的要求非常相似。

using System;
using System.Drawing;
using System.Windows.Forms;

public partial class ImageButton : PictureBox
{
    private Image _upImage, _downImage, _hoverImage;

    [System.ComponentModel.Browsable(true),
     System.ComponentModel.Category("Images")]
    public Image UpImage
    {
        get { return _upImage; }
        set
        {
            if (value != null)
            {
                _upImage = value;
                this.Image = _upImage;
            }
        }
    }

    [System.ComponentModel.Browsable(true),
     System.ComponentModel.Category("Images")]
    public Image DownImage
    {
        get { return _downImage; }
        set
        {
            if (value != null)
            {
                _downImage = value;
            }
        }
    }

    [System.ComponentModel.Browsable(true),
     System.ComponentModel.Category("Images")]
    public Image HoverImage
    {
        get { return _hoverImage; }
        set
        {
            if (value != null)
            {
                _hoverImage = value;
            }
        }
    }

    public ImageButton()
    {
        InitializeComponent();
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        if (DownImage != null)
            this.Image = DownImage;

        base.OnMouseDown(e);
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        if (UpImage != null)
            this.Image = UpImage;

        base.OnMouseUp(e);
    }

    protected override void OnMouseEnter(EventArgs e)
    {
        if (HoverImage != null)
            this.Image = HoverImage;

        base.OnMouseEnter(e);
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        if (UpImage != null)
            this.Image = UpImage;

        base.OnMouseLeave(e);
    }

}

What I have done is inherrited from the standard PictureBox to make an ImageButton . 我所做的工作是从标准PictureBox继承而来的,以制作一个ImageButton I have three properties for the Image to display with no mouse action (UpImage), the Image to display when the MouseDown event is triggered (DownImage), and the Image to display when the mouse is hovering over the control (HoverImage). 我有三个属性的Image与没有鼠标动作(UpImage)时,显示Image时MouseDown事件被触发(DownImage)来显示,并且Image当鼠标悬停在控制(HoverImage)来显示。

Note that you should add a check for the MouseUp and MouseLeave events. 请注意,您应该添加对MouseUp和MouseLeave事件的检查。 If I click on the image and drag the mouse away from the control, the control will go from the UpImage to the DownImage to the UpImage again because I have left the control (MouseLeave) even though my mouse is still down. 如果单击图像并将鼠标拖离控件,则控件将再次从UpImage转到DownImage,再到UpImage,因为即使我的鼠标仍处于按下状态,我也已离开控件(MouseLeave)。 You may desire that the DownImage remain displayed when the mouse leaves the control. 您可能希望当鼠标离开控件时,DownImage保持显示。 Additionally, when the MouseUp event occurs, you should check if the mouse is still hovering over the control. 此外,发生MouseUp事件时,应检查鼠标是否仍悬停在控件上。 If it is, you will want to display the HoverImage rather than the UpImage. 如果是这样,您将要显示HoverImage而不是UpImage。

You could also check for which mouse button is used. 您还可以检查使用了哪个鼠标按钮。 Maybe you only want the images to change with left button clicks, not right or middle. 也许您只希望通过单击鼠标左键而不是右键或中间键来更改图像。

But this should get you started. 但这应该可以帮助您入门。

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

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