What is the difference between Left button Mouse events and Right button Mouse Events

hi there,

    Please find code section.  I have taken two panels. I just written showmessage in each of Panels mouse up event. I am setting DFlag to True in mouse down event. I am writing identity of panels to the file using AppendTextFile function in the mouse move event of panels.

           First I clicked on the panel1 by mouse Left button. And I dragged the mouse on to the Panel2 without releasing the mouse left button and released the mouse on panel2. I got the message "Panel1 mouse up" that means even though I released the mouse on panel2, Panel1 mouse up has fired.I opened the text file with name "xxx.txt" in C drive, here I found entire message as "Panel1 mouse move".  Ok!


             Next I clicked on the panel2 by mouse Left button. And I dragged the mouse on to the Panel1 without releasing the mouse left button and released the mouse on panel1. I got the message "Panel2 mouse up" that means even though I released the mouse on panel1, Panel2 mouse up has fired.I opened the text file with name "xxx.txt" in C drive, here I found entire message as "Panel2 mouse move".  Ok!


               Next I clicked on the panel1 by mouse Right button. And I dragged the mouse on to the Panel2 without releasing the mouse Right button and released the mouse on panel2. I got the message "Panel2 mouse up", Panel2 mouse up has fired. I opened the text file with name "xxx.txt" in C drive, here I found some first part of message as "Panel1 mouse move" and next part of message as "panel2 mouse move".  Ok!  


               Next I clicked on the panel2 by mouse Right button. And I dragged the mouse on to the Panel1 without releasing the mouse Right button and released the mouse on panel1. I got the message "Panel1 mouse up", Panel1 mouse up has fired. I opened the text file with name "xxx.txt" in C drive, here I found some first part of message as "Panel2 mouse move" and next part of message as "panel1 mouse move".  Ok!  


         I hope you understood.

  Why mouse left button and mouse right button events are different.  Could you please explain me clearly.

                                  Thank you

         

           
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls;
 
type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    procedure Panel1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Panel2MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Panel2MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure FormCreate(Sender: TObject);
    procedure Panel1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Panel2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    Procedure AppendTextFile( S, FileName : String );
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
  DFlag : Boolean;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.Panel1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  showmessage('panel1 mouse up');
  DFlag := False;
end;
 
procedure TForm1.Panel2MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  showmessage('panel2 mouse up');
  DFlag := False;
end;
 
procedure TForm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if DFlag then
    AppendTextFile('panel1 mouse move','c:\xxx.txt');
end;
 
procedure TForm1.Panel2MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if DFlag then
    AppendTextFile('panel2 mouse move','c:\xxx.txt');
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  DFlag := False;
end;
 
procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  DFlag := True;
end;
 
procedure TForm1.Panel2MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  DFlag := True;
end;
 
Procedure TForm1.AppendTextFile( S, FileName : String );
Var
  T : TextFile;
  lSize : Integer;
Begin
 
  AssignFile( T, FileName );
  {$i-} Reset( T );{$i+}
  If IoResult <> 0 Then
   {$i-}ReWrite( T ){$i+}
  Else
  begin
    {$i-}Append( T );{$i+}
  end;
  if IOResult = 0 then
  begin
    {$i-}Writeln( T, S );{$i+}
  lSize := FileSize(T) div 10240;
  CloseFile( T );
  end;
  if lSize >= 5 then
    RenameFile(FileName,'c:\Log'+FormatDateTime('ddHHNN',now)+'.txt')
End;
 
 
 
end.

Open in new window

inampudi1Asked:
Who is Participating?
 
Geert GOracle dbaCommented:
actually best is to set it in onmousedown and unset in onmouseup

the difference is in the 2 different event handlers for the 2 buttons:

procedure TControl.WMLButtonDown(var Message: TWMLButtonDown);
begin
  SendCancelMode(Self);
  inherited;
  if csCaptureMouse in ControlStyle then MouseCapture := True;
  if csClickEvents in ControlStyle then Include(FControlState, csClicked);
  DoMouseDown(Message, mbLeft, []);
end;

procedure TControl.WMRButtonDown(var Message: TWMRButtonDown);
begin
  inherited;
  DoMouseDown(Message, mbRight, []);
end;

altering the components you want this behaviour for would be easiest by adding the MouseCapture to the WMRbuttonDown


type
  TPanel = class(stdCtrls.TPanel)
  private
    procedure WMRButtonDown(var Message: TWMRButtonDown); message WM_RBUTTONDOWN;
  end;
 
  TForm1 = class(TForm)
  ...
  end;
 
procedure TPanel.WMRButtonDown(var Message: TWMRButtonDown); 
begin
  if csCaptureMouse in ControlStyle then MouseCapture := True; 
  inherited;
end;
  

Open in new window

0
 
Eddie ShipmanAll-around developerCommented:
Could it be that the capture control has not been changed when you release the button?
0
 
8080_DiverCommented:
They be recognized as being different for the same reason that the hyphen on the main key-board area and the one on the numeric pad area different or the 2 Enter keys are different.  
Now, have you stepped through your code while doing these exercises?  
Or, if you don't want to do that, add some logging to a text file so that you log the entery and exit from all of the routines.  That way, you can get a transcript of what is happening.  Otherwise, mostly what you are going to get as "assistance" is speculation.
If you have CodeSite 4, add some CodeSite logging, it will show you in real-time what is going on.  (I do love my COdeSite. ;-)
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Geert GOracle dbaCommented:
the capture control isn't set for dragging with the right mouse button
when a  capture control is set it receives all mouse messages regardless of what component is below the mouse
after left mouse down the capture control is set and from then on all messages are sent to that component
see controls.pas unit for WM_MOUSEDOWN if you have access to the delphi sources
0
 
Eddie ShipmanAll-around developerCommented:
Could he possibly change the capture control in an OnMouseLeave event?
0
 
inampudi1Author Commented:
hi experts,

   Thank you for your replies.
      This left and Right mouse events are different only in Delphi or in windows also.  
  When delphi sets capture control to a particular component, I mean with what event of a component Delphi sets  capture control to that component. Could you please explain me clearly what is Capture control, how to set for a particular component from delphi.  

                            Thank You
0
 
Geert GOracle dbaCommented:
oops, it's ExtCtrls, not StdCtrls

  TPanel = class(ExtCtrls.TPanel)


0
 
Geert GOracle dbaCommented:
Givind a B grade goes with explaining why you give a B grade.

I am missing on the why part ...
0
 
odissey1Commented:
I would suggest to add Abort procedure onMouseUP event to release all clicks, else mouse event stick to Panel1:

procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
     Memo1.Lines.Add('pDown');
end;

procedure TForm1.Panel1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
     Memo1.Lines.Add('pUp');
     Abort;//<--------------------------------
end;

Otherwise excellent solution, I was looking for it for a long time
odissey1
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.