简体   繁体   中英

Picture GUI C# (Using picture boxes)

is there a way to do a click event on a picture in the picture box?? (so that i can click on a picture and have stuff happen.) Ive tried labels but there hard to use. Thanks for the help.

--Label Code--

namespace Today
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

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

simple answer for you is just double click on the PictureBox control from the Visual Studio Design View , so that Visual Studio create an Event Handler and subscribe it to Click Event of the PictureBox control.

or you can use from coding:

Try This:

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

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

You can create an Event Handler for the Click event for the Picturebox . First, "subscribe" to the event in your constructor:

    public Form1()
    {
        InitializeComponent();

        pictureBox1.Click += pictureBox1_Click;
    }

Then, you can make a method called pictureBox1_Click by inserting this:

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

Edit: The Click event will fire when you click on an image displayed inside the PictureBox . Alternatively, you can subscribe to MouseDown or MouseUp .

There are 2 possibilities, your PictureBox can show an image with smaller size than the size of PictureBox , in that case you want that clicking only on the Rectangle of the image will fire the event Click . The other possibility is when your image is a transparent image, such as a wheel?, in that case clicking only on the non-transparent part of your image will fire the event, in other words, clicking on the transparent region won't fire the event. The following code assumes your situation is the latter, it's of course even harder to implement than the former:

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);
    }
}

Usage :

PictureBoxX pic = new PictureBoxX();
pic.Image = someTransparentImage;
pic.Click += (s,e) => {
   MessageBox.Show("Clicked on image!");
};

Trying the code above, you will see that clicking only on the non-transparent of your image will fire the Click event and show the message "Clicked on image!" . Note that the BindingFlags is located in the namespace System.Reflection , you can add using System.Reflection; for convenience.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM