C# picturebox上的鼠标事件?
我有一个带有自定义图像的开始按钮。我没有弄乱C.net的这一部分,但我对VB.net有点了解 我见过人们有类似于公共空间图片盒(public void picturebox_MouseDown)之类的东西,但似乎没有一个能起作用。我试图在给定鼠标事件时更改图像 MouseDown会将图像更改为StartButtonDown MouseUp会将图像更改为StartButtonUp MouseEnter会将图像更改为StartButtonHover MouseLeave会将图像更改为StartButtonPC# picturebox上的鼠标事件?,c#,.net,winforms,mouseevent,picturebox,C#,.net,Winforms,Mouseevent,Picturebox,我有一个带有自定义图像的开始按钮。我没有弄乱C.net的这一部分,但我对VB.net有点了解 我见过人们有类似于公共空间图片盒(public void picturebox_MouseDown)之类的东西,但似乎没有一个能起作用。我试图在给定鼠标事件时更改图像 MouseDown会将图像更改为StartButtonDown MouseUp会将图像更改为StartButtonUp MouseEnter会将图像更改为StartButtonHover MouseLeave会将图像更改为StartBut
有什么具体的事情我应该做吗,我已经用谷歌搜索了大约一个小时,但仍然没有找到任何帮助。这里是我写的东西,它与您的要求非常相似
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);
}
}
我所做的是从标准PictureBoxt中固有的,用来制作一个ImageButton。我有三个属性,用于在不使用鼠标操作的情况下显示图像UpImage、当鼠标向下事件触发时显示的图像DownImage,以及当鼠标悬停在控件HoverImage上时显示的图像
注意,您应该为MouseUp和MouseLeave事件添加检查。如果我单击图像并将鼠标从控件上拖开,控件将再次从UpImage转到DownImage再到UpImage,因为即使我的鼠标仍处于向下状态,我仍保留了控件鼠标。当鼠标离开控件时,您可能希望向下图像保持显示。此外,当MouseUp事件发生时,您应该检查鼠标是否仍悬停在控件上。如果是,您将希望显示悬停图像,而不是UpImage
您还可以检查使用了哪个鼠标按钮。也许你只想通过点击左键来改变图像,而不是右键或中键
但这应该让你开始