Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. detecting mouseclick in arraylist

detecting mouseclick in arraylist

Scheduled Pinned Locked Moved C#
graphicsdata-structureshelptutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cyn8
    wrote on last edited by
    #1

    Hi, i need some help in figuring this out. i have a class as shown below: public interface IDrawable { void Draw( Graphics g ); int CountNo(ArrayList al); } public class Ellipse : IDrawable { public Color c = Color.Black; public Point l = new Point(0, 0); public Size s = new Size(0, 0); public int stroke = 0; public Ellipse( Color c, Point l, Size s, int stroke ) { this.c = c; this.l = l; this.s = s; this.stroke = stroke; } public void Draw( Graphics g ) { g.DrawEllipse( new Pen( c, stroke ), l.X ,l.Y, s.Width,s.Height ); } in the form: private ArrayList alDrawingObjects = new ArrayList(); ....... alDrawingObjects.Add( new Rect( Color.Black, new Point(pt.X, pt.Y), new Size(50, 50), 5 ) ); alDrawingObjects.Add( new Rect( Color.Black, new Point(pt.X, pt.Y), new Size(50, 50), 5 ) ); ....... private void form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics objGraphics ; //You can't modify e.Graphics directly. objGraphics = e.Graphics; foreach ( IDrawable d in alDrawingObjects ) { d.Draw( objGraphics ); } // Free up resources. objGraphics.Dispose(); } i need to be able to select with a mouseclick on any of the shape drawn. I thought of using Contain method but i don't know how to implement it. i know that i need to search through the array list and test if the mouseclick point or position is in the drawn shape. may i know how to implement this? Thank u in advance.

    C 1 Reply Last reply
    0
    • C cyn8

      Hi, i need some help in figuring this out. i have a class as shown below: public interface IDrawable { void Draw( Graphics g ); int CountNo(ArrayList al); } public class Ellipse : IDrawable { public Color c = Color.Black; public Point l = new Point(0, 0); public Size s = new Size(0, 0); public int stroke = 0; public Ellipse( Color c, Point l, Size s, int stroke ) { this.c = c; this.l = l; this.s = s; this.stroke = stroke; } public void Draw( Graphics g ) { g.DrawEllipse( new Pen( c, stroke ), l.X ,l.Y, s.Width,s.Height ); } in the form: private ArrayList alDrawingObjects = new ArrayList(); ....... alDrawingObjects.Add( new Rect( Color.Black, new Point(pt.X, pt.Y), new Size(50, 50), 5 ) ); alDrawingObjects.Add( new Rect( Color.Black, new Point(pt.X, pt.Y), new Size(50, 50), 5 ) ); ....... private void form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics objGraphics ; //You can't modify e.Graphics directly. objGraphics = e.Graphics; foreach ( IDrawable d in alDrawingObjects ) { d.Draw( objGraphics ); } // Free up resources. objGraphics.Dispose(); } i need to be able to select with a mouseclick on any of the shape drawn. I thought of using Contain method but i don't know how to implement it. i know that i need to search through the array list and test if the mouseclick point or position is in the drawn shape. may i know how to implement this? Thank u in advance.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      cyn8 wrote:

      Graphics objGraphics ; //You can't modify e.Graphics directly. objGraphics = e.Graphics;

      Bizarre. This is totally superfluous, AFAIK. What you need to do is, in your mouse down event, iterate over the objects again, and check if the mouse is inside any of them. Obviously, for an ellipse, you need to read up on the maths to find out if a point is inside an ellipse. One cheats way of doing it, is to create a new bitmap, draw the objects onto it in white, and check if your mousedown point is white or black as you draw each one. This is very inefficient, really should only be used where you can't work it out mathematically ( for example if you had a completely irregular shape )

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      C 1 Reply Last reply
      0
      • C Christian Graus

        cyn8 wrote:

        Graphics objGraphics ; //You can't modify e.Graphics directly. objGraphics = e.Graphics;

        Bizarre. This is totally superfluous, AFAIK. What you need to do is, in your mouse down event, iterate over the objects again, and check if the mouse is inside any of them. Obviously, for an ellipse, you need to read up on the maths to find out if a point is inside an ellipse. One cheats way of doing it, is to create a new bitmap, draw the objects onto it in white, and check if your mousedown point is white or black as you draw each one. This is very inefficient, really should only be used where you can't work it out mathematically ( for example if you had a completely irregular shape )

        Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        C Offline
        C Offline
        cyn8
        wrote on last edited by
        #3

        What you need to do is, in your mouse down event, iterate over the objects again,... can you explain further? The problem is that for all the shape drawn before, i can't seem to save the area which they contained. if i mouseclick on the form, how can i check that the mouse click in within the area of the previously drawn shape. Please help, i've been cracking my head for hours. Thanks

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups