Move the form by responding to the "wm_nchittest" message.
Windows sends a "WM_NCHITTEST" message to determine whether the mouse action occurred in the form
Customer area, or a special area of the border (non-client area).
If Windows discovers that the user clicked the form title, the system moves the form,
When you click the form border, the system starts to change the size of the form.
The routines are as follows:
//declaring a custom event to intercept "Wm_nchittest" messagesProcedure Moveform (varm:twmnchittest); Message wm_nchittest; Procedure Tform1.moveform (varm:twmnchittest);begininherited; //inheritance, the form can continue to handle future eventsif(m.result=htclient)//If it happens in the customer area and((Getkeystate (Vk_control) <0)//detects if the "Ctrl" key is pressed ThenM.result:=htcaption;//change ". The value of the Result fieldEnd;
Method Two:
Get Windows messages by creating a handler for Application.onmessage.
You can adjust the application's response to different messages or to
Messaging services. This is followed by a mouse-down message from the form client area,
Sends a message that is pressed within the title bar.
The routines are as follows:
proceduretform1.formcreate (Sender:tobject); beginApplication.onmessage: =Appmessage; //capturing messages: Associating a program's received message events with the message filtering process End; procedureTform1.appmessage (varmsg:tmsg;varHandled:boolean); begin if(MSG.message= Wm_lbuttondown) and //if the left mouse button is pressed,(DefWindowProc (Handle, Wm_nchittest,0, getmessagepos) = htclient) and //determine if the cursor is within the client area((Getkeystate (Vk_control) <0) //detects if the "Ctrl" key is pressed Then beginSendMessage (Handle wm_nclbuttondown htcaption getmessagepos); //send the mouse to press the message in the title barHandled: =true; End;
Method Three:
Send a "wm_syscommand" message directly to the form and need to use an archive
The "Sc_dragmove" flag, defined as follows:
Const sc_dragmove:longint= $F 012;
We can only send this message to the Twincontrol derived component and can only respond to
Mouse down event because the system captures the mouse at this time (when the mouse button is released,
The drag operation is meaningless).
The routines are as follows:
ConstSc_dragmove:longint=$F 012;Implementation{$R *. DFM}procedureTform1.formmousedown (sender:tobject; Button:tmousebutton; Shift:tshiftstate; Xy:integer);beginifSsctrlinchShift Then//determine if the "Ctrl" key is pressedbeginreleasecapture;//objects that release mouse events(Sender asTwincontrol). Perform (Wm_syscommand, Sc_dragmove,0);//Send MessageEnd;End;
This approach works with forms and components, such as moving components, and writing their "OnMouseDown" events in the same way as the "OnMouseDown" event of a form.
If you want to move a form only, you can write code as follows:
procedure Tform1.formmousedown (sender:tobject; Button:tmousebutton; Shift:tshiftstate; X Y:integer); begin if inch Then Begin releasecapture; 0 ); End ; End;
Operation Result:
You can move the form by pressing the Ctrl key and dragging the mouse.
There are a number of ways to implement a untitled form's movement,
You can also determine the "left" of a form by detecting the position (coordinates) of the mouse
and "Top" values, and so on, no longer a detailed example.
procedure Tform1.formmousemove (sender:tobject; Shift:tshiftstate; X, Y:integer); begin if inch Then releasecapture;perform (wm_syscommand, $F 012,0); End
Reference: http://www.cnblogs.com/rogee/archive/2010/09/20/1832063.html
Untitled Form Drag (three methods)