Delphi

How to provide Dragging or Resizing of any control.

The WM_SYSCOMMAND Message gives us various options for manipulating windows and replicating user input, but there are some undocumented parameters that are very useful.

The MSDN Documentation provides various usefull valid parameters that can be passed in the WParam of the message.

These are as follows:

  • SC_CLOSE – Closes the window.
  • SC_CONTEXTHELP – Changes the cursor to a question mark with a pointer. If the user then clicks a control in the dialog box, the control receives a WM_HELP message.
  • SC_DEFAULT – Selects the default item; the user double-clicked the window menu.
  • SC_HOTKEY – Activates the window associated with the application-specified hot key. The lParam parameter identifies the window to activate.
  • SC_HSCROLL – Scrolls horizontally.
  • SC_KEYMENU – Retrieves the window menu as a result of a keystroke. For more information, see the Remarks section.
  • SC_MAXIMIZE – Maximizes the window.
  • SC_MINIMIZE – Minimizes the window.
  • SC_MONITORPOWER – Sets the state of the display. This command supports devices that have power-saving features, such as a battery-powered personal computer.
    The lParam parameter can have the following values:
    1 – the display is going to low power
    2 – the display is being shut off
  • SC_MOUSEMENU – Retrieves the window menu as a result of a mouse click.
  • SC_MOVE – Moves the window.
  • SC_NEXTWINDOW – Moves to the next window.
  • SC_PREVWINDOW – Moves to the previous window.
  • SC_RESTORE – Restores the window to its normal position and size.
  • SC_SCREENSAVE – Executes the screen saver application specified in the [boot] section of the System.ini file.
  • SC_SIZE – Sizes the window.
  • SC_TASKLIST – Activates the Start menu.
  • SC_VSCROLL – Scrolls vertically.

In addition there are some undocumented constants that can be used as well.

  • SC_DragMove = $F012;
    Will start a Drag operation to move the window. This is similar tot he effect you get when you drag the caption of a winodw, however using this in the mouse down on a form, instantly gives you a form that can be dragged by clicking anywhere.
  • SC_DRAGSIZE_N = $F003;
    Starts a resize operation on the top edge of the form. The window then supports either keyboard or mouse resizing.
  • SC_DRAGSIZE_S = $F006;
    Starts a resize operation on the bottom edge of the form. The window then supports either keyboard or mouse resizing.
  • SC_DRAGSIZE_E = $F002;
    Starts a resize operation on the right edge of the form. The window then supports either keyboard or mouse resizing.
  • SC_DRAGSIZE_W = $F001;
    Starts a resize operation on the left edge of the form. The window then supports either keyboard or mouse resizing.
  • SC_DRAGSIZE_NW = $F004;
    Starts a resize operation on the Top left corner of the form. The window then supports either keyboard or mouse resizing.
  • SC_DRAGSIZE_NE = $F005;
    Starts a resize operation on the top right corner of the form. The window then supports either keyboard or mouse resizing.
  • SC_DRAGSIZE_SW = $F007;
    Starts a resize operation on the bottom left corner of the form. The window then supports either keyboard or mouse resizing.
  • SC_DRAGSIZE_SE = $F008;
    Starts a resize operation on the bottom right corner of the form. The window then supports either keyboard or mouse resizing.

To use these constants we simply send a perform message to the window we wish to work with.
e.g.

AFormMouseDown(…)
begin
ReleaseCapture;
// releases the capture of mouse events for this form
Perform(WM_SYSCOMMAND,SC_DragMove,0);
// send the drag move message
end
;

It is important to use the release capture method to tell windows not to send the mouse messages to the form that has just received the mouse down.