4 Replies - 624 Views - Last Post: 18 July 2009 - 01:48 PM Rate Topic: -----

#1 cwhathappens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-July 09

C# Click Form drop pictures where clicked

Posted 18 July 2009 - 10:43 AM

Using C# I have drawn a picture in a rectangle box and click on a form and the picture appears.

What I need to do is click and the same picture is dropped on the form anywhere on the form where the mouse is clicked.

I have been looking for is a tutorial somewhere on the web that explains how to do this without much luck.

Does anyone have a link(s)?

Thanks in Advance
Is This A Good Question/Topic? 0
  • +

Replies To: C# Click Form drop pictures where clicked

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: C# Click Form drop pictures where clicked

Posted 18 July 2009 - 11:19 AM

Take a look at Form's MouseClick event, maybe that could help you:

		private void Form1_MouseClick(object sender, MouseEventArgs e) {
			// e.X and e.Y = coordinates where mouse was clicked
			Rectangle myRect = new Rectangle(e.X, e.Y, 50, 50);
		}


This post has been edited by janne_panne: 18 July 2009 - 11:20 AM

Was This Post Helpful? 0
  • +
  • -

#3 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 942
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: C# Click Form drop pictures where clicked

Posted 18 July 2009 - 12:54 PM

I think this might be the code you are trying to draw. If I'm wrong, could you post the code you are trying to draw:

       private void drawImage_Click(object sender, EventArgs e)
        {
                       
            Pen pn = new Pen(Color.Blue);
            Rectangle rect = new Rectangle(20, 20, 100, 100);
            g.DrawArc(pn, rect, 0, 180);

            Point p1 = new Point(40, 50);
            Point p2 = new Point(40, 80);
            g.DrawLine(pn, p1, p2);

            Point p3 = new Point(100, 50);
            Point p4 = new Point(100, 80);
            g.DrawLine(pn, p3, p4);

            Font fnt = new Font("Verdana", 16);
            g.DrawString("Click on Form", fnt, new SolidBrush(Color.Blue), 14, 10);
        }




You will see, as janne_panne, posted. The MouseEventArgs have an X and Y property. You can use this to draw your picture. If the code above is your code, you can replace the specific values with e.X and e.Y to try and get what it is you are looking for. This would be a simple example of what you could try.

       private void drawImage_Click(object sender, EventArgs e)
        {
                       
            Pen pn = new Pen(Color.Blue);
            // This will create the rectangle at position of the mouse, plus 20 units
            Rectangle rect = new Rectangle(e.X + 20, e.Y + 20, 100, 100);
            g.DrawArc(pn, rect, 0, 180);

            // This will create a point at the position of the mouse
            // plus the number of units you are using
            Point p1 = new Point(e.X + 40, e.Y + 50);
            Point p2 = new Point(e.X + 40, e.Y + 80);
            g.DrawLine(pn, p1, p2);

            // Same this as above
            Point p3 = new Point(e.X + 100, e.Y + 50);
            Point p4 = new Point(e.X + 100, e.Y + 80);
            g.DrawLine(pn, p3, p4);

            Font fnt = new Font("Verdana", 16);
            g.DrawString("Click on Form", fnt, new SolidBrush(Color.Blue), e.X + 14, e.Y + 10);
        }




To help you understand it. Think that when you were doing your original drawing, e.X and e.Y were both zero. That way, you would have your original drawing. By adding e.X and e.Y to what you have, it has the effect of shifting all of your coordinates by that value.

Hope that clears up some confusion. :)

*edit*
I noticed I didn't shift the coordinates of the rectangle

This post has been edited by SixOfEleven: 18 July 2009 - 12:56 PM

Was This Post Helpful? 0
  • +
  • -

#4 cwhathappens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-July 09

Re: C# Click Form drop pictures where clicked

Posted 18 July 2009 - 01:25 PM

[quote name='SixOfEleven' date='18 Jul, 2009 - 11:54 AM' post='706210']
I think this might be the code you are trying to draw. If I'm wrong, could you post the code you are trying to draw:

Yes that is the code I am using and now it works like I want it to.

	   
 private void drawImage_MouseClick_1(object sender, MouseEventArgs e)
		{
			Pen pn = new Pen(Color.Blue);
			Rectangle rect = new Rectangle(e.X, e.Y, 20, 20);
			g.DrawArc(pn, rect, 0, 360);
			Point p1 = new Point(e.X + 10, e.Y + 20); // draw body
			Point p2 = new Point(e.X + 10, e.Y + 40);
			g.DrawLine(pn, p1, p2);
			Point p3 = new Point(e.X + 0, e.Y + 30);  // draw arms
			Point p4 = new Point(e.X + 20, e.Y + 30);
			g.DrawLine(pn, p3, p4);
			Point p5 = new Point(e.X + 10, e.Y + 40);  // draw legs
			Point p6 = new Point(e.X + 0, e.Y + 50);
			g.DrawLine(pn, p5, p6);
			Point p7 = new Point(e.X + 10, e.Y + 40);  // draw legs
			Point p8 = new Point(e.X + 20, e.Y + 50);
			g.DrawLine(pn, p7, p8);
		}



I was using what janne_panne had suggested and it worked but it would not work without adding or subtracting from the rest of the coordinates after the rectangle, like you have shown.

Now you know how much of a newbie I am.

Thanks to both of you.

What is the possibility of putting the image into a bounding box? Is that really difficult to learn right now? I would really like to know more how to incorporate it.
Was This Post Helpful? 0
  • +
  • -

#5 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 942
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: C# Click Form drop pictures where clicked

Posted 18 July 2009 - 01:48 PM

Here is what you could look into.

You can take the e.X and e.Y values as the starting point of the bounding box. Then, you would have to find out what the width and the height of the image you are drawing is. You could then create a rectangle and draw the rectangle.

You would do something like this:

Rectangle myBox = new Rectangle(e.X, e.Y, imageWidth, imageHeight);

g.DrawRectangle( ... ); 
// I don't have C# loaded at the moment and can't recall the exact parameters for DrawRectangle



Give that a try, if you can't get it to work, post your code and I'm sure somebody will try and help you get it right. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1