【发布时间】:2020-11-01 22:02:39
【问题描述】:
我有一个按钮的图像。我为图像使用了平面按钮。默认情况下,在标准按钮中,当您单击或将鼠标悬停在按钮上时,Windows 按钮的背景图像会发生变化。但我想在鼠标按住事件中更改背景图像。
我正在使用 Visual Studio 2008。
【问题讨论】:
标签: c# visual-studio winforms
我有一个按钮的图像。我为图像使用了平面按钮。默认情况下,在标准按钮中,当您单击或将鼠标悬停在按钮上时,Windows 按钮的背景图像会发生变化。但我想在鼠标按住事件中更改背景图像。
我正在使用 Visual Studio 2008。
【问题讨论】:
标签: c# visual-studio winforms
使用MouseDown 和MouseUp 事件来回改变背景:
private void btn_MouseDown(object sender, MouseEventArgs e)
{
//Replace with the appropriate control/image/color change:
btn.BackColor = Color.Black;
}
private void btn_MouseUp(object sender, MouseEventArgs e)
{
//As mentioned above
btn.BackColor = SystemColors.Control;
//Show the MsgBox here
MessageBox.Show("The background is fine!");
}
【讨论】:
创建一个包含 2 个图像的 ImagemList。
Private Sub Button1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave
'out the button
End Sub
Private Sub Button1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove
'In the button
Button1.BackgroundImage = ImageList2.Images(1)
End Sub
【讨论】:
我想如果您使用 winforms,这将非常复杂 - 当您将其标记为 winforms 时,您可能会这样做。我认为在winforms中你必须定义自己的控件来实现这一点,这会花费你很多时间和精力。或者,您可以使用 mousedown 和 mouseup 事件,但是如果您想更改其他内容,该方法不是很灵活。
但是,如果您不需要使用 winforms,但也可以使用 WPF,则有多种可能性,因为 WPF 正是为此而设计的。您可以定义自己的样式和模板来更改控件的视觉外观。要更改鼠标按住事件的颜色,您可以使用触发器。有关模板的详细信息,请参见以下页面:click
希望对您有所帮助。
【讨论】: