David Emery

David Emery

  • NA
  • 1
  • 0

Problem with mouse events over image in picturebox

Mar 19 2010 1:58 PM

I am writing a program to draw lines over an existing image within a PictureBox. The routine works perfectly INSIDE the PictureBox (I'm drawing a rubberband line), but I cannot get the mouse to work on any buttons OUTSIDE the picturebox. (In this example, Button1 doesn't respond)
I have written three mouse event subs (MouseUP, MouseMOVE, MouseDOWN) that work within the PictureBox, but I don't know how to write the program so that I get normal mouse function when the mouse is pointing outside the PictureBox.
This MUST me easy to do... but I'm obviously missing something. I'd really appreciate some help with this.
Here is (most of) my code , some of it borrowed from various examples I've found here and there. I'm using Visual Studio 2005. (the form contains PictureBox1, TextBox1, TextBox2, TextBox3, TextBox4, Button1)
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Windows.Forms

Public Class Form1
    Public bitmap As System.Drawing.Bitmap
    Public bm As System.Drawing.Bitmap
    Public BitmapOrg As System.Drawing.Bitmap
    Private drag As Boolean = False
    Private DrawMode As Integer = 1
    Private x0, y0, x, y As Integer
    Private cx, cy As Integer
    Private gB As Graphics

    Private Sub Form1_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub
    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        Dim p As Point = New Point(e.X, e.Y)
        x0 = p.X   
        y0 = p.Y  
        TextBox1.Text = "MouseDOWN " & CStr(x0) & ", " & CStr(y0)
        drag = True
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        Dim p As Point = New Point(e.X, e.Y)
        x = p.X
        y = p.Y
        cx = x - x0
        cy = y - y0
        TextBox2.Text = "MouseMOVE " & CStr(cy) & ", " & CStr(cx)
        If drag Then
            Invalidate()
        End If
    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        cx = x - x0
        cy = y - y0
        TextBox3.Text = "MouseUP " & CStr(cy) & ", " & CStr(cx)
        Dim pen As Pen = New Pen(Color.Yellow)
        RefreshBackground()
        drag = False
        pen.Dispose()
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        bitmap = New System.Drawing.Bitmap("c:\Bitmaps\Test_A.bmp")
        Me.PictureBox1.Image = bitmap
        Dim g As Graphics = Graphics.FromImage(bitmap)
        Dim pen As Pen = New Pen(Color.Yellow)
        TextBox4.Text = CStr(drag.ToString)
        If drag Then
            g.DrawLine(pen, x0, y0, x, y)
        End If
        pen.Dispose()
    End Sub

    Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        MsgBox("Here at Button 1")
    End Sub

    Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.SizeChanged
        RefreshBackground()
    End Sub

    Private Sub RefreshBackground()
        Dim sz As Size = Me.Size
        Dim rt As Rectangle = New Rectangle(0, 0, sz.Width, sz.Height)
        Dim bm As Bitmap
        If (bm IsNot Nothing) Then
            bm = bitmap.Clone(rt, bitmap.PixelFormat)
            BackgroundImage = bm
        End If
    End Sub

    Public Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
       
    End Sub
   
End Class

 

Answers (1)