Preventing a Form from being moved 

You can prevent the user from moving your Form by "fooling" Windows into thinking the user clicked in the Client area of the form.  That is, you'll need to map the clicks from the Caption to the Client area.  To accomplish this, manipulate the return value to the WM_NCHITTEST message.  Method 1 below, illustrates this idea.
 

KEYWORDS: WM_NCHITTEST, HTCAPTION, HTCLIENT 


Method 1:
 

(1) Create a message handler...
 

//in Form's header...
    void __fastcall WMNCHitTest(TMessage &Msg); 

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

 


 
 

(2) Map the clicks... 

//---------------------------------------------------------------------------
//in Form's source...

void __fastcall TForm1::WMNCHitTest(TMessage &Msg) 

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

 
 


 
 



 

Another way of accomplishing this task is to remove the "Move" option from the System (Window) Menu, as illustrated in Method 2.
 

Method 2:
 

//---------------------------------------------------------------------------
// in Form's source...

__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    HMENU HSysMenu = GetSystemMenu(Handle, false);
    DeleteMenu(HSysMenu, SC_MOVE, MF_BYCOMMAND);
}