Sign in to follow this  
u235

[.net] selecting portions of image (ms paint-like)

Recommended Posts

I'm coding up the first version of a little heightmap editor and I was wondering about something. I would like the users to be able to select portions of the heightmap (with the click and drag rectangle thing like in ms paint) so they can limit certain operations that I have built in to only certain areas. My problem is, I have no idea how to do that. I looked all over Google, The Code Project, and even here and couldn't find a single thing on it. Anyone got any idea where to start or can point me in the right direction? Rating up to whoever can get me or show me where to get this info. Thanks a bunch. -AJ

Share this post


Link to post
Share on other sites
Well this is how I would approch it:

1) You'll need some sort of state machine. So when the user clicks on the "selection" button the program will remember what "mode" it's in.

2) Next you'll need two events, one of mouse down, the other for mouse up. Of course mouse down records X1 and Y1 and mouse up records X2 and Y2.

3) If you're just going to have one selection onscreen at a time then this is simple, just use a normal rectangle struct. If you're planing on some more complex stuff then you'll probably want a 2D array of booleans, one bool value per-pixel.

4) Going with the 2D boolean array method. When the mouse up event is called simple clear the array then fill any elements in the imaginary rectangle with true.

Now as per drawing the rectangle. What you could do is when the user first presses the button down record X1 and Y1, and from then on record X2 and Y2. The mouse up function is only a signal that the action is complete. Since X2 and Y2 are constantly recorded, drawing a simple rectangle( X1, Y1, X2, Y2 ) should work for a "dragging" effect.

Hope that helps.

[Edited by - Scet on April 1, 2006 11:52:15 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Scet
Well this is how I would approch it:

1) You'll need some sort of state machine. So when the user clicks on the "selection" button the program will remember what "mode" it's in.

2) Next you'll need two events, one of mouse down, the other for mouse up. Of course mouse down records X1 and Y1 and mouse up records X2 and Y2.

3) If you're just going to have one selection onscreen at a time then this is simple, just use a normal rectangle struct. If you're planing on some more complex stuff then you'll probably want a 2D array of booleans, one bool value per-pixel.

4) Going with the 2D boolean array method. When the mouse up event is called simple clear the array then fill any elements in the imaginary rectangle with true.

Now as per drawing the rectangle. What you could do is when the user first presses the button down record X1 and Y1, and from then on record X2 and Y2. The mouse up function is only a signal that the action is complete. Since X2 and Y2 are constantly recorded, drawing a simple rectangle( X1, Y1, X2, Y2 ) should work for a "dragging" effect.

Hope that helps.



Well well well, returning the favor, I see [smile] Thanks for the quick reply. That actually makes perfect sense, but I do have one question. If I were at my computer I'd just try it and see what happens, but I'm not so I will just ask... when drawing the rectangle while the mouse is down, do I need to rect.Dispose() the current rect each time the mouse moves and make a new one of the correct size or will GDI+ do that automatically? Thanks again.

-AJ

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this