Cursor position and draw rectangle on desktop...

lidds

Junior Contributor
Joined
Nov 9, 2004
Messages
210
Hi,

I have been looking for ages on google but with no luck. What I am doing is trying to add a screen capture facility to my app. I have written the code to capture a region on my computer depending on xy values that I have entered just to test the code, and this works. But what I want to be able to do is the user to click a button and then select one point and then a second point on the screen and to get the cursor positions when the mouse is clicked in both locations.

What I am having the problem doing is stopping my script from recording the cursor positions when the user clicks the button. In other words I want the user to click the button to create a screen capture then need the app to wait for 2 mouse clicks before proceeding through the rest of the code.

Visual Basic:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim SC As New ScreenShot.ScreenCapture

        Dim x1 As Integer
        Dim y1 As Integer
        Dim x2 As Integer
        Dim y2 As Integer

        ' Need some code here to wait for a mouse click before doing code below
        x1 = Cursor.Current.Position.X
        y1 = Cursor.Current.Position.Y

        ' Need some code here to wait for second mouse click before doing code below
        x2 = Cursor.Current.Position.X
        y2 = Cursor.Current.Position.Y

        'At the moment I have just entered in positions to test
        Dim MyBitMap As Bitmap = SC.CaptureDeskTopRectangle(New Rectangle(0, 0, _
                                     300, 300), 300, 300)
        MyBitMap.Save("c:\desktopregion.jpg", Imaging.ImageFormat.Jpeg)

    End Sub

Thanks

Simon
 
What about...
Visual Basic:
private _numberOfMouseClicks As Integer = 0  ''Keeps a tally of mouse clicks so you know if you're on the first or second click
private _x1 As Integer = 0
private _y1 As Integer = 0
private _x2 As Integer = 0
private _y2 As Integer = 0

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me._numberOfMouseClicks += 1   ''you clicked, so count it

        Dim SC As New ScreenShot.ScreenCapture

        ' Need some code here to wait for a mouse click before doing code below
      If Me._numberOfMouseClicks = 1 Then
        Me._x1 = Cursor.Current.Position.X
        Me._y1 = Cursor.Current.Position.Y
     Else If Me._numberOfMouseClicks = 2
        ' Need some code here to wait for second mouse click before doing code below
        Me._x2 = Cursor.Current.Position.X
        Me._y2 = Cursor.Current.Position.Y

        'At the moment I have just entered in positions to test
        Dim MyBitMap As Bitmap = SC.CaptureDeskTopRectangle(New Rectangle(Me._x1, Me._y1, _
                                     Me._x2, Me._y2), Math.Abs(Me._x1 - Me._x2), Math.Abs(Me._y1 - Me._y2))  'Or whatever the X/Y  areguments are...  and I think that'll work for x/y distance
        MyBitMap.Save("c:desktopregion.jpg", Imaging.ImageFormat.Jpeg)
        ' reset the mouse click counter becuase we got the two clicks we want and need to get ready to capture the next rectagle capture
        Me._numberOfMouseClicks = 0
   
    End If
End Sub
 
The only problem with this is that because the code is called when the user hits the button the cursor position is always where the button is and therefore does not allow the user to identify a region.

I think the only way is for the code to wait for the user to click once then twice.

Thanks for the help

Simon
 
You could move the code into the Form_Click event:

Button Event
set the Boolean CountingClicks to true

Form_Click Event
If CountingClicks
if numClicks = 0
get x1, y1
numClicks += 1
elseif numClicks = 1
get x2, y2
grab screen
CountingClicks = false
numClicks = 0
end if
end if

But, if you want to allow the user to click somewhere off the form, then it is not going to work (there is no event to catch a mouse click off the form). I think you are then into the realms of p/invoke and mouse hooks.
 
Sorry, I wasn't paying attenting to the specific event. I was thinking about what Jo0ls suggested about the form click event.
 
Back
Top