开发者

Change background of windows button in mousehold event in C#

I have a image for a button. 开发者_开发百科I've used flat button for the image. By default in standard button when you click or hold the mouse over the button the background image of the windows button changes. But I want to change the background image in mouse hold event.

I am using visual studio 2008.


Use the MouseDown and MouseUp events to change the background back and forth:

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!");
}


Create a ImagemList with 2 images.

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


I guess this will be very complicated to do if you use winforms - and as you tagged it as winforms, you probably do that. I think in winforms you would have to define your own control to achieve this and it will cost you a lot of time and nerves. Alternatively, u could use the mousedown and mouseup events, but that method is not very flexible if you want to change something else.

However, if you do not need to use winforms, but you can also use WPF, there are several possibilities there, because WPF is designed exactly for that. You can define your own styles and templates to change the visual appearance of your control. To change the color of the mouse hold event, you can use triggers. See the following page for details about templates: click

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜