如何在按住鼠标并将光标拖离按钮时更改按钮的样式,尤其是图像?
您会注意到,在此操作中,按钮的默认行为是将其样式还原为悬停样式。这可以使用MouseOverBackColor进行部分配置。我想确保无论什么时候应用MouseOverBackColor,我都会在按钮上有一个特定的图像。
我已经尝试过以下代码,有一个" isMouseDown"在离开事件中检查的标志。但是,这对我没有用。
private void btnFormMinimize_MouseClick(object sender, MouseEventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
bool isMouseDown = false;
private void btnFormMinimize_MouseDown(object sender, MouseEventArgs e)
{
Button b = (Button)sender;
b.Image = Properties.Resources.icon_minimize_click;
isMouseDown = true;
}
private void btnFormMinimize_MouseEnter(object sender, EventArgs e)
{
Button b = (Button)sender;
b.Image = Properties.Resources.icon_minimize_hover;
}
private void btnFormMinimize_MouseLeave(object sender, EventArgs e)
{
Button b = (Button)sender;
if (isMouseDown)
{
b.Image = Properties.Resources.icon_minimize_hover;
}
else
{
b.Image = Properties.Resources.icon_minimize;
}
}
private void btnFormMinimize_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown= false;
}
提前致谢。