Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[VB6] Moving form when clicking and dragging picture box

162 views
Skip to first unread message

Matthe...@gmail.com

unread,
Jan 21, 2006, 9:44:26 PM1/21/06
to
So, I'm using mousedown to determine the mouse button is clock,
mousemove on the picturebox to determine the current mouse position
and I would like to move the form to follow the mouse.


I figured that the following code in mousemove would work:

Me.Left = X
Me.Top = Y


It is now obvious that left and top are not designed to handle this
situation, as the redraw is horrendous, and the form barely gets to
where my mouse is.


Does anyone have another suggestion?


Thank you very much,

Matt

Lance Wynn

unread,
Jan 21, 2006, 11:34:53 PM1/21/06
to
This code seems to do what you want.


Option Explicit
Private lRelOffsetLeft As Long
Private lRelOffsetTop As Long

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As
Single, Y As Single)
If Button = vbLeftButton Then
lRelOffsetLeft = X + Picture1.Left
lRelOffsetTop = Y + Picture1.Top
End If
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
Dim newLeft As Long
Dim newTop As Long
If Button = vbLeftButton Then
newLeft = (X + Me.Left + Picture1.Left) - lRelOffsetLeft
newTop = (Y + Me.Top + Picture1.Top) - lRelOffsetTop
Me.Move newLeft, newTop

End If
End Sub

<Matthe...@gmail.com> wrote in message
news:1137897866.8...@z14g2000cwz.googlegroups.com...

Matthe...@gmail.com

unread,
Jan 22, 2006, 12:53:31 AM1/22/06
to
Thanks!

That's awesome! Smoooth...


Also thanks for the proper coding. I code very slopily.

The whole vbleftbutton thing will help me write up a context menu!

Any ideas on that?


Thanks,

Matt

Randy Birch

unread,
Jan 22, 2006, 1:35:27 AM1/22/06
to
Private Const HTCAPTION As Long = 2
Private Const WM_NCLBUTTONDOWN = &HA1

Private Declare Function ReleaseCapture Lib "user32" () As Long

Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long


Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As
Single, Y As Single)

If Button = vbLeftButton Then

ReleaseCapture
SendMessage Me.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, ByVal 0&
End If

End Sub

... and, if you want to drag the picturebox around the form, change the hwnd
...


Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As
Single, Y As Single)

If Button = vbLeftButton Then

ReleaseCapture
SendMessage Picture1.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, ByVal 0&
End If

End Sub


--

Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/

Please reply to the newsgroups so all can participate.


<Matthe...@gmail.com> wrote in message
news:1137897866.8...@z14g2000cwz.googlegroups.com...

: So, I'm using mousedown to determine the mouse button is clock,

:

Lance Wynn

unread,
Jan 22, 2006, 1:59:42 AM1/22/06
to
I don't know about "Proper Coding", but it works. Randy's post is much
smoother, and it behaves a lot more like windows, and you don't need to
track any of the locations in Modular variables. I'd go with his method,
much more professional results.

Lance
<Matthe...@gmail.com> wrote in message
news:1137909211.3...@o13g2000cwo.googlegroups.com...

Matthe...@gmail.com

unread,
Jan 22, 2006, 11:44:24 AM1/22/06
to
Thansk Randy. Lance, yours was totally fine, the only draw back was
that I was adjusting the size of the form to "shrink" to the size of
the picture, and I was running into issues with that.

Randy's solution solves my problem.


Thanks for all your help guys!

Matt

David J Mark

unread,
Jan 23, 2006, 7:59:00 AM1/23/06
to
You want the form to move when you drag the picture box? Here is an example
to do this without the picture. It can easily be changed to use a picture
box if you really want to use one.

You will have to look up the API declarations...


Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single,
y As Single)
On Error Resume Next

If Button = vbLeftButton Then
ReleaseCapture

SendMessage hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
End If
End Sub


<Matthe...@gmail.com> wrote in message
news:1137897866.8...@z14g2000cwz.googlegroups.com...

Rick Rothstein [MVP - Visual Basic]

unread,
Jan 23, 2006, 9:59:02 AM1/23/06
to
> You want the form to move when you drag the picture box? Here is an
example
> to do this without the picture. It can easily be changed to use a picture
> box if you really want to use one.
>
> You will have to look up the API declarations...
>
> Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As
Single,
> y As Single)
> On Error Resume Next
>
> If Button = vbLeftButton Then
> ReleaseCapture
> SendMessage hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
> End If
> End Sub

Too bad Randy didn't think to post that same code (with the API
declarations) yesterday, huh?<g>

Rick


Matthe...@gmail.com

unread,
Jan 29, 2006, 12:01:43 PM1/29/06
to
yea, too bad Randy didn't do that.

0 new messages