Add a New Event to a Component: Right-Click in a TDBGrid

I need to add a right-click event to aTDBGrid, but am ata loss as to how to accomplish it. I want to pop up a menu. Do you knowhow?

Before I startinto this, there is an easier way to handle a right-mouse click topop up a menu of sorts: Drop a TPopupMenu on a form and set its AutoPopupto True, then set the DBGrid’s PopupMenu property to the name of theTPopupMenu.

It’s amazing how easy it is (at least in manycases) to add functionality to a component. Mouse clicks are problablythe easiest to implement, because the message structure of the windows messageis fairly simple — it’s only one parameter!

unit Extgrid;interfaceuses  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,  Forms, Dialogs, Grids, DBGrids;type  TExtDBGrid = class(TDBGrid)  private    { Private declarations }    FOnRightClick : TNotifyEvent;    procedure WMRButtonDown(var Message : TWMRButtonDown); messageWM_RBUTTONDOWN;  protected    { Protected declarations }  public    { Public declarations }  published    { Published declarations }    property OnRightClick: TNotifyEvent read FOnRightClick write FOnRightClick;  end;procedure Register;implementationprocedure TExtDBGrid.WMRButtonDown(var Message : TWMRButtonDown);begin  inherited;  if Assigned(FOnRightClick) then    FOnRightClick(Self);end;procedure Register;begin  RegisterComponents('Samples', [TExtDBGrid]);end;end.

Okay, what did we just do? First of all we defined an eventhandling procedure:

procedure WMRButtonDown(var Message : TWMRButtonDown); messageWM_RBUTTONDOWN;

the message directive following the declaration tellsthe component to watch for the message WM_RBUTTONDOWN, which is the messagefor the right mouse button down. What we’ve done with this procedure ismake the component look for this message. However, mere awareness isn’tenough, by far! We have to add a property of type TNotifyEvent (whichif you remember is the domain in which mouse clicks fall) so that userscan add code to process it. So, in the published section of theunit, we added:

property OnRightClick: TNotifyEvent read FOnRightClick writeFOnRightClick;

to assign the message to the component.

If you’re familiar with writing components, you’ll notice the differencein how message properties are handled as opposed to regular properties.Typically, when setting and getting property values you would use a Getor Set method or a direct read and write to the properties’ fields. Inessence, things happen in response to the change in state of the property.This appears to occur in the notation of the unit above, but there’s actuallya bit more going on. In fact, it’s a bit backwards. The FOnRightClickis not really a variable, but rather a pointer to an event-handlingprocedure. Luckily we don’t have to know the mechanics behind this, becausethe compiler will automatically do all the work for us in creating thehandler’s declaration. However, if we look at the WMRButtonDownprocedure, we’ll see that the variable is actually being used as a procedurecall:

procedure TExtDBGrid.WMRButtonDown(var Message : TWMRButtonDown);begin  inherited;  if Assigned(FOnRightClick) then    FOnRightClick(Self);end;

As I mentioned above, message handling works sort of in reverseof regular property setting. Where with a regular property, the changein the state of the property causes some code to fire off with an event,it’s a message-aware procedure that sits in memory that changes the stateof the message property. To illustrate this, in the code example above,the WMRButtonDown procedure makes the component aware of the right-click.Whenever the message appears in the message queue, WMRButtonDown fires.When it fires, it in turn fires off the FOnRightClick procedure, whichon the component would be the event handler OnRightClick.

Use this code as a basis for writing your own events. Theonline help under WinAPI help has excellent discussions of messages andhow they interact with the system. In addition to this though, I stronglyrecommend getting a book on the Windows API and getting the Resource Kitand SDK to really get into event handling.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS