2013-06-06 14 views
6

3つの画像ボックスで新しいフォームを作成します。このコードは、マウスがピクチャボックスに入ったときに境界線を描き、それが去ったときにそれを削除することを意図しています。結果には一貫性がありません。場合によっては境界線を描画/削除することもありますが、境界線を描画しないこともあります。このコードは複雑ではありません。 VSを使用する2012.PictureBox MouseEnter/MouseLeave発砲しないイベント

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 
+0

あなたの 'PictureBox'コントロールには子コントロールがありますか? – EkoostikMartin

+0

いいえ、そうではありません。新しいフォームは、上記の3つの画像ボックスとコードを追加します。 – dbasnett

+0

プラットフォームWinforms、WPF? – OneFineDay

答えて

1

私も問題を再現できます。だから、「何かを描く」の代わりに、ピクチャボックスのプロパティの使用方法についての上記のコメントに拡大し、私はこの迅速かつ汚いアプローチを提案してみましょう:

  • はRectangleShapeオブジェクト、VisualBasic Powerpack 3.0アドオンが提供するものを使用してください。単にあなたのPictureBoxが入っているのと同じフォームにそれらの1つを入れ、それを見えないようにします(visible = 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 
    
+0

OPはもう少し簡単なので、OPにはやりたくないようです.Graphicsにはいくつかの方法があります。問題は、BorderStyleを切り替えることによって問題を解決する方法です。私は1つのPictureBoxでテストしたが、それは素晴らしいが、3つのPictureBoxesで、何か奇妙な、おそらくハンスパッサントによって説明されています。 –

+0

@ KingKing上記のコメントで述べたように私は回避策があることを知っています。私は理由を知りたかった。 BorderStyleのドキュメントには、副作用の記述はありません。 「ネイティブウィンドウが破壊されて再作成されたとき」にイベントが失われた場合、なぜこれが機能しますか? – dbasnett

+0

@ KalaNag - イベントが発生していない場合、発生しているように見えますが、これは機能しません。 – dbasnett

0

は、フォームの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 
0

私はパネルの内側に私のPictureBoxを置くことによって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; 

    } 

コントロールのサイズが変更されたため、境界線がコントロールのサイズを変更するため、カーソルが無期限に出入りしているときにマウスが境界線を移動すると、ピクチャボックスがちらつきます。

魅力的な作品です!

関連する問題