Move A Form With No Title Bar/Resize Controls
This sample shows 2 ways to move a form that has no title bar. All that is
visible is the body or client area of the form. It also shows how to resize a
control by dragging its edge.
The first method of moving a form is a bit more cumbersome but illustrates one
possibility. By detecting the cursor location in the form's MouseDown event and
comparing it to its position in the MouseUp event we can determine if the form
should be moved and where to move it to. Then using the SetWindowPlacement
API we can move the form to the new location. GetCursorPos
is used to get the current position of the cursor on the screen.
The second method uses the ReleaseCapure and
SendMessage APIs and is very concise requiring only 2 lines of code.
These are the same APIs that let you resize a control by dragging one of its
edges just like you resize a window.
Download Source Code
To create a form without a title bar set the following properties:
Caption = ""
ControlBox = False
|GetCursorPos and SetWindowPlacement
This method uses two main APIs. The GetCursorPos function
to see where the form is and the SetWindowPlacement
function to move the form to the new location. GetCursorPos
uses a PointApi structure which contains elements
for the x and y coordinates of the cursor. SetWindowPlacement
uses a WindowPlacement structure which uses the
PointApi as well as Rect structures as
We start with the form load event which sets the WindowPlacement
structure's .Length property to the size of the structure and its .ShowCmd
property to SW_SHOWNORMAL. This is the action we
want to take when we move the form.
In the form's MouseDown event a call is made to GetCursorPos.
This retrieves the location of the mouse when it is pressed and saves it in a
set of global variables.
When the mouse is released, the form's MouseUp event triggers. Another call to
GetCursorPos is made to get the new coordinates of the cursor. If
the old and new values are the same, plus or minus a few pixels, the form has
not been moved. When they differ, the new location of the form is set. This is
done by setting the values of the .rcNormalPosition property of the
WindowPlacement structure. This element is of type
Rect meaning it can contain the coordinates of the four corners of
the form. Once the new location of the form is set, SetWindowPlacement
is called to move the form to its new spot.
Using the ReleaseCapture and SendMessage
functions only requires 2 lines of code (not counting the API declares).
Call SendMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
Download the source and run it. Move the cursor anywhere on the form, except
over the Quit button or the labels, and press the left mouse button. Move the
cursor to a new location and release the mouse button. The form will move to
the new location. Now click on the Hand and move the form. Release the mouse to
stop moving the form. Drag the left or right edge of the center button to