图片框 - 处理Click事件对图像的非透明区域 [英] PictureBox - Handle Click Event on Non-Transparent Area of Image

查看:457
本文介绍了图片框 - 处理Click事件对图像的非透明区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不在C#Windows窗体,其中两个图片框重叠。 TopPictureBox是包含一个透明的PNG图片。默认TopPictureBox可以通过点击TopPictureBox图像的任何可见的或透明的区域被点击。但我想做出TopPictureBox只能通过点击图像的可见区域,而不是在透明区域被点击。此外,我想做出光标只会改变形象,而不是在透明区域的可见区域。

I have to make a windows form in C# where two PictureBox are overlapping. TopPictureBox is containing a transparent png picture. By default TopPictureBox can be clicked by clicking any visible or transparent area of the image in TopPictureBox. But I want to make that TopPictureBox only can be clicked by clicking visible area of image, not in transparent area. Also I want to make that cursor will only change on the visible area of the image, not in transparent area.

有没有做这些什么办法?

IS THERE ANY WAY TO DO THESE?

我使用此代码,使TopPictureBox透明。

I am using this code to make TopPictureBox transparent.

TopPictureBox.BackColor = Color.Transparent;

感谢您的帮助。

< A HREF =http://i.stack.imgur.com/YAyTo.png相对=nofollow>

推荐答案

检查是否在图片框的位置透明或不依赖于图片 SizeMode 图片框的属性。

Checking if a position in PictureBox is Transparent or not depends on the Image and SizeMode property of PictureBox.

您不能简单地使用 GetPixel 位图,因为根据 SizeMode 的。你应该先检测图片根据 SizeMode 的大小和位置:

You can not simply use GetPixel of Bitmap because the image location and size is different based on SizeMode. You should first detect the size and location of Image based on SizeMode:

public bool HitTest(PictureBox control, int x, int y)
{
    var result = false;
    if (control.Image == null)
        return result;
    var method = typeof(PictureBox).GetMethod("ImageRectangleFromSizeMode",
      System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
    var r = (Rectangle)method.Invoke(control, new object[] { control.SizeMode });
    using (var bm = new Bitmap(r.Width, r.Height))
    {
        using (var g = Graphics.FromImage(bm))
            g.DrawImage(control.Image, 0, 0, r.Width, r.Height);
        if (r.Contains(x, y) && bm.GetPixel(x - r.X, y - r.Y).A != 0)
            result = true;
    }
    return result;
}



然后,你可以简单地使用的HitTest 检查方法,如果鼠标在图片框的非透明区域:

Then you can simply use HitTest method to check if the mouse is over a non-transparent area of PictureBox:

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (HitTest(pictureBox1,e.X, e.Y))
        pictureBox1.Cursor = Cursors.Hand;
    else
        pictureBox1.Cursor = Cursors.Default;
}

private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
    if (HitTest(pictureBox1, e.X, e.Y))
        MessageBox.Show("Clicked on Image");
}



另外设置背景色 Color.Transparent 不仅使图片框透明相对于它的父。例如,如果你有2 图片框表格设置透明背景色,只是因为你看到窗体的背景。为了使图片框支持透明背景,你应该吸取什么是控制自己的后面。你可以找到一个 TransparentPictureBox 在这篇文章:如何使用C#两个透明层?

Also setting BackColor to Color.Transparent only makes the PictureBox transparent relative to it's parent. For example if you have 2 PictureBox in a Form setting the transparent back color, just cause you see the background of form. To make a PictureBox which supports transparent background, you should draw what is behind the control yourself. You can find a TransparentPictureBox in this post: How to make two transparent layer with c#?

这篇关于图片框 - 处理Click事件对图像的非透明区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆