6

创建一个包含三个图片框的新表单。此代码旨在在鼠标进入图片框时绘制边框,并在离开时将其移除。结果不一致。有时它会绘制/删除边框,有时则不会。这段代码并不复杂。使用 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
4

3 回答 3

1

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

  • 使用由VisualBasic Powerpack 3.0插件提供的 RectangleShape 对象。只需将其中一个放在与您的 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
    
于 2013-06-07T01:51:46.983 回答
0

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

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;

    }

我更改了控件的大小,因为否则,当鼠标悬停在边框上时,图片框会不断闪烁,因为光标会无限期地进入和离开,因为边框会改变控件的大小。

奇迹般有效 !

于 2013-12-05T16:51:06.763 回答
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
于 2013-06-07T03:05:41.740 回答