Results 1 to 6 of 6

Thread: Mouse clicking

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Posts
    9

    Mouse clicking

    I'm trying to store the coordinates of where the left mouse button is clicked.
    the button will be clicked in a picture box, and then these coordinates will be used to draw a rectangle on the screen.
    so far i know that i can use e.X and e.Y to get the position.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    VB Code:
    1. Dim Coordinates As Point
    2.  
    3.     Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    4.         If e.Button.Left Then
    5.  
    6.             Coordinates = New Point(e.X, e.Y)
    7.             'or
    8.             Coordinates = New Point(MousePosition.X, MousePosition.Y)
    9.         End If
    10.     End Sub

  3. #3
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    something like this... too many flickers though
    VB Code:
    1. Private startX, startY As Integer
    2.     Private isDragging As Boolean = False
    3.     Private Sub picBoard_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picBoard.MouseDown
    4.         startX = e.X
    5.         startY = e.Y
    6.         isDragging = True
    7.     End Sub
    8.  
    9.     Private Sub picBoard_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picBoard.MouseMove
    10.         If isDragging Then
    11.             Dim gr As Graphics = picBoard.CreateGraphics
    12.             gr.Clear(picBoard.BackColor)
    13.  
    14.             Dim rect As New Rectangle(startX, startY, e.X - startX, e.Y - startY)
    15.             gr.FillRectangle(Brushes.Blue, rect)
    16.             gr.DrawRectangle(Pens.Red, rect)
    17.  
    18.             gr.Dispose()
    19.         End If
    20.     End Sub
    21.  
    22.     Private Sub picBoard_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picBoard.MouseUp
    23.         isDragging = False
    24.     End Sub

    picBoard is the picturebox
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Posts
    9
    I get an error from this code:

    Private Sub picBoard_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picImage.MouseMove
    If isDragging Then
    Dim gr As Graphics = picImage.CreateGraphics
    gr.Clear(picImage.BackColor)

    Dim rect As New Rectangle(startX, startY, e.X - startX, e.Y - startY)
    gr.DrawRectangle(Pens.Red, rect)
    gr.FillRectangle(Brushes.Transparent, rect)
    Dim bmpCropped As New Bitmap(e.X - startX, e.Y - startY)

    Dim grBitmap As Graphics = Graphics.FromImage(bmpCropped)

    grBitmap.DrawImage(picImage.Image, 0, 0, rect, _
    GraphicsUnit.Pixel)
    picImage.Image = bmpCropped
    picImage.SizeMode = PictureBoxSizeMode.Normal
    gr.Dispose()
    End If
    End Sub


    This is supposed to crop the image from the rectangle drawn.
    the error is from this line of code because it accepts invalid inputs i think.

    Dim bmpCropped As New Bitmap(e.X - startX, e.Y - startY)

  5. #5
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by ryans
    the error is from this line of code because it accepts invalid inputs i think.

    Dim bmpCropped As New Bitmap(e.X - startX, e.Y - startY)
    what is the error message first of all?
    second, I would guess that e.X is smaller than startX, or the same thing about e.y (meaning you dragged the mouse up, or left, while that code only works when you drag the mouse down or to the right). If it's the case they you're basically asking .NET to create a bitmap with a negative size, which doesnt make sense... you just have to switch the variables around depending on what the user does
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Posts
    9

    Error message

    Originally posted by MrPolite
    what is the error message first of all?
    second, I would guess that e.X is smaller than startX, or the same thing about e.y (meaning you dragged the mouse up, or left, while that code only works when you drag the mouse down or to the right). If it's the case they you're basically asking .NET to create a bitmap with a negative size, which doesnt make sense... you just have to switch the variables around depending on what the user does
    This is the error message given when i try and draw a rectangle

    An unhandled exception of type 'System.ArgumentException' occurred in system.drawing.dll

    Additional information: Invalid parameter used.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width