Difference between revisions of "Dragging windows"
m (Added declarations) |
(Changed to use PostMessage (as recomended by MSDN)) |
||
(6 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
+ | {{VB6}} | ||
So, you want to drag a window without using the real title bar? | So, you want to drag a window without using the real title bar? | ||
Line 6: | Line 7: | ||
Declare Function ReleaseCapture Lib "user32" () As Long | Declare Function ReleaseCapture Lib "user32" () As Long | ||
− | Declare Function | + | Declare Function PostMessage Lib "user32" Alias "PostMessageA" _ |
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _ | (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _ | ||
lParam As Any) As Long | lParam As Any) As Long | ||
Line 12: | Line 13: | ||
Const WM_NCLBUTTONDOWN = &HA1 | Const WM_NCLBUTTONDOWN = &HA1 | ||
Const HTCAPTION = 2 | Const HTCAPTION = 2 | ||
− | + | ||
+ | And when you want to do the dragging itself: | ||
+ | |||
ReleaseCapture | ReleaseCapture | ||
− | + | PostMessage 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. | This should go in the mousedown event of the control/object you want to drag the window by. | ||
Line 24: | Line 27: | ||
# It obeys the windows rules and settings on drawing windows while moving. | # 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 | + | The only problems I have found with this are: |
+ | |||
+ | # It doesn't work when you use the right mouse button (although this may be just Windows XP) | ||
+ | # You cant limit it to only moving in one direction | ||
+ | |||
+ | If you want to use this to drag a control, just replace Form.hWnd with Control.hWnd. This will NOT work for dragging windowless controls like Labels or ImageBoxes but you can still drag a windowed control by them. | ||
+ | |||
+ | == Resizing windows == | ||
+ | |||
+ | A small variation of this code can be used to resize forms and controls by just replacing '''HTCAPTION''' with '''HTBOTTOMRIGHT''': | ||
+ | |||
+ | Const HTBOTTOMRIGHT = 17 | ||
+ | |||
+ | ReleaseCapture | ||
+ | PostMessage Form.hWnd, WM_NCLBUTTONDOWN, HTBOTTOMRIGHT, ByVal 0& | ||
− | + | == See also == | |
− | * | + | * {{API link|ReleaseCapture}} |
Latest revision as of 12:06, 13 October 2009
This article is based on Visual Basic 6. Find other Visual Basic 6 articles. |
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 PostMessage Lib "user32" Alias "PostMessageA" _ (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _ lParam As Any) As Long Const WM_NCLBUTTONDOWN = &HA1 Const HTCAPTION = 2
And when you want to do the dragging itself:
ReleaseCapture PostMessage 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:
- It's faster and rarely suffers from flickering.
- It's easier. Two lines once compared to a huge routine (or two?)
- It obeys the windows rules and settings on drawing windows while moving.
The only problems I have found with this are:
- It doesn't work when you use the right mouse button (although this may be just Windows XP)
- You cant limit it to only moving in one direction
If you want to use this to drag a control, just replace Form.hWnd with Control.hWnd. This will NOT work for dragging windowless controls like Labels or ImageBoxes but you can still drag a windowed control by them.
Resizing windows
A small variation of this code can be used to resize forms and controls by just replacing HTCAPTION with HTBOTTOMRIGHT:
Const HTBOTTOMRIGHT = 17
ReleaseCapture PostMessage Form.hWnd, WM_NCLBUTTONDOWN, HTBOTTOMRIGHT, ByVal 0&