Intercept message (1)
I haven't given you something for a long time, I am a bit interest today, write a little.
In Delphi, each component has an intrinsic message processing mechanism, which is the default, and if there is no special formation processing, the default message processing is called.
I don't say it to you. Everyone is practicing, and there is a big pile of VCL what structure, so it is reluctant to read a headache.
There are three main methods of intercepting messages:
1. Overload the virtual method WNDPROC to build inheritance, this can intercept all messages
2. The message handles the message written for a message, which can intercept the established message
3, overloading the virtual method of building inheritance Defhandler.
1 kind: heavy dash WndProc
Procedure TmyObject.WndProc (Var Message: TMESSAGE);
Begin
{
Inherited WNDPROC (Message);
END;
Give an example:
Procedure Tcontrol.WndProc (Var MeESAG: TMESSAGE);
Begin
{
IF (Message.msg> = WM_Mousefirst) and (Message.msg <= WM_MouseLast) THEN
IF Dragging Then
DragMousemsg (TwMMouse (Message))
Else Begin
......... (Treatment of other mouse messages)
END;
Dispatch (Message); / / Otherwise sending information normally
END;
Let's write a complete class, an EDIT control without focus
Unit myedit;
Interface
Uses
Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs,
STDCTRLS;
Type
TMYEDIT = Class (TEDIT)
Private
{Privateeclarations}
protected
{Protected Declarations}
{Other Fields and Methods}
Procedure WndProc (var message: tMessage); override; // overload
public
{PublicDeclarations}
Published
{PublishedDeclarations}
END;
PROCEDURE register;
IMPLEMentation
Procedure register; // Register in IDE
Begin
RegisterComponents ('Samples', [TMYEDIT]);
// Register to the Samples page, the control name is TMYDIT
END;
Procedure TmyEdit.wndProc (Var Message: TMESSAGE);
Begin
IF message.msg = wm_mousemove dam
Begin
{Set the cursor as Crarrow, not the default Crbeam cursor}
Cursor: = Crarrow;
EXIT;
END;
{Shield off WM_SETFOCUS message, do not allow TMYEDIT controls to get input focus}
If Message.msg = WM_SETFOCUS TEN EXIT; {Nothing does not implement}
Inherited WndProc (Message); {other news father's WndProc processing}
END;
End.
For the second method, it is very simple, you can use the message generator, the third one, I have never used it, interested friends can do it yourself. Because the first processing has the advantage, all of the messages can be handled, so it is more practical.