Solved

traping WM_RBUTTONDBLCLK message in a component

Posted on 2001-08-28
9
366 Views
Last Modified: 2010-05-18
hi all
 I want to write a component that catch the WM_RBUTTONDBLCLK message.
The final goal is a non visual component providing an OnRightDbClick event.
So, how my component can catch it's owner's message ?

hope it's clear.

Thanks

Mike
0
Comment
Question by:EKIM
9 Comments
 
LVL 6

Expert Comment

by:edey
ID: 6433356
I think you have to subclass the owner.

GL
Mike
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 6435160
Application.HookMainWindow allows to get your private message feed.
0
 
LVL 33

Expert Comment

by:Slick812
ID: 6437625
hello EKIM, here's the code for a component that will do the right mouse button double click thing. It uses the Application.OnMessage to get the WM_RBUTTONDBLCLK. You should look into the Application.OnMessage if you are going to deal with windows messages. Oh, and in a component, it might be really UNwise to set the Handled to True, since it will knockout that message in ALL the forms of that app.

- - - - - - - - - - - - - - - - - -

unit RButDblClk;
interface
uses
  SysUtils, Windows, Messages, Classes, Forms;

type
  TOnRightDlbClick = procedure(Sender:TObject)of Object;
  TRButDblClk = class( TComponent )
  private
     FOnRightDlbClick: TOnRightDlbClick;
  protected
     procedure AppMessage(var Mesg: TMsg; var Handled: Boolean);
  public
     constructor Create( AOwner: TComponent ); override;
     destructor Destroy; override;
  published
     property OnRightDlbClick: TOnRightDlbClick read FOnRightDlbClick write FOnRightDlbClick;
  end;

procedure Register;

implementation

constructor TRButDblClk.Create( AOwner: TComponent );
begin
  inherited Create( AOwner );
  Application.OnMessage := AppMessage;
end;

destructor TRButDblClk.Destroy;
  begin
  inherited Destroy;
  end;

procedure TRButDblClk.AppMessage(var Mesg: TMsg; var Handled: Boolean);
begin
Handled := False;
if Mesg.message = WM_RBUTTONDBLCLK then
  begin
  if Assigned(FOnRightDlbClick) then FOnRightDlbClick(Self);
  end;
end;

procedure Register;
begin
  RegisterComponents('Additional', [TRButDblClk]);
end;

end.
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 6439215
Slick812, using Application.OnMessage in a component is unwise. Application.HookMainWindow is the way to go because it installs and handles a handler chain.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:EKIM
ID: 6439411
robert : With ApplicationHookMainWindow, what happen if the db right click occurs not on the main form, but on another application's form ? and if this form is not modal ?

regards

Mike
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 6439548
Nothing happens. Application.HookMainWindow is local to the application. It is like Application.OnMessage, only that a component should not occupy Application.OnMessage.
0
 

Author Comment

by:EKIM
ID: 6441123
robert : ok, give me just some time to have a look on your solution.

Slick812 : thanks a lot for this full comment. Even it will maybe not the best solution, I do appreciate such comments.

regards

Mike
0
 
LVL 10

Accepted Solution

by:
Jacco earned 150 total points
ID: 6442111
Hi Mike,

Here is yet another solution. This one is selective for one TControl. So this is not the owner but a setable control. This method works by extending the msg-handling chain of a control by setting the WindowProc property.

Regards Jacco

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TRDBLClickComp = class(TComponent)
  private
    fControl: TControl;
    fOldProc: TWndMethod;
    fOnRightDblClick: TNotifyEvent;
    procedure SubClassWndProc(var Message: TMessage);
    procedure SetControl(const Value: TControl);
  protected
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  public
    destructor Destroy; override;
    property Control: TControl read fControl write SetControl;
    property OnRightDblClick: TNotifyEvent read fOnRightDblClick write fOnRightDblClick;
  end;

  TForm1 = class(TForm)
    Memo1: TMemo;
    btnCreateCatcher: TButton;
    btnReroute: TButton;
    procedure btnCreateCatcherClick(Sender: TObject);
    procedure btnRerouteClick(Sender: TObject);
    procedure ReportClick(Sender: TObject);
  private
    { Private declarations }
    fTest: TRDBLClickComp;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

destructor TRDBLClickComp.Destroy;
begin
  Control := nil;
  inherited Destroy;
end;

procedure TRDBLClickComp.Notification(AComponent: TComponent; Operation: TOperation);
begin
  if (AComponent = fControl) and (Operation = opRemove) then
    Control := nil;
end;

procedure TRDBLClickComp.SetControl(const Value: TControl);
begin
  if fControl <> Value then
  begin
    if Assigned(fControl) then
    begin
      fControl.WindowProc := fOldProc;
      if fControl.Owner <> Owner then
        fControl.RemoveFreeNotification(Self);
    end;
    fControl := Value;
    if Assigned(fControl) then
    begin
      fOldProc := fControl.WindowProc;
      fControl.WindowProc := SubClassWndProc;
      if fControl.Owner <> Owner then
        fControl.FreeNotification(Self);
    end;
  end;
end;

procedure TRDBLClickComp.SubClassWndProc(var Message: TMessage);

begin
  if Message.Msg = WM_RBUTTONDBLCLK then
  begin
    if Assigned(fOnRightDblClick) then
      fOnRightDblClick(fControl);
  end else
    fOldProc(Message);
end;

procedure TForm1.btnCreateCatcherClick(Sender: TObject);
begin
  fTest := TRDBLClickComp.Create(Self);
  fTest.Control := Self;
  fTest.OnRightDblClick := ReportClick;
end;

procedure TForm1.btnRerouteClick(Sender: TObject);
begin
  fTest.Control := btnReroute;
end;

procedure TForm1.ReportClick(Sender: TObject);
begin
  Memo1.Lines.Add('RightDblClick on ' + TComponent(Sender).Name);
end;

end.
0
 

Author Comment

by:EKIM
ID: 6443727
hi jacco !

Nice job ! It works fine, and I just had to copy/paste your example to be sure that it was what I need.

Points have been increased up to 150 for "really complete-full-working-answer".  :-)

Regards.

Mike
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now