Key words: Mobile, message, untitled
Author: Zhu nengwen
We all know that Windows uses the title bar to move the form. When we press the left mouse button on the title bar, Windows will send the wm_nchittest message to tell the system to move the form. However, in actual programming, sometimes for interface needs or special requirements, the form does not have a title bar, we must move the form through the program. Below I will introduce several methods and techniques for moving forms.
Method 1: Calculate the position of the form based on the location difference between the mouse and the movement. The variables to be defined are as follows:
VaR
Opos, CPOs: tpoint;
Flag: Boolean = false;
Code for handling the onmousedown event:
Flag: = true;
Opos. X: = X;
Opos. Y: = y;
Code for handling onmousemove events:
If flag then
Begin
CPOs. X: = X;
CPOs. Y: = y;
Left: = left + CPOs. X-opos. X;
Top: = Top + CPOs. Y-opos. Y;
End;
Code for handling onmousemove events:
Flag: = false;
Method 2: Customize the message, intercept the "wm_nchittest" message, and convert the Message Value "htclient" to "htcaption ". The implementation is as follows:
First define a message constant: const wm_mytest = wm_user + 200;
The private part declaration process:
Procedure moveclient (VAR message: tmessage); message wm_nchittest;
Implementation of the process:
Inherited; // inherited. The form can continue to process subsequent events.
If message. Result = htclient then
Message. Result: = htcaption;
Method 3: send a "wm_syscommand" message directly to the form. Use the Unarchived "SC _dragmove" flag, which is defined as follows:
Const SC _dragmove = $ f012;
We can only send this message to the twincontrol derived component, and only respond to the mouse press event, because the system will capture the mouse at this time (when the mouse key is released, the drag operation is meaningless ). The onmousedown event processing code in the form:
Releasecapture; // release the Mouse capture status;
(Sender as twincontrol). Perform (wm_syscommand, SC _dragmove, 0); // send a mobile message to the form;
I have provided three methods for moving a form without a title bar. The method is easy to understand and does not need to understand the WINDOWS Message Processing Mechanism. However, this method requires too much processing of mouse events on the form, it is not concise enough. Method 2: Through message conversion, this method is easy to implement, but there is a disadvantage that the form will no longer respond to other messages. If there is a right-click on the form, then the right-click will not receive a response. Method 3 is the best method. By defining the SC _dragmove message, note that the value of SC _dragmove must be $ f012 or $ f011. Send the SC _dragmove message through the perform method, this method can also handle other mouse events, unlike method 2. Therefore, it is better to use method 3 to move the Untitled form. (I have used the above methods on delphi6.0 + windows2k ).