Search code examples
delphieventsnotificationsdelphi-10.4-sydney

Adding OnMouseDown event-handler to VCL component?


In a Delphi 10.4.2 32-bit VCL Application, I use the component TSVGIconImage from the SVGIconImageList library from the GetIt PackageManager.

Although the component supports the OnDblClick event-handler, it does NOT support the OnMouseDown event-handler! I.e., I can add an OnMouseDown event-handler by double-clicking the OnMouseDown event in the Object Inspector, however that event-handler gets never called at run-time:

procedure TformMain.SVGIconImage1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  CodeSite.Send('called!'); // never called!
end;

The TSVGIconImage component is declared in SVGIconImage.pas as:

TSVGIconImage = class(TCustomControl)

So shouldn't the TSVGIconImage component inherit its OnMouseDown event from TCustomControl?

Anyway, how can I add a working OnMouseDown event for TSVGIconImage in my application's code?

EDIT: After testing this in a separate VCL Application I found out that the TSVGIconImage OnMouseDown event handler is working there at run-time. So it must be something else that blocks the TSVGIconImage OnMouseDown event handler in my application. I have still to find out the cause.


Solution

  • A) Place a TApplicationEvents component on your form.

    B) Double-click the ApplicationEvents.OnMessage event in the Object Inspector to create an OnMessage event handler and write a case-filter for WM_LBUTTONDOWN:

    procedure TForm1.AppEvents1Message(var Msg: tagMSG; var Handled: Boolean);
    begin
      case Msg.message of
        WM_LBUTTONDOWN:
          begin
            if Msg.hwnd = SVGIconImag1.Handle then
              DoSomething;
          end;
      end;
    end;
    

    Thanks to @AndreasRejbrand and @fpiette for their constructive and helpful input!