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

Moving captionless forms

Status
Not open for further replies.

AtomicChip

Programmer
May 15, 2001
622
0
0
CA
I currently have a captionless form that has an image (Say, Image1) within it, which is maximized to the borders of the form. How would I be able to move the form based on the mouse events of Image1?
 
Yes, it can be done, just by using some API's. Initiate dragging in the image's mousedown event and stop dragging in the image's mouseup event.

Copy this in your code:

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 Declare Function ReleaseCapture Lib _
"user32" () As Long
Private Declare Function GetCursorPos Lib _
"user32" (lpPoint As POINTAPI) As Long

Private Type POINTAPI
X As Long
Y As Long
End Type

' Used to support captionless drag.
Private Const WM_NCLBUTTONDOWN = &HA1
Private Const HTCAPTION = 2

Private Sub Image1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
' Allow captionless drag.
If Button = vbLeftButton Then
Call ReleaseCapture
Call SendMessage(Me.hWnd, _
WM_NCLBUTTONDOWN, HTCAPTION, ByVal 0&)
End If

End Sub

Private Sub Image1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim pt As POINTAPI

' This is relative to the screen, so we can't
' use the coordinates passed in the event.
Call GetCursorPos(pt)

End Sub


Success, Herman :-Q
 
Seems like a lot of work....:
Option Explicit
Dim StartX As Single
Dim StartY As Single

Private Sub Image1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
StartX = X
StartY = Y
End Sub

Private Sub Image1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Form1.Left = Form1.Left - StartX + X
Form1.Top = Form1.Top - StartY + Y
End Sub Tim

Remember the KISS principle:
Keep It Simple, Stupid!
 
Yes Tim,

your solution is simpler. However I like mine better. Why? Because it does what you expect. While dragging in a Windows environment you'll expect a dashed rectangular form to see what you're dragging where to. Your solution does the dragging but you don't see anything moving until you release the mousebutton. I think it will confuse users.
So, I'd say Keep It Standard, Stupid (it's another KISS).

Herman :-Q
 
OK! got me there. Try
Option Explicit
Dim StartX As Single
Dim StartY As Single

Private Sub Image1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
StartX = X
StartY = Y
End Sub

Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then 'left button
Form1.Left = Form1.Left - StartX + X
Form1.Top = Form1.Top - StartY + Y
End If
End Sub Tim

Remember the KISS principle:
Keep It Simple, Stupid!
 
IMHO, Herman's solution is the better one as, like they he says, it's a proper windows drag and therefore does all the correct stuff. Tim's is an emulation at best (e.g switch off "show windows contents when dragging" in Diplay Properties, and spot the difference)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top