4 Replies - 1527 Views - Last Post: 22 July 2010 - 03:45 PM Rate Topic: -----

#1 Aztral  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-July 09

Best way to achieve this with WinForm?

Posted 06 July 2010 - 01:27 PM

I'm building a control that is essentially a PictureBox but that I can overlay icons on. That was fairly simple to do but I would also like to be able to handle mousing over those icons.

To overlay the icons I'm just handling the PictureBox paint event as such:

private List<VRIcon> overlayIcons = new List<VRIcon>();

...

private void VRScrollableImage_Paint(object sender, PaintEventArgs e)
{
    foreach (VRIcon ico in overlayIcons)
    {
        if ((ico != null) && ico.Visible && (ico.Image != null))
        {
            e.Graphics.DrawImage(ico.Image, ico.Location);
        }
    }
}


Which works well enough but there doesn't seem to be a 'simple' way to detect and handle mousing over of each icon.

My idea to do this at the moment is to handle MouseMove events in the PictureBox and send the mouse coords to every visible overlayed icon and do a primitive collision check based on the mouse coords and the icon dimensions.

Is this the best way to go about this? Is there a cleaner way to do it in a C# WinForm application? It doesn't seem particularly clean or efficient but I'm not sure how else I would go about it.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Best way to achieve this with WinForm?

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,666
  • Joined: 02-June 10

Re: Best way to achieve this with WinForm?

Posted 06 July 2010 - 01:42 PM

Since it is a list of icons it seems you may not know if you have 1 or 10 icons overlaid on the main picture.
So the problem with just drawing the icons is that you loose track of the icon as an object.

Personally I think I would handle it in a more object oriented way.

Have a UserControl with the background image, then dynamically add foreground PictreBoxes for each icon, and subscribe your UserControl to each dynamically created picturebox's .Click event.

Now you can detect a click on a foreground PictureBox, and pass that to your parent UserControl.
The UserControl can then raise an event of .Clicked with the object argument being the actual icon picture box... You know where in your event args you have "object sender"? Well make the sender the icon picturebox object.

or some enum indicating they clicked on the "health" or "weapon" or "user" icon. Or on the 5th icon. Or as you need for your project.
Was This Post Helpful? 3
  • +
  • -

#3 JITHU  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 62
  • View blog
  • Posts: 201
  • Joined: 02-July 07

Re: Best way to achieve this with WinForm?

Posted 06 July 2010 - 02:14 PM

In addition, you could use a TableLayoutPanel to put your pictureboxes, so it will take care of arranging your icons correctly in your usercontrol.
Was This Post Helpful? 1
  • +
  • -

#4 Aztral  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-July 09

Re: Best way to achieve this with WinForm?

Posted 06 July 2010 - 02:55 PM

Thanks guys that worked nicely and it feels a whole lot cleaner.
Was This Post Helpful? 0
  • +
  • -

#5 Aztral  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-July 09

Re: Best way to achieve this with WinForm?

Posted 22 July 2010 - 03:45 PM

Coming back to this after working on other things for a while I'm not sure that this solution is going to work as well as I thought. It was acceptable when I was only attempting to overlay a few (very few) icons but as that number grows the app starts to get kind of ugly. When I was just drawing pure bitmaps there were no issues, when I draw PictureBoxes the control becomes a bit sluggish, scrolling the image causes weird artifacts to show up, filtering a specific type of icon on/off (showing and hiding all icons of a certain type) is sluggish, when icons are hidden the bottom of the image often does not go away, scrolling off the background image such that the icons are off the screen then back so they are visible again causes them to be distorted and it feels kind of sloppy overall. I had none of these issues when I was just drawing bitmaps.

Any ideas?

Thanks again.

Edit: Basically, I started this project without fully understanding the extent to which I'd be using graphics. I know that Winform is hardly the premiere graphics rendering tool in 2010. For that reason I've been considering using either HOOPS or switching this to a WPF application. I think either would provide significantly better graphical capabilities. Anyone have an opinion on that?

This post has been edited by Aztral: 22 July 2010 - 04:26 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1