c#在图片盒中使用不同颜色的Hitbox

时间:2018-04-02 21:03:23

标签: c# picturebox

所以问题是,当我编写程序时,我希望点击事件比矩形(=> pictureBox)更复杂。 想象一下,例如,下面的图片,即图形图片框:enter image description here

在那个方框下面,是另一个看起来大致如下的图片框: enter image description here

你可以看到我要去的地方。第一张图像将是唯一可见的图像,因为另一张图像位于正下方。当我点击图像时,坐标被赋予第二个图片框,并且根据点击的颜色,会发生一些事情。 (例如,如果我点击红色区域,一个MessageBox说"你点击了那个棕色蛋!")

到目前为止一切都还可以,只有一件事是关闭的: 如果我点击想象(!!!)紫色区域内的任何地方,那么什么都没发生!这意味着什么?我甚至不知道,坐标可能是以错误的方式给出的? 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");
                }
            }
        }
    }

最后,我需要的是一种为事物制作复杂的不规则命中箱的方法,而不会让一切变得丑陋。你在这里看到的是我试图解决问题的方法

我希望你能帮助我,因为我已经在这方面工作了好几天,而且我的能力已经达到最终

0 个答案:

没有答案