Click here to Skip to main content
15,896,278 members
Home / Discussions / C#
   

C#

 
Questionexport .m file from text file in VS2008 Pin
mohamadali&M18-Jan-10 18:10
mohamadali&M18-Jan-10 18:10 
AnswerRe: export .m file from text file in VS2008 Pin
Ashfield18-Jan-10 21:13
Ashfield18-Jan-10 21:13 
QuestionKeyEventArgs - differentiate control chars on numeric keypad [modified] Pin
cdesmarais18-Jan-10 12:21
cdesmarais18-Jan-10 12:21 
AnswerRe: KeyEventArgs - differentiate control chars on numeric keypad Pin
Luc Pattyn18-Jan-10 16:07
sitebuilderLuc Pattyn18-Jan-10 16:07 
Questionmanaged and unmanaged code............ Pin
3bood.ghzawi18-Jan-10 10:12
3bood.ghzawi18-Jan-10 10:12 
AnswerRe: managed and unmanaged code............ Pin
Not Active18-Jan-10 11:42
mentorNot Active18-Jan-10 11:42 
QuestionHow to Select Drawn Objects Without Getting a System.Windows.Forms.Control Pin
GeniusMchlahla18-Jan-10 7:34
GeniusMchlahla18-Jan-10 7:34 
AnswerRe: How to Select Drawn Objects Without Getting a System.Windows.Forms.Control Pin
J. Dunlap18-Jan-10 13:11
J. Dunlap18-Jan-10 13:11 
Which out of the following are you doing? I can give you an answer for any of them...

  • Hit-testing individual shapes - i.e. determining whether the mouse is over a shape, and if so, which one of the shapes on your control it is over.
  • Hit-testing the entire control - i.e. determining whether the mouse is over any drawn object on the control
  • Determining redraw or clipping areas

I'll assume the first one for now, so here's a fairly efficient way to hit-test to determine which individual shape, if any, the mouse (or any other single point) is over.

First, you need to set up code to create a GraphicsPath for the shape that matches what's being drawn. For example, a GraphicsPath for a set of circles could be created like this:
var gp = new GraphicsPath();
gp.AddEllipse(myCircleRect1); //bounds of the first ellipse to be drawn, as would be passed to Graphics.DrawEllipse()
gp.AddEllipse(myCircleRect2); //ditto


Now, you can call gp.IsVisible(pt) to determine if a given point is over any filled part (as opposed to the outline) of the shape if it is a filled shape, or gp.IsOutlineVisible(pt, pen) to determine if a point is within the outline of the shape if it is not filled. If you want to check both the outline and the inside of a shape at the same time, use gp.Widen(pen) when you create the graphics path, after adding the shapes. In either case, be sure that your pen has all the characteristics of the one being used to actually draw the shape - width, mitre mode, end caps, dash type, etc. The GraphicsPath hit testing methods take these into account, which saves you a huge amount of coding to determine the actual line's hit test area.

To make use of this efficiently, this is probably the best way:

1. Re-build the shape's graphics path when the shape and/or drawing pen changes. Use gp.GetBounds() to get the outer bounding rectangle of the graphics path. Make sure to either pass the pen to GetBounds(), or use gp.Widen() when creating it as I mentioned above. Store the bounds of the shape with the object itself. If you have a whole lot of shapes, don't keep the graphics path around - call Dispose() on it and get rid of it. If you have a smaller number of shapes, you might want to store the GraphicsPath along with the cached bounds.

2. When a mouse down event happens, search through your list of objects (sorted by z-order so that you check the topmost shapes first), and find the first bounding rectangle that contains the mouse cursor point. When you find one, either retrieve your stored graphics path or create a new one (as above), and use IsVisible() or IsOutlineVisible() as above to find out if the mouse is actually over the shape. If not, continue to check bounding rectangles until you find another one that the mouse is within, and test the graphics path on that one, etc.

If you store the graphics paths, sure to cache the bounding rectangle when an individual shape changes, instead of calling GetBounds() on each object each time the mouse is clicked. Otherwise you won't be saving some CPU expense that you could otherwise save. Also no matter what you do with the GraphicsPath objects (caching them or discarding them immediately), always call Dispose() when you're completely done with them, just like you do with Graphics, Pen, and Brush objects.

Also note that the bounding rectangle is good to do hit testing with, but if you're determining the drawn area for redraw purposes or for clipping, you'll need to widen the bounds by a pixel in each direction to account for the possibility of antialiasing effects that could be outside of the hit testing bounds.
GeneralRe: How to Select Drawn Objects Without Getting a System.Windows.Forms.Control Pin
GeniusMchlahla19-Jan-10 5:23
GeniusMchlahla19-Jan-10 5:23 
GeneralRe: How to Select Drawn Objects Without Getting a System.Windows.Forms.Control Pin
J. Dunlap19-Jan-10 6:27
J. Dunlap19-Jan-10 6:27 
QuestionEditing crystal reports at runtime Pin
kruegs3518-Jan-10 7:03
kruegs3518-Jan-10 7:03 
QuestionMessage Removed Pin
18-Jan-10 6:19
professionalN_tro_P18-Jan-10 6:19 
AnswerRe: Application Info and Error logging Pin
Eddy Vluggen18-Jan-10 7:19
professionalEddy Vluggen18-Jan-10 7:19 
GeneralMessage Removed Pin
18-Jan-10 8:19
professionalN_tro_P18-Jan-10 8:19 
GeneralRe: Application Info and Error logging Pin
Eddy Vluggen18-Jan-10 10:33
professionalEddy Vluggen18-Jan-10 10:33 
AnswerRe: Application Info and Error logging Pin
Not Active18-Jan-10 7:26
mentorNot Active18-Jan-10 7:26 
AnswerRe: Application Info and Error logging Pin
Bardy8518-Jan-10 8:33
Bardy8518-Jan-10 8:33 
AnswerRe: Application Info and Error logging Pin
Keith Barrow18-Jan-10 23:30
professionalKeith Barrow18-Jan-10 23:30 
QuestionPhone dialer application with C# Pin
Drs201018-Jan-10 4:16
Drs201018-Jan-10 4:16 
AnswerRe: Phone dialer application with C# Pin
Covean18-Jan-10 5:36
Covean18-Jan-10 5:36 
GeneralRe: Phone dialer application with C# Pin
Drs201018-Jan-10 13:11
Drs201018-Jan-10 13:11 
GeneralRe: Phone dialer application with C# [modified] Pin
Covean18-Jan-10 21:36
Covean18-Jan-10 21:36 
GeneralRe: Phone dialer application with C# Pin
Drs201019-Jan-10 3:22
Drs201019-Jan-10 3:22 
GeneralRe: Phone dialer application with C# Pin
Covean19-Jan-10 3:54
Covean19-Jan-10 3:54 
GeneralGood Pin
ADNANE-Dev18-Jan-10 4:11
ADNANE-Dev18-Jan-10 4:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.