Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
图片GUI C#(使用图片框)_C#_User Interface_Graphics_Picturebox - Fatal编程技术网

图片GUI C#(使用图片框)

图片GUI C#(使用图片框),c#,user-interface,graphics,picturebox,C#,User Interface,Graphics,Picturebox,是否有方法在图片框中的图片上执行单击事件?? (这样我就可以点击图片,让事情发生。) 我试过使用标签,但很难使用。 谢谢你的帮助 --标签代码-- 简单的回答是从Visual Studio的设计视图双击PictureBox控件,以便Visual Studio创建一个事件处理程序,然后订阅控件的单击事件 或者,您可以从编码中使用: 试试这个: namespace Today { public partial class Form1 : Form { public F

是否有方法在图片框中的图片上执行单击事件?? (这样我就可以点击图片,让事情发生。) 我试过使用标签,但很难使用。 谢谢你的帮助

--标签代码--


简单的回答是从Visual Studio的
设计视图
双击
PictureBox
控件,以便Visual Studio创建一个
事件处理程序
,然后
订阅
控件的
单击事件

或者,您可以从编码中使用:

试试这个:

namespace Today
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            pictureBox1.Click += pictureBox1_Click;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            //Code Here
        }
    }
}

您可以为
图片框的
单击
事件创建
事件处理程序。首先,在构造函数中“订阅”事件:

    public Form1()
    {
        InitializeComponent();

        pictureBox1.Click += pictureBox1_Click;
    }
然后,通过插入以下内容,可以创建一个名为
pictureBox1\u Click
的方法:

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        //Code
    }

编辑:当您单击
图片框内显示的图像时,
单击
事件将触发。或者,您可以订阅
MouseDown
MouseUp
有两种可能,您的
PictureBox
可以显示比
PictureBox
小的图像,在这种情况下,您希望仅单击图像的矩形将触发事件
单击
。另一种可能性是,当您的图像是透明图像(如车轮?)时,在这种情况下,仅单击图像的非透明部分将触发事件,换句话说,单击透明区域不会触发事件。下面的代码假设您的情况是后者,它当然比前者更难实现:

public class PictureBoxX : PictureBox
{
    static PropertyInfo ImageRectangle;
    static PictureBoxX() {
        ImageRectangle = typeof(PictureBox).GetProperty("ImageRectangle",
                          BindingFlags.NonPublic | BindingFlags.Instance);
    }        
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x202 || m.Msg == 0x201) {
            var imageRect = (Rectangle)ImageRectangle.GetValue(this, null);
            using (var bm = new Bitmap(imageRect.Width, imageRect.Height,
                         System.Drawing.Imaging.PixelFormat.Format32bppPArgb))
            using(var g = Graphics.FromImage(bm)) {
                int x = (int) (m.LParam.ToInt64() & 0xffff) - imageRect.X;
                int y = (int) (m.LParam.ToInt64() >> 16) - imageRect.Y;
                var rect = imageRect;
                rect.Location = Point.Empty;
                g.DrawImage(Image, rect);
                if (!imageRect.Contains(x, y)) return;
                else {
                    Color c = bm.GetPixel(x, y); 
                    //I choose the threshold 30 for the alpha channel
                    //the ideal threshold is 0, it's up to you.
                    if (c.A < 30) return;
                }
            }
        }
        base.WndProc(ref m);
    }
}

尝试上面的代码,您将看到仅单击图像的不透明部分将触发
单击
事件,并显示消息
“单击图像!”
。请注意,
BindingFlags
位于命名空间
System.Reflection
,您可以使用System.Reflection添加
为了方便。

我的意思是像点击屏幕上的图片一样picturebox@MahchinLizard:是的,当您单击picturebox上的图片时,会触发此单击事件。我知道有没有方法单击picturebox中的图片并执行某些操作。(对于gui)@MahchinLizard图片盒包含您在其中显示的图片。如果您单击该图片,将触发此事件,因此您可以使用方法
pictureBox1\u click
。但是您是否可以选择一个图像,以便如果单击它,您可以重新表述它?如果要在单击
PictureBox
时选择其他图像:
((PictureBox)发件人)。image=//要设置的图像
。如果要获取单击的图像:
image clickedImage=((PictureBox)sender).image您的问题确实有点让人困惑。这是一个固定的图像,你想打破热点,以执行特定的功能或。。。。
public class PictureBoxX : PictureBox
{
    static PropertyInfo ImageRectangle;
    static PictureBoxX() {
        ImageRectangle = typeof(PictureBox).GetProperty("ImageRectangle",
                          BindingFlags.NonPublic | BindingFlags.Instance);
    }        
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x202 || m.Msg == 0x201) {
            var imageRect = (Rectangle)ImageRectangle.GetValue(this, null);
            using (var bm = new Bitmap(imageRect.Width, imageRect.Height,
                         System.Drawing.Imaging.PixelFormat.Format32bppPArgb))
            using(var g = Graphics.FromImage(bm)) {
                int x = (int) (m.LParam.ToInt64() & 0xffff) - imageRect.X;
                int y = (int) (m.LParam.ToInt64() >> 16) - imageRect.Y;
                var rect = imageRect;
                rect.Location = Point.Empty;
                g.DrawImage(Image, rect);
                if (!imageRect.Contains(x, y)) return;
                else {
                    Color c = bm.GetPixel(x, y); 
                    //I choose the threshold 30 for the alpha channel
                    //the ideal threshold is 0, it's up to you.
                    if (c.A < 30) return;
                }
            }
        }
        base.WndProc(ref m);
    }
}
PictureBoxX pic = new PictureBoxX();
pic.Image = someTransparentImage;
pic.Click += (s,e) => {
   MessageBox.Show("Clicked on image!");
};