0

So the thing is, when I program things I would like to have click-events that are more complex than a rectangle(=>pictureBox). Imagine for example, the following picture, which is the graphical picturebox: enter image description here

Beneath that box, is another picturebox which looks roughly like this: enter image description here

You can see where I am going. The first image will be the only one that is visible, since the other one is directly beneath. When I click on the image, the coordinates are given to the second picturebox and depending, on what color has been clicked, something happens. (e.g. if I click the red area, a MessageBox says "you clicked the brown egg!")

Everything is ok until now, only one thing is off: If I click anywhere inside of the imaginary(!!!) purple area, then nothing happens! Which means what? I don't even know, maybe the coordinates are given in a wrong way? enter image description here

     public partial class Form1 : Form
{
    readonly Color BlueField = Color.FromArgb(255, 0, 0, 255);
    readonly Color GreenField = Color.FromArgb(255, 0, 255, 0);
    readonly Color RedField = Color.FromArgb(255, 255, 0, 0);
    public Form1()
    {
        InitializeComponent();
    }
    private void checkColor()
    {


        using (var bmp = new Bitmap(pictureBox2.Image))
        {
            int cursorX = Convert.ToInt32(Cursor.Position.X);
            int cursorY = Convert.ToInt32(Cursor.Position.Y);
            MessageBox.Show(" cursorXOld= " + cursorX + " cursorYOld= " + cursorY);
            MessageBox.Show(Convert.ToString(pictureBox2.Location.X)+","+ Convert.ToString(pictureBox2.Location.Y)+","+ Convert.ToString(pictureBox2.Image.Width)+","+ Convert.ToString(pictureBox2.Image.Height));
            if ((cursorX > pictureBox2.Location.X) && (cursorX < (pictureBox2.Location.X + pictureBox2.Image.Width)) && (cursorY > pictureBox2.Location.Y) && (cursorY < (pictureBox2.Location.Y + pictureBox2.Image.Height)))
            {
                //MessageBox.Show(" cursorXOld= "+ cursorX + " cursorYOld= " + cursorY);
                var colorAtMouse = bmp.GetPixel(cursorX - pictureBox2.Location.X, cursorY - pictureBox2.Location.Y);
                if (colorAtMouse.ToArgb() == BlueField.ToArgb())
                {
                    MessageBox.Show("Background clicked");
                }
                if (colorAtMouse.ToArgb() == GreenField.ToArgb())
                {
                    MessageBox.Show("White egg clicked");
                }
                if (colorAtMouse.ToArgb() == RedField.ToArgb())
                {
                    MessageBox.Show("Brown egg clicked");
                }
            }
        }
    }

In the end, what I need is a way to make complicated irregular hitboxes for things, without making everything ugly. What you see here is how I tried to fix the problem

I hope you can help me, since I've been literally working for days on this, and I reached the end of my capacities

Alexander
  • 33
  • 5
  • 1
    Use the e.Location property you get from the MouseDown event. – LarsTech Apr 02 '18 at 21:12
  • That results in the same problem, just in the upper left, and not the lower right. Extremely strange – Alexander Apr 03 '18 at 16:15
  • I don't know why you are doing this: `cursorX - pictureBox2.Location.X` It should just be `GetPixel(e.Location.X, etc...` If the hidden pictureBox has the same dimensions as the visible pictureBox, you shouldn't have to offset anything. – LarsTech Apr 03 '18 at 16:26
  • Finally! With your help I finally figured it out. The first part of the problem was the thing you said. The second part of the problem was that "if" that checked wether I even clicked the pictureBox. It had to go. – Alexander Apr 03 '18 at 16:53
  • This one: if ((cursorX > pictureBox2.Location.X) && (cursorX < (pictureBox2.Location.X + pictureBox2.Image.Width)) && (cursorY > pictureBox2.Location.Y) && (cursorY < (pictureBox2.Location.Y + pictureBox2.Image.Height))) { – Alexander Apr 03 '18 at 16:53
  • Thank you, can i give you an "upvote" or something like that? – Alexander Apr 03 '18 at 16:54
  • I just now realized that making this if-query was completely unnecessary and badly thought out. The function only gets triggered by the clicking on the picturebox, so why should i check if the pointer is in the pictureBox, of course it is :/ – Alexander Apr 03 '18 at 17:00

0 Answers0