1

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.

user1580348
  • 5,721
  • 4
  • 43
  • 105
  • You could report the issue to the developer https://github.com/EtheaDev/SVGIconImageList – R. Hoek Mar 07 '21 at 21:11
  • I have reported it: https://github.com/EtheaDev/SVGIconImageList/issues/158 But shouldn't it be possible to add a working `OnMouseDown` event-handler in my application code? – user1580348 Mar 07 '21 at 22:07
  • 1
    I had a look at the repository and really couldn't find anything odd about this `TCustomControl` that would explain why the `OnMouseDown` doesn't work. So, I somewhat reluctantly downloaded the library using GetIt. After quite a few errors (like the demo project not being runnable because a published property isn't found), I made a small demo app using the `TSVGIconImage`. My first impression is that this is not a very good SVG renderer: it is by far the slowest SVG renderer I have ever seen, and it doesn't render my math graphs correctly. Still, **the `OnMouseDown` handler works for me!** – Andreas Rejbrand Mar 07 '21 at 23:13
  • Proof: https://privat.rejbrand.se/SVGclick.gif – Andreas Rejbrand Mar 07 '21 at 23:16
  • (Well, to be fair, the library is probably not meant to be a 100% compliant and high-performance SVG 1.1 renderer; it's probably supposed to be used for fairly simple icons. And it probably is a very good library for such applications.) – Andreas Rejbrand Mar 08 '21 at 00:01
  • Now I tried it on a Win10 machine, too. `OnMouseDown` where on this one too. – Andreas Rejbrand Mar 08 '21 at 10:52
  • @AndreasRejbrand Thank you for your advice! In the meantime, I have also found out that `TSVGIconImage.OnMouseDown` does indeed work on a simple test app. I still don't know what is blocking the `TSVGIconImage.OnMouseDown` event handler in my application. – user1580348 Mar 08 '21 at 11:27
  • @AndreasRejbrand What better (most compatible and fastest) SVG library would you recommend? – user1580348 Mar 08 '21 at 13:05
  • @user1580348: I have no experience with non-browser SVG viewers, but just for fun I now downloaded a few VCL SVG libraries. Non of them implements `dominant-baseline` properly, but some support `text-anchor`. I suspect I demand too much of a non-browser SVG viewer when I ask for `dominant-baseline` support. (All modern web browsers are really good SVG viewers: Firefox, Chrome, Edge.) – Andreas Rejbrand Mar 08 '21 at 13:38
  • Would it be sensible to include a WebBrowser component in a Windows 32 VCL Application to be used as an SVG viewer for local SVG files? What WebBrowser component would you recommend for this purpose? – user1580348 Mar 08 '21 at 13:50
  • @user1580348: Personally I think so. If you want a fast a functional SVG 1.1 viewer, it is really hard to beat a modern browser. If you need to use simple SVG icons "everywhere" in the GUI (button icons, toolbar icons, menu icons, etc.) or as simple decorations, it doesn't make sense to use a browser. But if the SVG document is a "major" part of the GUI (for instance, a mathematical plot), I really do think it makes sense to embed a browser. Any modern browser will do. – Andreas Rejbrand Mar 08 '21 at 14:33
  • @AndreasRejbrand Please look at this example here: https://stackoverflow.com/questions/66531907/how-to-center-and-stretch-shrink-svg-display-in-twebbrowser – user1580348 Mar 08 '21 at 14:55

1 Answers1

1

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!

user1580348
  • 5,721
  • 4
  • 43
  • 105
  • Regarding filtering: Test if `Msg.hwnd = MySvgIconImage.Handle` (assuming you only have *one* problematic icon image). But it would probably be a better idea to fix the underlying issue. – Andreas Rejbrand Mar 08 '21 at 12:10
  • tagMSG has a HWND member. You can compare it with the TSVGIconImage.Handle if it has an handle. If it has no handle, then you can check for the handle of container (TPanel or TForm, TFrame, etc) and use the coordinates in lparam member to check if it correspond to the TSVGIconImage. Also use the coordinates in LParam to find where the user clicked. – fpiette Mar 08 '21 at 12:12
  • @fpiette: `TSVGIconImage` is a `TCustomControl` so it is windowed. – Andreas Rejbrand Mar 08 '21 at 12:50
  • Please now you have the solution, would you please edit your answer with the correct code according to Andreas and my comment? This way, the next one looking for the information will have it ready. – fpiette Mar 08 '21 at 14:55