The Tcontrol translates system messages into VCL events to incorporate system messages into the VCL. The mouse event is a good example of what happens when we click a mouse to generate a mouse click message, and how does this click message be processed and executed?
There is a piece of code in the control cell:
1Tcontrol =class(tcomponent)2 Private3 ...4Fcontrolstate:tcontrolstate;//state//tcontrolstate = set of (Cslbuttondown, csclicked, .....);5 ...6Fonmousedown:tmouseevent;//the method used to accept the user's definition. 7 ...8 procedureDomousedown (varMessage:twmmouse; Button:tmousebutton; Shift:tshiftstate);9 ...Ten procedureWmlbuttondown (varMessage:twmlbuttondown);messageWm_lbuttondown;//left mouse button One procedureWmrbuttondown (varMessage:twmrbuttondown);messageWm_rbuttondown;//Right mouse button A procedureWmmbuttondown (varMessage:twmmbuttondown);messageWm_mbuttondown;//Middle mouse button - protected - procedureMouseDown (Button:tmousebutton; Shift:tshiftstate; X, Y:integer);Dynamic; the ... - PropertyOnmousedown:tmouseeventReadFonmousedownWriteFonmousedown; - End;
Tcontrol declares a onmousedown property that reads and writes an event pointer called Fonmousedown. When we need to deal with a button being pressed, we define a button-down method and assign a value to the button onmousedown property. At this point, Fonmousedown points to this user-defined method.
When we left-click the mouse, TObject calls the dispatch () method to distribute the Wm_lbuttiondown message, and then enter the appropriate message handler function. As follows:
1 procedureTcontrol.wmlbuttondown (varmessage:twmlbuttondown);2 begin3 Sendcancelmode (self);4 inherited;5 ifCscapturemouseinchControlStyle ThenMouseCapture: =True;6 ifCsclickeventsinchControlStyle ThenInclude (Fcontrolstate, csclicked);7 Domousedown (Message, Mbleft, []);8 End;
If it is a click event, add the csclicked in Fcontrolstate. and jump to the Domousedown () method.
1 procedureTcontrol.domousedown (varMessage:twmmouse; Button:tmousebutton;2 shift:tshiftstate);3 begin4 if not(csnostdeventsinchControlStyle) Then5 withMessage Do6 if(Width >32768)or(Height >32768) Then7 withCalccursorpos Do8MouseDown (Button, Keystoshiftstate (Keys) +Shift, X, Y)9 ElseTenMouseDown (Button, Keystoshiftstate (Keys) +Shift, Message.xpos, message.ypos); One End;
After doing some necessary processing in domousedown (), the MouseDown () is called;
1 procedure Tcontrol.mousedown (Button:tmousebutton; 2 shift:tshiftstate; X, Y:integer); 3 begin 4 if Then Fonmousedown (self, Button, Shift, X, Y); 5 End;
In MouseDown (), user-defined code is executed through the Fonmousedown event pointer. This completes the process of converting from one Wm_lbuttiondown message to the corresponding code.
Also, it is important to note that the click event of the mouse is not the corresponding message, the mouse clicking event is included in the Wm_lbuttiondown message, and the mouse double-click event message is the WM_LBUTTONDBLCLK message. And, if we want to click the mouse, the onmousedown () event is processed first, and then the onclick () event is processed. And, if we double-click the mouse, first process the onmousedown () event before processing the ondblclick () event. This is good to be verified:
1 procedureTform1.formclick (sender:tobject);2 begin3ShowMessage ('Click');4 End;5 6 procedureTform1.formdblclick (sender:tobject);7 begin8ShowMessage ('DblClick');9 End;Ten One procedureTform1.formmousedown (sender:tobject; Button:tmousebutton; A shift:tshiftstate; X, Y:integer); - begin -ShowMessage (' Down'); the End;
If we do not comment on line 14th, then no matter how we click the mouse, we can only handle the onmousedown () event, then ShowMessage () and get the focus, the OnClick () or ondblclick event will not be executed. Similarly, when we double-click the mouse, we will first ShowMessage (' click ').
Tcontrol with Windows messages