Skip to main content
 首页 » 编程设计

c#之在鼠标悬停时重新定位图像

2024年02月13日12birdshome

我正在尝试在 visual studio 2012 上使用 c# 重新创建一个 mac 风格的菜单栏。我可以让图像在 mouseEnter 上移动并重新定位回 mouseLeave 上的原始位置。我通过设置图片框的位置来做到这一点。

我遇到的问题是当我用鼠标输入图像时,如果我将鼠标留在旧图像位置底部和新图像位置底部之间的图像底部区域,图像将在两个位置。

谁能建议如何阻止或避免这种情况。

private void pic1_MouseEnter(object sender, EventArgs e) 
{ 
    pic1.Location = new Point(328, 300); 
} 
 
private void pic1_MouseLeave(object sender, EventArgs e) 
{ 
    pic1.Location = new Point(328, 316); 
} 

请您参考如下方法:

尝试在移动图像位置之前分离事件处理程序,然后再将其附加回去。像这样:

private void pic1_MouseEnter(object sender, EventArgs e) 
{ 
    pic1.MouseEnter -= pic1_MouseEnter; 
    pic1.MouseLeave -= pic1_MouseLeave; 
 
    pic1.Location = new Point(328, 300); 
 
    pic1.MouseEnter += pic1_MouseEnter; 
    pic1.MouseLeave += pic1_MouseLeave; 
}