在VB中动画Picturebox

时间:2015-04-10 02:02:43

标签: vb.net loops events

我是VB的新手,只是无法弄清楚如何使用MouseHover事件为按钮设置动画。

我想为项目中的所有按钮(图片框)创建一个循环,当用户将鼠标放在按钮上时,该循环会增加按钮的大小。

可能是这样的:

For Each Form As Form In Application.OpenForms
      For Each Control As Control In Form.Controls

韩国社交协会。任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:0)

使用Inherits为您创建一个新按钮(或PictureBox)类。这是代码。

Public Class cuteButton
Inherits System.Windows.Forms.Button

Protected Overrides Sub OnMouseHover(e As EventArgs)
    '
    'Wite code here to change button size or whatever.
    '

    MyBase.OnMouseHover(e)
End Sub

End Class

答案 1 :(得分:0)

一种非常简单的方法是使用常见的MouseHover事件来增加按钮(我猜它实际上是一个带有图像的Picturebox):

Private Sub CustomButton_Grow(sender As Object, e As System.EventArgs) Handles Picturebox1.MouseHover, Picturebox2.MouseHover
   'Set a maximum height to grow the buttons to.
   'This can also be set for width depending on your needs!
   Dim maxHeight as single = 50

   If sender.Height < maxHeight then
      sender.Size = New Size(sender.Width+1,sender.Height+1)
   End if
End Sub

然后,您可以使用MouseLeave事件快速重置所有按钮。如果您希望该部分也是动画的,那么您可以使用全局缩小例程来不断缩小所有按钮,但MouseHover中的按钮除外。祝你好运!

答案 2 :(得分:0)

即使您有10,000个按钮或图片框,这也会有效....

我假设你只有1个表格和许多按钮,,,你必须具体针对你的问题

此代码适用于按钮,图片框,文本框

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    For Each cntrl As Control In Controls  ' Looping for each Button as Control
        If TypeOf cntrl Is Button Then
            AddHandler cntrl.MouseEnter, AddressOf cntrl_MouseEnter ' Adding the event and handler
            AddHandler cntrl.MouseLeave, AddressOf cntrl_MouseLeave ' Adding the event and handler

        End If
    Next
End Sub
     '' Assuming you wanna eNlarge everytime the mouse Hover or Enter 

Private Sub cntrl_MouseEnter(sender As Object, e As EventArgs)
    CType(sender, Button).Size = New Point(CType(sender, Button).Size.Width + 50, CType(sender, Button).Size.Height + 50)
End Sub
     '' Here it goes back normal size
Private Sub cntrl_MouseLeave(sender As Object, e As EventArgs)
    CType(sender, Button).Size = New Point(CType(sender, Button).Size.Width - 50, CType(sender, Button).Size.Height - 50)
End Sub