Moving a Form by clicking in its client area 

You can let your users move your Form by having them click in the client area.  This is especially useful for captionless or weird shaped forms.  The key to this method is to "fool" Windows into thinking the user clicked in the Caption area of the form.  That is, you'll need to map the clicks from the Client to the Caption area... 

KEYWORDS: "fool" Windows, WM_NCHITTEST, HTCLIENT, HT_CAPTION


Method 1:

(1) Create a message handler...
 

//in header file 
    void __fastcall WMNCHitTest(TMessage &Msg); 

BEGIN_MESSAGE_MAP 
    MESSAGE_HANDLER(WM_NCHITTEST, TMessage, WMNCHitTest) 
END_MESSAGE_MAP(TForm) 

 


 
 

(2) Map the clicks... 

//---------------------------------------------------------------------------
void __fastcall TForm1::WMNCHitTest(TMessage &Msg) 

    TForm::Dispatch(&Msg); 
    if (Msg.Result == HTCLIENT) Msg.Result = HTCAPTION; 

 


Method 2:

Alternatively, you can send your Form the WM_SYSCOMMAND message with the SC_DRAGMOVE flag to initiate a move operation.  This is best done from within an OnMouseDown event handler...
 

//---------------------------------------------------------------------------

void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
    TShiftState Shift, int X, int Y)
{
    if (Button == mbLeft)
    {
        ReleaseCapture();
        SNDMSG(Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
    }
}