0

I want to get the x,y location on click over a picturebox.I have 2 pictureboxes and one is over the other.The small picturebox has a transparent region background(.png). I want to get the second's picturebox when I click on this area.

this is the code getting the x,y position:

  pictureBox2.MouseClick += (s, e) =>
        {
            if (e.Button == MouseButtons.Right)
            {
                MessageBox.Show(String.Format("Right Clicked2 at X: {0} Y: {1}", e.X, e.Y));
            }
            else if (e.Button == MouseButtons.Left)
            { MessageBox.Show(String.Format("Mouse Clicked2 at X: {0} Y: {1}", e.X, e.Y)); }
        };

        pictureBox1.MouseClick += (s, e) =>
        {
            if (e.Button == MouseButtons.Right)
            {
                MessageBox.Show(String.Format("Right Clicked at X: {0} Y: {1}", e.X, e.Y));
            }
            else if (e.Button == MouseButtons.Left)
            { MessageBox.Show(String.Format("Mouse Clicked at X: {0} Y: {1}", e.X, e.Y)); }
        };

enter image description here

with this code the second image shown(good showing).But when I cleck to the transparent area it gives me the message from the picturebox2 and not the picturebox1.Any idea how can assign this area to picturebox1?

     var pos = this.PointToScreen(pictureBox2.Location);
        pos = pictureBox1.PointToClient(pos);
        pictureBox2.Parent = pictureBox1;
        pictureBox2.Location = pos;
        pictureBox2.BackColor = Color.Transparent;

enter image description here

Apollon
  • 311
  • 7
  • 29
  • 2
    **Unrelated**: whatever you're trying to do here, winforms is not the right technology. If you want to create hardcore games, look at XNA, or MonoGame or Unity3D. If you want to create simpler 2D games you can use WPF. – Federico Berasategui Mar 09 '14 at 23:49
  • I dont understand what you are trying to do. What do you mean "Delete transparent background"? – γηράσκω δ' αεί πολλά διδασκόμε Mar 10 '14 at 00:27
  • I dont want to show this transparent area.and when this area is clicked I want to have picturebox1 clicked and not picturebox2 – Apollon Mar 10 '14 at 00:36
  • @HighCore isn't XNA "dead"? (At least according to the [XNA Wikipedia article](http://en.wikipedia.org/wiki/Microsoft_XNA)). – Uwe Keim Mar 10 '14 at 06:23
  • It's dead, it wasn't the right technology. Survival skills of gaming frameworks only compare favorably to the milk in my refrigerator :) The pictureBox1 mouse events are never going to fire, a simple workaround is to use Bitmap.GetPixel() and check the alpha. – Hans Passant Mar 11 '14 at 00:02

1 Answers1

1

Just use one picturebox and see if you are inside this area. The area is a semi cycle with center x0, 0. So calculate the

squareroot(y*y + (x0-x)*(x0-x)), x,y is where you click. 

If it is smaller from the radius you are inside otherwise outside.

Edit.

Create a graphic path

using System.Drawing.Drawing2D; //is needed for the graphics path

GraphicsPath gp = new GraphicsPath();

gp.AddEllipse (x, y, width, height); //x,y the upper left corner of the rect containing the ellipse

if( gp.IsVisible (e.X, e.Y) ){
    //is inside
}
else{
    //is outside
}

Note: The y coordinate will be negative

valter