Difference between revisions of "Dragging windows"

From HashVB
Jump to: navigation, search
(Moved into the wiki)
 
m (Added declarations)
Line 5: Line 5:
 
Until I found the following code:
 
Until I found the following code:
  
 +
Declare Function ReleaseCapture Lib "user32" () As Long
 +
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
 +
  (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
 +
  lParam As Any) As Long
 +
 +
Const WM_NCLBUTTONDOWN = &HA1
 +
Const HTCAPTION = 2
 +
 
  ReleaseCapture
 
  ReleaseCapture
 
  SendMessage Form.hWnd, WM_NCLBUTTONDOWN, HTCAPTION, ByVal 0&
 
  SendMessage Form.hWnd, WM_NCLBUTTONDOWN, HTCAPTION, ByVal 0&

Revision as of 14:06, 3 November 2005

So, you want to drag a window without using the real title bar?

I have seen lots of people using their own code in the mouse move event to do this, and I've even done it myself.

Until I found the following code:

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

Const WM_NCLBUTTONDOWN = &HA1
Const HTCAPTION = 2

ReleaseCapture
SendMessage Form.hWnd, WM_NCLBUTTONDOWN, HTCAPTION, ByVal 0&

This should go in the mousedown event of the control/object you want to drag the window by.

It has various advantages over home grown code:

  1. It's faster and rarely suffers from flickering.
  2. It's easier. Two lines once compared to a huge routine (or two?)
  3. It obeys the windows rules and settings on drawing windows while moving.

If you want to use this to drag a control, just replace Form.hWnd with Control.hWnd. This will NOT work with windowless controls like Labels or ImageBoxes.

See also