Solved

traping WM_RBUTTONDBLCLK message in a component

Posted on 2001-08-28
9
363 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
Comment Utility
I think you have to subclass the owner.

GL
Mike
0
 
LVL 11

Expert Comment

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

Expert Comment

by:Slick812
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:EKIM
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

6 Experts available now in Live!

Get 1:1 Help Now