Search code examples
c#winformsimagemap

How to realize with WinForms in C# something like an image with map areas in a homepage (at hoovering cover areas with semi transparent rectangle)?


I have a panel (here called parent), where I draw a calculated picture. Some rectangular areas of this picture shall higlight by hoovering over. It is the same behaviour like on a web page using , and , e. g. the german map on the right upper side.

At hoovering the according rectangle shall be covered by a half transparent blue . (And depending on keys like Alt, Ctrl and/or Shift in other colors, and clickable).

The first solution was a single instance of a transparent Panel - inherited from the Panel class. In the hoovering event of the parent I moved and resized the single instance to the right place, changing the color. This had some problems: * moving and resizing (SetBounds()) fired MouseLeave event of the parent and a MouseEnter event of the single panel. The events had to be adapted accordingly to get it working correctly, I did it, but it is was very slow, due to finding the right map area from the list.

The second solution was to generate dynamically an instance of a transparent panel for each map area. Each transparent panel had to set the e. g. Color.FromArgb(50, Color.Blue) at entering, and remove it at leaving the panel. But it seems to be even slower than before. If the mouse hurries over several maps, they are all drawn like hoovered, and slowly get transparent again.

Does anybody know a good solution for this requirements:

  • at picture resize in parent panel, map etc. has to be changed as well
  • partly transparent highlight hoovered rectangle area.
  • detection of Ctrl/Shift/Alt as events for an area and change of the color.
  • detect click events there

Are there other controls I better use for this purpose?

Thanks for on practice based ideas.

PS: The world map with satellite pictures shows better what I want to do: At hoovering the background is still visible more or less. But in my case the parent image, its size and the maps are calculated at runtime (after settings are completed by the user).


Solution

  • Solution

    Description

    I found now a solution, that reacts sufficiently fast to hoovering areas with the mouse.
    The Main pictures is drawn in a PictureBox instance, in more detail its property Image is assigned.
    The SizeMode property is set to Zoom, that automatically centers and resizes the image with keeping the aspect ratio of the assigned image.
    I use a dynamically created Picturebox instance for each map area (childs), that is invisible, if the mouse does not hoover over it.
    At hoovering over the map area the child shall appear - this is done in the mouse move event of the parent PictureBox, where I iterate over the children, detecting whether the mouse position is in the bounds of a child. The found child is set visible. Therefore the mouse enters this child control.
    In the leave event of the child control I set it invisible again. I experienced losses of mouse leave events for the child controls, if the mouse is moved too fast over all map areas. I assume, if the mouse pointer already left the area before it has been set visible, the event is never raised.
    The solution is, that all (other) child controls are set invisible, if in the mouse move event handler of the parent control does find no (a) child control.

    Steps to implement

    What to do, to implement my solution:

    • Use a designed parent PictureBox instance.
      • Assign the (dynamically) drawn picture to the Image property
      • Set SizeMode to Zoom
      • a list of Picturebox instances as form field

    At assignment of a newly calculated image to the parent Picturebox instance:

    • remove all child controls, its event handler, its entry from the list, if they already exist.
    • create dynamically a PictureBox instance for each map area and add it to a list.
      • add it to the parent PictureBox instance as a child control.
      • set its Tag property points to a data object containing the origin map bounds and the object represented by the rectangular map area.
      • set the bounds to the map area scaled and centered according to the bounds of the parent Picturebox instance, that it suits the automatically zoomed image.
      • register a mouse click event
      • register a mouse leave event
      • set background color to e. g. semi transparent green
      • set Visible to false

    In the parent Picturebox instance the mouse over event handler does:

    • finding the child Picturebox instance, where the mouse points at
    • if found it sets the found child visible
    • set all (other) child Picturebox invisible

    In the parent Picturebox instance the resize event handler does:

    • scale/move all the map area Picturebox instances according to the bounds of the parent image and the bounds of the parent Picturebox instance.

    The mouse leave event of each map area Picturebox instance set itself invisible (losses of events previously mentionned).
    The mouse click event of each map area Picturebox instance makes whatever shall be done by clicking the map area. Here playing a sine tone of the right chromatic pitch.

    Pictures

    The pictures below show the prototype with the map areas (not yet correctly aligned, some offset):

    The first picture is for illustration of parent picture and all map areas. The main picture (scale) and all map areas (dynamically created child Picturebox instances) are drawn in the first picture (by disabling the invisible action for map areas).

    first picture

    The second image is productive, where the mouse hoovers over tone G4.
    In the second image the form has been resized - therefore the parent image is automatically been centered and resized. The map areas were simply changed in their Bound property in the resize event handler of the parent PictureBox. And the invisible action has been enabled for the map areas.

    second picture