Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drag and drop form from anywhere 1

Status
Not open for further replies.

neseidus

Programmer
Sep 1, 2004
41
CA
Hi

I found some code on the net that allows the user to move the form by clicking on it anywhere... Here it is:

Code:
Const WM_NCLBUTTONDOWN = &HA1

Public Sub MoveForm(ByRef frmWhich As Form)

' Used to move forms without title bars (put in
' MouseDown event of form)

ReleaseCapture
SendMessage frmWhich.hWnd, WM_NCLBUTTONDOWN, 2, 0&

End Sub

So I just call MoveForm Me in the mousedown event.
This works great but only with the left mouse button... I need to have it do this with the right button only.

I tried changing the WM_NCLBUTTONDOWN to &HA4 (the value for the right button down message) but this didn't work for some reason

Any help appreciated

-Steve
 
Here is a Non API method to do this

Code:
Option Explicit

Private mblnMouseDown As Boolean
Private msngX As Single
Private msngY As Single


Private Sub UserForm_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Button = 2 Then
    mblnMouseDown = True
    msngX = X
    msngY = Y
End If
End Sub

Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If mblnMouseDown Then
    Me.Left = Me.Left + (X - msngX)
    Me.Top = Me.Top + (Y - msngY)
End If
End Sub

Private Sub UserForm_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    mblnMouseDown = False
End Sub

OR just use it in the Userform like

Code:
Private Sub UserForm_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'// allows us to move the Captionless form, we NEED THIS!
    Const WM_NCLBUTTONDOWN = &HA1
    Const HTCAPTION = 2
    If Button = 2 Then
        ReleaseCapture
        SendMessage frmHdl, WM_NCLBUTTONDOWN, HTCAPTION, 0&
    End If
End Sub



Ivan F Moala
xcelsmall.bmp
 
Hey

Your second suggestion doesn't work for some reason, but I don't know why I didn't think of your first suggestion =)

Thanks
 
Hmmm well I can't seem to get it to move at the same rate as the mouse using your method, and also it flickers terribly

With the API call it doesn't flicker at all, so this would be much preferred

Any other ideas?

Thanks again
 
My current solution is to just have the user hold control down to drag-drop but this is kind of unintuitive
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top