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:
Beneath that box, is another picturebox which looks roughly like this:
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?
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