formsMap control, WinForms and Mouse Events

Within this forum we want to offer discussions all around our .NET based Map control including all .Net-language specific questions concerning calls of the xServer-API. Attention: xServer-relevant concepts can be found in further forums such as xRoute or xTour.
Post Reply
hwieser
Posts: 5
Joined: Thu Nov 03, 2016 3:18 pm

formsMap control, WinForms and Mouse Events

Post by hwieser »

I'm trying to replicate the Selection use case from the demo application in WinForms using the formsMap control.
I had to tweak the SelectInteractor a little bit to work with the mouse events from WinForms, but that's not really the issue.

The problem is that the mouse-events don't fire at all. It seems the wrapped WPF control just consumes all mouse events and does not call the corresponding WinForms events. Even hooking directly into the WPF events doesn't do anything.

I've tried to subscribe to Mouse events directly in my form, but it's the same result.

Is this desired behaviour? Is there a way to enable forwarding of mouse events?
I'm forced to use WinForms, so going all-out WPF is not an option here.

I'm using Ptv.XServer.Controls.Map v2.0.50727.
Joost
Posts: 310
Joined: Fri Apr 25, 2014 1:46 pm

Re: formsMap control, WinForms and Mouse Events

Post by Joost »

You can still subscribe to the WPF event of the wrapped WPF controll.

Code: Select all

mapControl.WrappedMap.MouseRightButtonDown += WrappedMap_MouseRightButtonDown;
private void WrappedMap_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
        // insert your own logic
}
This is how I usually interact with the .Net control in WinForm projects.
Joost Claessen
Senior Technical Consultant
PTV Benelux
hwieser
Posts: 5
Joined: Thu Nov 03, 2016 3:18 pm

Re: formsMap control, WinForms and Mouse Events

Post by hwieser »

That seems to work, but not all events are being passed on depending on the formsMap.MouseDragMode.
When it's set to "None" the MouseLeftButtonDown doesn't fire, but the MouseLeftButtonUp does...?

When I set the MouseDragMode to SelectOnShift like in the DemoCenter, the selection works, kind of.

But the ellipse and polygons aren't drawn. Well in WinForms one needs to overwrite the OnPaint event, so that at least explains that behaviour. And without the polygon drawn the InputHitTest doesn't work properly.
Joost
Posts: 310
Joined: Fri Apr 25, 2014 1:46 pm

Re: formsMap control, WinForms and Mouse Events

Post by Joost »

TLDNR: use PreviewMouseLeftButtonDown instead of MouseLeftButtonDown

Explanation:
To understand this behavior you need to know the basic of the RoutedEvent that WPF brings along. Explanation by MS: https://msdn.microsoft.com/en-us/librar ... .110).aspx .

For simplicity let say that the our program consist of
Window element
which contains a WpfMap element
which containt a LayoutRoot element
which contains multiple Image elements

The MouseLeftButtonDown is a bubling event, meaning if you click on the map the following happens:
Image.MouseLeftButtonDown is called
LayoutRoot.MouseLeftButtonDown is called
then WpfMap.MouseLeftButtonDown is called
then Window.MouseLeftButtonDown is called

If one of those set the event to handled (EventArgs.handled = true) the event will stop and no longer be passed on. This is what is happening in your example. If the MouseDragMode is set to NONE the LayoutRoot.MouseLeftButtonDown will set it to handled and you event handler will not be fired of.

However mouse click do not directly raise the click event, but a first a preview-click event. You can use the preview click if you want to be sure your code gets called before any PTV map code. In my sample I'm using right click which does not gets handled by any PTV code so it doesn't matter if I hooked into the click or the preview click events.
Joost Claessen
Senior Technical Consultant
PTV Benelux
hwieser
Posts: 5
Joined: Thu Nov 03, 2016 3:18 pm

Re: formsMap control, WinForms and Mouse Events

Post by hwieser »

Yep, I'm using the Right button also, works ok.
I'm still having issues to draw the polygon though. It seems the embedded WPF control is drawn on top of whatever WinForms draws.
Even overriding the OnPaint event of the formsMap doesn't yield any visible results for me.

So I don't really get why the WPF stuff doesn't just work..?
hwieser
Posts: 5
Joined: Thu Nov 03, 2016 3:18 pm

Re: formsMap control, WinForms and Mouse Events

Post by hwieser »

I've attached some code that I based on the Selection use case from the DemoCenter.
I have made some adjustements like using the PreView-Mouse-Events and passing the wrappedmap to the constructor.

I'm using Pins instead of FrameworkElements in my layer so that I can change the color rather than drawing a Ellipse around it.

The code to draw the Ellipse was left in, as well as the code that draws the Polygon when holding ALT and left clicking. Though none are ever drawn...?

Maybe you can expose the WPF map directly so I can embed into an ElementHost directly, which should give me more control?


Notes:
Clicking on a Pin will select it and change its color to red
Holding SHIFT allows multiple selection of Pins when clicking


Edit: I've also tried to add the wpfMap in an ElementHost inside a WinForm. Same result.
Just for kicks I tried to add a regular WPF Canvas instead, and tried to draw on top of that using Children.Add() with an Ellipse - that worked like a charm.

It seems that the object that implements IAddChild isn't exposed correctly to allow adding Children like that.
Attachments
SelectionWinForms.zip
Selection demo with WinForms
(1.26 MiB) Downloaded 509 times
Joost
Posts: 310
Joined: Fri Apr 25, 2014 1:46 pm

Re: formsMap control, WinForms and Mouse Events

Post by Joost »

I found a hint towards why your code is not working. The SelectInteractor from the demo wants to works from the mapView of the main window, but the helper function cannot find it. You tried to work around it by using the WpfMap directly but you are are using the wrong functions for translating coordinates.

I;m looking into why this does not happen in the Demo, I'll get back to you on this once I have learned more.
Joost Claessen
Senior Technical Consultant
PTV Benelux
Joost
Posts: 310
Joined: Fri Apr 25, 2014 1:46 pm

Re: formsMap control, WinForms and Mouse Events

Post by Joost »

I found the solution and attached a working sample. I stayed close to the original sample code , but include a few of your custom requirements (using pins, coloring instead of drawing the elipse).

It turned out that your code was creating the the SelectInteractor to soon. Parts of the map are loaded in the background in parallel and your code was creating the map SelectInteractor before the main mapview was created. The FindChild<Ptv.XServer.Controls.Map.MapView> then returned a map view of one of the gadgets, namely the overview map.

I move the creation of the SelectInteractor to the Loaded event of the map control and now my samples works.
Attachments
SelectionWinForms.zip
(1.26 MiB) Downloaded 537 times
Joost Claessen
Senior Technical Consultant
PTV Benelux
hwieser
Posts: 5
Joined: Thu Nov 03, 2016 3:18 pm

Re: formsMap control, WinForms and Mouse Events

Post by hwieser »

Thanks, that works like a charm.
Post Reply