delphiclickonmousedownonmouseuptpanel

Cannot catch all clicks when using onmousedown, onmouseup and click events


I'm trying to create a custom button out of a TPanel component. For this, I have provided an override for the onmousedown and onmouseup events (to do some drawing), and I've used the onclick event to handle the clicks.

Unfortunately, if I rapidly click my panel, every other click is "lost", but I can't figure out why.

Even the easiest of examples fails in this regard. I created a new VCL application, added a listbox, one panel, and implemented the events as follows:

procedure TForm1.Panel1Click(Sender: TObject);
begin
  listbox1.Items.Add('click');
end;

procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  listbox1.Items.Add('mouse down');
end;

procedure TForm1.Panel1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  listbox1.Items.Add('mouse up');
end;

The result is as follows:

mouse down
click
mouse up
mouse down
mouse up

etcetera... Each second click is disregarded, but I have no idea why.

Can anybody explain this please?


Solution

  • Your panel is processing double-clicks when you rapidly click on it. use:

    Panel1.ControlStyle := Panel1.ControlStyle - [csDoubleClicks]
    

    to map double-clicks into clicks. (in your custom control set ControlStyle in it's constructor).

    csDoubleClicks The control can receive and respond to double-click messages. Otherwise, map double-clicks into clicks.

    See TControl.ControlStyle