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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with creating a User Moveable Control 2

Status
Not open for further replies.

JESTAR

Programmer
Feb 13, 2002
213
0
0
GB
I have a frame that contains several buttons, textboxes etc. At the top is a label (Label2). When the user clicks down on Label2 and moves the mouse, the Frame will hopefully follow the mouse until the user releases the button. I have this:

[tt]
Dim MoveControl as Boolean

Private Sub Label2_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
MoveControl = True
CurrentX = X
CurrentY = Y
End Sub

Private Sub Label2_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
If MoveControl Then
Select Case Index
Case 3:
frameForm.Top = Y
frameForm.Left = X
End Select
End If
End Sub

Private Sub Label2_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
MoveControl = False
End Sub
[/tt]

It doesn't work though. Any ideas?
 
Try this code, click anywhere on the frame to move it...[green]'Module

[/green][blue]Option Explicit

Public Declare Function [/blue]GetWindowLong [blue]Lib [/blue]"user32" [blue]Alias [/blue]"GetWindowLongA" ([blue]ByVal [/blue]hwnd [blue]As Long[/blue], ByVal nIndex [blue]As Long[/blue]) As Long
[blue]Public Declare Function [/blue]SetWindowLong [blue]Lib [/blue]"user32" [blue]Alias [/blue]"SetWindowLongA" ([blue]ByVal [/blue]hwnd [blue]As Long[/blue], [blue]ByVal [/blue]nIndex [blue]As Long[/blue], ByVal dwNew[blue]Long As [/blue]Long) As Long
[blue]Public Declare Function [/blue]SetWindowPos [blue]Lib [/blue]"user32" ([blue]ByVal [/blue]hwnd [blue]As Long[/blue], ByVal hWndInsertAfter [blue]As Long[/blue], [blue]ByVal [/blue]X [blue]As Long[/blue], [blue]ByVal [/blue]Y [blue]As Long[/blue], [blue]ByVal [/blue]cx [blue]As Long[/blue], ByVal cy As Long, ByVal wFlags As Long) As Long

[blue]Public Const [/blue]GWL_STYLE = (-16)
[blue]Public Const [/blue]GWL_EXSTYLE = (-20)
[blue]Public Const [/blue]WS_THICKFRAME = &H40000
[blue]Public Const [/blue]WS_EX_STATICEDGE = &H20000 '
[blue]Public Const [/blue]SWP_FRAMECHANGED = &H20
[blue]Public Const [/blue]SWP_NOMOVE = &H2
[blue]Public Const [/blue]SWP_NOZORDER = &H4
[blue]Public Const [/blue]SWP_NOSIZE = &H1

[blue]Public [/blue]DragX [blue]As Single
Public [/blue]DragY [blue]As Single

Public Sub [/blue]Reposition(ctrl [blue]As [/blue]Control, X [blue]As Single[/blue], Y [blue]As [/blue]Single)
DragX = X
DragY = Y
ctrl.Drag 1
[blue]End Sub

Public Sub [/blue]SetCtrlMobile(abc [blue]As [/blue]Control)
[blue]On Error Resume Next
[/blue]SetWindowLong abc.hwnd, GWL_STYLE, GetWindowLong(abc.hwnd, GWL_STYLE) [blue]Or [/blue]WS_THICKFRAME
SetWindowLong abc.hwnd, GWL_EXSTYLE, GetWindowLong(abc.hwnd, GWL_EXSTYLE) [blue]Or [/blue]WS_EX_STATICEDGE
SetWindowPos abc.hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE [blue]Or [/blue]SWP_NOSIZE [blue]Or [/blue]SWP_NOZORDER Or SWP_FRAMECHANGED
[blue]End Sub

[/blue][green]'Form

[/green][blue]Option Explicit

Private Sub [/blue]Form_Load()
SetCtrlMobile Frame1
[blue]End Sub

Private Sub [/blue]Form_DragDrop(Source [blue]As [/blue]Control, X [blue]As Single[/blue], Y [blue]As [/blue]Single)
Source.Move X - DragX, Y - DragY
[blue]End Sub

Private Sub [/blue]Frame1_MouseDown(Button [blue]As Integer[/blue], Shift [blue]As Integer[/blue], X [blue]As Single[/blue], Y As Single)
Reposition Frame1, X, Y
[blue]End Sub

[/blue]

 
Thanks for the code. One problem though, it allows me to move the frame, but doesn't drop it where I move to... hmm like I can click on it and drag an outline to a new place, but it won't move the actual frame there. Is there a MouseUp event too?
 
Also, it draws a thin grey border around the frame (the background is white). Is there a way around that?
 
No there's no mouseup event, if you dont move it far enough it moves back to the original position, for the grey border try playing around with or changing the WS_THICKFRAME constant
 
Thanks it works great. I was trying to move it over a large textbox but it wouldn't have it. Is there a way to allow this? Thanks again.
 
>Also, it draws a thin grey border around the frame (the background is white). Is there a way around that?

Comment out these lines like so (No Grey Line)...


[blue]Public Sub [/blue]SetCtrlMobile(abc [blue]As [/blue]Control) [blue]On Error Resume Next [/blue][green]'SetWindowLong abc.hwnd, GWL_STYLE, GetWindowLong(abc.hwnd, GWL_STYLE) Or WS_THICKFRAME 'SetWindowLong abc.hwnd, GWL_EXSTYLE, GetWindowLong(abc.hwnd, GWL_EXSTYLE) Or WS_EX_STATICEDGE [/green]SetWindowPos abc.hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE [blue]Or [/blue]SWP_NOSIZE [blue]Or [/blue]SWP_NOZORDER Or SWP_FRAMECHANGED [blue]End Sub [/blue]

I'll just point out however, if you use this code on some other controls such as textbox,command button etc, you can also resize the control as well as reposition it (The reason for the 'other' code in SetCtrlMobile Sub).
 
Thanks again, the first solution (changing the THICHFRAME thing to H80000) worked great, fitted in with the program design. The main problem now is moving the control over the huge textbox on the form. Thanks again though.
 
You'll need code in each dragdrop event for all of the controls you might want to release the mouse over. Like the code LPlates showed for the Form DragDrop event, but you'll have to modify it to allow for the position of the control on the form.

Robert
 
Thanks TheVampire. I added this as you suggested:
[tt]
Private Sub txtBox_DragDrop(Source As Control, X As Single, Y As Single)
Source.Move X - DragX, Y - DragY
End Sub[/tt]

It allows me to release the moveable frame on top of the huge textbox.

LPlates, why not put all this code in an FAQ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top