When processing the form message, I think many people will touch the mouse to leave the form (hereinafter referred to as MouseEleAve). In C Builder, there is no way to process this message directly, we need to do it yourself. By referring to some information, I found it to handle MouseEleave in C Builder, not more than the following four methods, is now written for your reference. If there is anything wrong, please correct it.
(1), clumsy Timer
Every time, someone will immediately think of Timer when it comes to the message of the mouse leaves the form. Yes, this method is very simple, it is indeed valid. It is only necessary to determine if the coordinates of the position where the mouse location is in the form in the Timer's Ontimer event. The detailed code is as follows:
Void __fastcall tform1 :: timer1timer (TOBJECT * Sender) {Point Pt; getCursorpos (& PT); // Get the mouse coordinate Rect Rect; getWindowRect (Handle, & Re); // Get the rectangular range IF of the form (! PtinRect " , PT)) / / Judgment whether the coordinates of the mouse are within the rectangle of the form
CAPTION = "OUT"; else capen = "in";}
Why do I want to be a clumsy Timer? There are two reasons: 1. Ontimer is a relatively low news, from strict sense, the above practice is not accurate. If the system is handling a large number of messages, we can't get a MouseEleave message in time. Second, Timer is a more valuable system resource, which seems to be a waste on MouseLeave, because we have a better way to do the same thing.
(2) SETCAPTURE ()
SetCapture () allows the specified form to capture all mouse messages, of course, including MouseLeave:
Void __fastcall tform1 :: formousemove (Tobject * sender, tshiftstate shift, int x, int y) {caption = "in"; setcapture (handle); TPOINT PT (X, Y); TRECT Rect; Rect = getClientRect (); if (! PtinRect (RECT, PT)) {ReleaseCapture (); caption = "out";}}
However, this method is too overbearing because setcapture () will have all mouse messages. Although releasecapture after the capture of MouseEleave, you can't react to other mouse messages during the capture process. Do not believe? You may wish to put a Button control in the form, then run some point? :)
(3) Limited TrackMouseEvent ()
The MSDN said that trackMouseEvent () allows the specified form to accept the WM_MouseLeave message. But after accepting the message, if you want to continue to accept the WM_MouseEleave message, you must recall TRACKMOUSEEVENT (): // ------------------------------------------------------------------------------------------------------------------------------------------------------ ---------------------------------------------__ fastcall tform1: : TFORM1 (TComponent * Owner: TFORM (OWNER) {mousetrack = false; // TForm1 private variable, detecting the mouse is already track} // ----------------- -------------------------------------------------- -------- Void __fastcall tform1 :: wndproc (tMessage & Message) // Reserved WndProc {if (Message.msg == WM_MouseLeave) / / Capture WM_MouseLeave message {CAPTION = "OUT"; mouseTrack = false // mouse Track has been completed} TFORM :: WndProc (Message);} //----------------------------- ------------------------------------------- void __fastcall tform1 :: flmousemove (Tobject * sender, INT X, INT Y) {CAPTION = "in"; if (! Mousetrack) {trackMouseEvent tt; tt.cbsize = sizeof (tt); tt.dwflags = tme_leave; tt.hwndtrack = handle ; TRACKMOUSEE VENT (& TT); mouseTrack = true; // Start mouse track}} // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------
This method is very useful, the only shortcoming is that Win98 may not be supported (I have not been supported by the specific support, which brother has Win98 to help me test it). In the Win2000 environment, I recommend this method, :)
(4), unknown cm_mouselave
In the 9CBS Forum, some people often say that they can achieve the same effect by capturing CM_MouseLeave messages. However, based on my test, cm_mouseeleave works very well on the control, which can be used to capture the message of the mouse to leave the control. But it seems that it is not a good test on the form, I may not do it myself. If there is any prawn know how to use it, please tell the younger brother, I will be grateful.
All the above code is compiled successfully in the Win2k Professional BCB6.0 environment.