MouseDown-Drag-MouseUp Screen capturing routine

M

Mark

Does anyone know where there is some sample code that explains now a user;
using the mouse can click on an image on the screen. Capture the image by
boxing it in using the mouse and then save it to a bitmap or onto another
form?

Thank you
 
C

Cor Ligthert[MVP]

Mark,

I thought that this one was forever in the 101 samples from Microsoft, maybe
you can check yourself if that is still there. Using that is your problem
quiet simple.

(The basic of this is that you save your position by mouse down to check
were it was at mouse up)

The static keyword is very fine for that.

Cor
 
M

Mark

Cor,
Thank you for pointing me in the right direction. It probably has been in
plain sight, but I wouldn't know it. The project I had in mind to help me
learn VB 2005 .NET was a simple highlight the shapes and then move them to
another location on the form.

One area of frustration for me is I can't seem to word the question in such
a way that I get back the results I need. For example: The title I used for
this thread would return everything having to do with each seperate event.
Not the combination of events needed to accomplish the task of capturing a
shape on the form so I can transfer it to another area on the screen.
 
C

Cor Ligthert[MVP]

Mark,

Maybe you can use this, I got it once from Fergus Cooney and changed it, the
purpose was to do things with images, which was then not yet in standard
programs.

It is used as a class for a form with images, however I think it is better
that you find that for yourself.

\\\
Public Class Enlarger
Private Shared EnlargerRectangle As Rectangle
Private Shared mVertical As Boolean
Private Shared imgHeight As Integer
Private Shared imgWidth As Integer
Private Shared LastMouseX As Integer
Private Shared LastMouseY As Integer
Public Shared ReadOnly Property Vertical() As Boolean
Get
Return mVertical
End Get
End Property
Public Shared ReadOnly Property Rectangle() As Rectangle
Get
Return EnlargerRectangle
End Get
End Property
Public Shared Sub Initialize(ByVal clientRectangle As Rectangle)
EnlargerRectangle = clientRectangle
imgHeight = EnlargerRectangle.Height
imgWidth = EnlargerRectangle.Width
If imgHeight < imgWidth Then
mVertical = True 'reverse because of next action
Else
mVertical = False
End If
End Sub
Public Shared Sub setOrientation()
If mVertical Then
mVertical = False
Dim testhight As Integer
If imgWidth > imgHeight * 2 / 3 Then
testhight = imgWidth * 2 / 3
Else
testhight = imgHeight
End If
Dim NewWidth = testhight * 3 / 2
Enlarger.Reset(NewWidth, testhight)
Else
'It has to be vertical
mVertical = True
Dim testwidth As Integer
If imgWidth > imgHeight * 2 / 3 Then
testwidth = imgHeight * 2 / 3
Else
testwidth = imgWidth
End If
Dim NewHight = testwidth * 3 / 2
Enlarger.Reset(testwidth, NewHight)
End If
End Sub
Private Shared Sub Reset(ByVal width As Integer, ByVal hight As Integer)
EnlargerRectangle.Location = New Point(0, 0)
EnlargerRectangle.Width = width
EnlargerRectangle.Height = hight
End Sub
Public Shared Sub move(ByVal curMouseX As Integer, ByVal curMouseY As
Integer, ByVal imgTopLeft As Point)
Dim newX As Integer = EnlargerRectangle.X
Dim newY As Integer = EnlargerRectangle.Y
Dim newHeight As Integer = EnlargerRectangle.Height
Dim newWidth As Integer = EnlargerRectangle.Width
If (Control.ModifierKeys And Keys.Control) Then
newWidth = EnlargerRectangle.Width + (curMouseX - LastMouseX)
If newWidth < 10 Then
newWidth = 10
End If
If newWidth > imgWidth Then
newWidth = imgWidth
End If
If mVertical Then
newHeight = (newWidth * 3) \ 2
If newHeight > imgHeight Then
newHeight = imgHeight
newWidth = (newHeight * 2) \ 3
End If
Else
newHeight = (newWidth * 2) \ 3
If newHeight > imgHeight Then
newHeight = imgHeight
newWidth = (newHeight * 3) \ 3
End If
End If
Else
newX += (curMouseX - LastMouseX) 'step
newY += (curMouseY - LastMouseY) 'step
If newX < 0 Then
newX = 0
End If
If newY < 0 Then
newY = 0
End If
If newX + EnlargerRectangle.Width > imgWidth Then
newX = imgWidth - EnlargerRectangle.Width
End If
If newY + EnlargerRectangle.Height > imgHeight Then
newY = imgHeight - EnlargerRectangle.Height
End If
End If
EnlargerRectangle = New Rectangle(newX, newY, newWidth, newHeight)
Mousedown(imgTopLeft)
End Sub
Public Shared Sub Mousedown(ByVal imgTopLeft As Point)
LastMouseX = EnlargerRectangle.X + EnlargerRectangle.Width - 30
LastMouseY = EnlargerRectangle.Y + EnlargerRectangle.Height - 30
Cursor.Position = New Point(imgTopLeft.X + LastMouseX, imgTopLeft.Y
+ LastMouseY)
End Sub
End Class
///

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top