Problem with mouse events on form with PictureBox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dfemery
    New Member
    • Mar 2010
    • 5

    Problem with mouse events on form with PictureBox

    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)

    Code:
    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
    Last edited by tlhintoq; Mar 19 '10, 08:56 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Code:
      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")
      Every time you paint you are reading the base image from the harddrive? Ouch!
      May I suggest you read it one time at startup and use it (or a clone of it) through out the rest of the application. This is not only going to slow you down a LOT but beat the heck out of your drive.

      Code:
      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
      Once you reassign a new BackgroundImage that is going to trigger a new Form1_Paint event, and reload the file from harddrive.

      I kinda wonder if the system isn't just overwhelmed.

      You might also want to toss in an Application.DoE vents() at the end of your Form1_Paint so you can give some CPU time to other things, like other button clicks.

      Comment

      • dfemery
        New Member
        • Mar 2010
        • 5

        #4
        Problem solved....

        You are absolutely correct about the repeated disk reads... I jfollowed your advice and cloned the bitmap... it was something I should have caught but I was entirely focused on the mouse issue!

        Anyway, that was not the problem.

        The problem was a foolish error... and simple as I thought it might be! The Button1 sub that wasn't working needed a "Handles" reference!!!

        It now works fine by adding "Handles Button1.Click" so that the sub now reads:

        Private Sub button1_Click(B yVal sender As Object, ByVal e As_ System.EventArg s) Handles Button1.Click
        MsgBox("Here at Button 1")
        End Sub

        Anyway... many thanks for the assistance!

        --Dave Emery

        Comment

        Working...