Connect with Expertise | Find Experts, Get Answers, Share Insights

Problem with mouse events on form with PictureBox

 
Join Date: Mar 2010
Posts: 5
#1: Mar 19 '10
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)

Expand|Select|Wrap|Line Numbers
  1. Imports System.Drawing
  2. Imports System.Drawing.Imaging
  3. Imports System.Windows.Forms 
  4. Public Class Form1
  5.     Public bitmap As System.Drawing.Bitmap
  6.     Public bm As System.Drawing.Bitmap
  7.     Public BitmapOrg As System.Drawing.Bitmap
  8.     Private drag As Boolean = False
  9.     Private DrawMode As Integer = 1
  10.     Private x0, y0, x, y As Integer
  11.     Private cx, cy As Integer
  12.     Private gB As Graphics 
  13.  
  14.     Private Sub Form1_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
  15.     End Sub 
  16.  
  17.     Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
  18.         Dim p As Point = New Point(e.X, e.Y)
  19.         x0 = p.X    
  20.         y0 = p.Y   
  21.         TextBox1.Text = "MouseDOWN " & CStr(x0) & ", " & CStr(y0)
  22.         drag = True
  23.     End Sub 
  24.     Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
  25.         Dim p As Point = New Point(e.X, e.Y)
  26.         x = p.X
  27.         y = p.Y
  28.         cx = x - x0
  29.         cy = y - y0
  30.         TextBox2.Text = "MouseMOVE " & CStr(cy) & ", " & CStr(cx)
  31.         If drag Then
  32.             Invalidate()
  33.         End If
  34.     End Sub 
  35.     Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
  36.         cx = x - x0
  37.         cy = y - y0
  38.         TextBox3.Text = "MouseUP " & CStr(cy) & ", " & CStr(cx)
  39.         Dim pen As Pen = New Pen(Color.Yellow)
  40.         RefreshBackground()
  41.         drag = False
  42.         pen.Dispose()
  43.     End Sub 
  44.     Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
  45.         bitmap = New System.Drawing.Bitmap("c:\Bitmaps\Test_A.bmp")
  46.         Me.PictureBox1.Image = bitmap
  47.         Dim g As Graphics = Graphics.FromImage(bitmap)
  48.         Dim pen As Pen = New Pen(Color.Yellow)
  49.         TextBox4.Text = CStr(drag.ToString)
  50.         If drag Then
  51.             g.DrawLine(pen, x0, y0, x, y)
  52.         End If
  53.         pen.Dispose()
  54.     End Sub 
  55.     Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  56.         MsgBox("Here at Button 1")
  57.     End Sub 
  58.     Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.SizeChanged
  59.         RefreshBackground()
  60.     End Sub 
  61.     Private Sub RefreshBackground()
  62.         Dim sz As Size = Me.Size
  63.         Dim rt As Rectangle = New Rectangle(0, 0, sz.Width, sz.Height)
  64.         Dim bm As Bitmap
  65.         If (bm IsNot Nothing) Then
  66.             bm = bitmap.Clone(rt, bitmap.PixelFormat)
  67.             BackgroundImage = bm
  68.         End If
  69.     End Sub 
  70.     Public Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
  71.  
  72.     End Sub
  73.  
  74. End Class

tlhintoq's Avatar
E
C
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 3,476
#2: Mar 19 '10

re: Problem with mouse events on form with PictureBox


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.
tlhintoq's Avatar
E
C
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 3,476
#3: Mar 19 '10

re: Problem with mouse events on form with PictureBox


Expand|Select|Wrap|Line Numbers
  1. Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
  2.         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.

Expand|Select|Wrap|Line Numbers
  1. Private Sub RefreshBackground()
  2.         Dim sz As Size = Me.Size
  3.         Dim rt As Rectangle = New Rectangle(0, 0, sz.Width, sz.Height)
  4.         Dim bm As Bitmap
  5.         If (bm IsNot Nothing) Then
  6.             bm = bitmap.Clone(rt, bitmap.PixelFormat)
  7.             BackgroundImage = bm
  8.         End If
  9.     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.DoEvents() at the end of your Form1_Paint so you can give some CPU time to other things, like other button clicks.
 
Join Date: Mar 2010
Posts: 5
#4: Mar 19 '10

re: Problem with mouse events on form with PictureBox


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(ByVal sender As Object, ByVal e As_ System.EventArgs) Handles Button1.Click
MsgBox("Here at Button 1")
End Sub

Anyway... many thanks for the assistance!

--Dave Emery
Reply

Tags
mouse event, picturebox