[英]Mouse events on picturebox?
我有一個帶有已制作的自定義圖像的“開始”按鈕。 我沒有弄錯C#.net的這一部分,但是我對VB.NET有所了解。
我見過人們有一些東西,比如public void picturebox_MouseDown()
東西,但是似乎都沒有用。 我正在嘗試給定鼠標事件時更改圖像。
MouseDown
會將圖像更改為StartButtonDown
MouseUp
會將圖像更改為StartButtonUp
MouseEnter
會將圖像更改為StartButtonHover
MouseLeave
會將圖像更改為StartButtonUp
有什么我應該做的特別的事情,我已經用Google搜索了大約一個小時,但仍然找不到任何可以幫助我的東西。
這是我寫的,與您的要求非常相似。
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);
}
}
我所做的工作是從標准PictureBox
繼承而來的,以制作一個ImageButton
。 我有三個屬性的Image
與沒有鼠標動作(UpImage)時,顯示Image
時MouseDown事件被觸發(DownImage)來顯示,並且Image
當鼠標懸停在控制(HoverImage)來顯示。
請注意,您應該添加對MouseUp和MouseLeave事件的檢查。 如果單擊圖像並將鼠標拖離控件,則控件將再次從UpImage轉到DownImage,再到UpImage,因為即使我的鼠標仍處於按下狀態,我也已離開控件(MouseLeave)。 您可能希望當鼠標離開控件時,DownImage保持顯示。 此外,發生MouseUp事件時,應檢查鼠標是否仍懸停在控件上。 如果是這樣,您將要顯示HoverImage而不是UpImage。
您還可以檢查使用了哪個鼠標按鈕。 也許您只希望通過單擊鼠標左鍵而不是右鍵或中間鍵來更改圖像。
但這應該可以幫助您入門。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.