PictureBox MouseEnter / MouseLeave事件未触发

时间:2013-06-06 20:12:50

标签: vb.net winforms

创建一个包含三个图片框的新表单。此代码用于在鼠标进入图片框时绘制边框,并在其离开时将其删除。结果不一致。有时它会绘制/移除边框,有时则不会。这段代码并不复杂。使用VS 2012。

Private Sub PictureBox_MouseEnter(sender As Object, e As EventArgs) _
    Handles PictureBox1.MouseEnter, PictureBox2.MouseEnter, PictureBox3.MouseEnter
    Dim pb As PictureBox = DirectCast(sender, PictureBox)
    pb.BorderStyle = BorderStyle.FixedSingle
    ' Debug.WriteLine("E " & pb.Name)
End Sub

Private Sub PictureBox_MouseLeave(sender As Object, e As EventArgs) _
    Handles PictureBox1.MouseLeave, PictureBox2.MouseLeave, PictureBox3.MouseLeave

    Dim pb As PictureBox = DirectCast(sender, PictureBox)
    pb.BorderStyle = BorderStyle.None
    ' Debug.WriteLine("X " & pb.Name)
End Sub

3 个答案:

答案 0 :(得分:1)

我也可以重现这个问题。因此,扩展上面关于“绘制其他东西”的评论而不是使用Picturebox的属性,让我建议这种快速而肮脏的方法:

  • 使用RectangleShape对象,即VisualBasic Powerpack 3.0插件提供的对象。只需将其中一个以PictureBox所在的相同形式放入,并使其不可见(可见= false)。

  • 代码也很简单:

    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.RectangleShape1.Location = New Point(Me.PictureBox1.Left - 1, Me.PictureBox1.Top - 1)
            Me.RectangleShape1.Size = New Size(Me.PictureBox1.Width + 1, Me.PictureBox1.Height + 1)
        End Sub
    
        Private Sub PictureBox1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
            Me.RectangleShape1.Visible = True
        End Sub
        Private Sub PictureBox1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
            Me.RectangleShape1.Visible = False
        End Sub
    End Class
    

答案 1 :(得分:0)

需要来自Form MouseEnter事件的帮助..

Dim pb As PictureBox = New PictureBox

Private Sub Form1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
   pb.BorderStyle = BorderStyle.None
End Sub

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove

   pb = PictureBox1
   pb.BorderStyle = BorderStyle.FixedSingle

End Sub

答案 2 :(得分:0)

我按照KalaNag的想法将我的图片框放在面板中并通过这样做来处理pciturebox上的事件

private void PictureBox_MouseEnter(object sender, EventArgs e)
    {
        PictureBox control = sender as PictureBox;

        (control.Parent as Panel).Width = 20;
        (control.Parent as Panel).Height = 20;
        (control.Parent as Panel).BorderStyle = BorderStyle.Fixed3D;

    }

    private void PictureBox_MouseLeave(object sender, EventArgs e)
    {
        PictureBox control = sender as PictureBox;

        (control.Parent as Panel).Width = 18;
        (control.Parent as Panel).Height = 18;
        (control.Parent as Panel).BorderStyle = BorderStyle.None;

    }

我更改了控件的大小,否则,当光标进入和离开时,当鼠标悬停边框时,图片框会一直闪烁,因为边框会改变控件的大小。

像魅力一样!

相关问题