Tutorial - Adding an OnEnter Event to TButton


Getting Started - What the component will do

It is often useful to have an onEnter and onExit event, the purpose of which is a procedure will be called whenever the mouse pointer enters or leaves your component. Delphi already provides this functionality for the TPanel component.

What this tutorial will dhow you how to create a new component (in this example it will be a descendant of the TButton Class) and add two new events to it: onEnter and onExit.

How it will be done

To start with we need to create a new component. In Delphi 3 this can be done by selecting the New Component menu item from the component menu. For its Ancestor Type select TButton (or if you want you can use another class). Enter a relevant Class Name for your new class, I used TACButton, and also enter which Palette Page you would like it to appear on (Here I used my Tutorials Palette page).

Finally you can Create the Unit and Install it. This will produce the skeleton code of the new component which we will use in the next section.

All we now need to do is write the code to accept the two new event handlers and then call the appropriate event when ever the mouse enters or exits the control. To detect the when mouse does either of these is actually very easy as Windows sends a message to the control when one of these events occurs. So all we have to do is respond to this message by calling the defined event handler.

Writing the actual code

In the skeleton code that we created earlier we need to add the functions needed to produce are additional functionality.

In the private declarations we need to add:

    FOnEnter: TNotifyEvent;
    FOnExit: TNotifyEvent;

These are for the events to be called when needed.
We now need to respond to the messages sent by windows the mouse enters and leaves are control these are:

    procedure CMMouseEnter(var Msg:TMessage); message CM_MOUSEENTER;
    procedure CMMouseLeave(var Msg:TMessage); message CM_MOUSELEAVE;

Next we need some Published properties to allow us to set set the event handlers for our new events.

    property OnEnter: TNotifyEvent read FOnEnter write FOnEnter;
    property OnExit: TNotifyEvent read FOnExit write FOnExit;

All these do is set the relevant private TNotifyEvent objects.

Finally we need to add the code to respond to the two Windows messages we prototyped earlier:

procedure TACButton.CMMouseEnter(var Msg:TMessage);
  if Assigned (FOnEnter) then

procedure TACButton.CMMouseLeave(var Msg:TMessage);
  if Assigned (FonExit) then

All that both of these procedures do is : 
  • Inherit any existing behaviour.
  • Check if the event handler has been assigned and if so then calls it.
  • Conclusion

    I hope this tutorial has proved to be useful. For further information on installing the component or placing it into a package then check out the tutorial on Adding Additional functionality to TButton.


    Web www.Delphi-Central.com
    Delphi Central - Delphi Programming Tutorials, Hints and Tips