Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.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
C# 使用带有透明颜色区域的图像按钮_C#_Button_Png - Fatal编程技术网

C# 使用带有透明颜色区域的图像按钮

C# 使用带有透明颜色区域的图像按钮,c#,button,png,C#,Button,Png,我有一张PNG的图片,颜色是透明和普通的 我用这个按钮: this.Button1.BackColor = System.Drawing.Color.Transparent; this.Button1.BackgroundImage = Image; this.Button1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None; this.Button1.FlatAppearance.BorderSize = 0; this

我有一张PNG的图片,颜色是透明和普通的

我用这个按钮:

this.Button1.BackColor = System.Drawing.Color.Transparent;
this.Button1.BackgroundImage = Image;
this.Button1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
this.Button1.FlatAppearance.BorderSize = 0;
this.Button1.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Transparent;
this.Button1.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
this.Button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.Button1.Location = new System.Drawing.Point(0, 0);
this.Button1.Margin = new System.Windows.Forms.Padding(0);
this.Button1.Name = "Skin";
this.Button1.Size = new System.Drawing.Size(294, 194);
this.Button1.TabIndex = 7;
this.Button1.UseVisualStyleBackColor = false;
当我点击透明区域时,它仍然算作点击按钮

当我在彩色区域单击时,如何使此按钮仅调用单击事件

当我点击透明区域时,我希望它算作点击此按钮后面的对象。

您有两个选项:

  • 使用带有
    区域的
    按钮

  • 按钮
    背景图像一起使用
    ,并检查用户在每次
    点击时点击的内容

只有当您可以创建一个
区域时,选项一才是可行的,该区域采用
GraphicsPath
,这意味着您需要从
图形
原形(如直线和曲线等)创建所需的形状

如果您只有一个透明的
位图
,最好不要使用带有
区域的
按钮

相反,您可以使用
按钮1
并在每次单击时检查单击像素的透明度

如果它是透明的,则调用它下面控件的click事件

private void Button1_MouseClick(object sender, MouseEventArgs e)
{
    Size r = Button1.BackgroundImage.Size;
    // check that we have hit the image and hit a non-transparent pixel
    if ((e.X < r.Width && e.Y < r.Height) &&
            ((Bitmap)Button1.BackgroundImage).GetPixel(e.X, e.Y).A != 0)
    {
        Console.WriteLine("BUTTON clicked");  // test
        // do or call your click actions here!
    }
    // else pass the click on..
    else
    {
        // create a valid MouseEventArgs
        MouseEventArgs ee = new MouseEventArgs(e.Button, e.Clicks, 
                                e.X + Button1.Left, e.Y + Button1.Top, e.Delta);
        // pass it on to the stuff below us
        pictureBox1_MouseClick(pictureBox1, ee);

        Console.WriteLine("BUTTON NOT clicked");  // test
    }
}
如果您想检查所有鼠标单击事件并将它们传递给父级,则必须对它们全部进行编码

首先让我们看一下

  • 鼠标下移事件
  • 单击事件
  • 滑鼠夹
  • 鼠标移动事件
  • 所以我们需要从
    MouseDown
    开始。我们可以在helper函数
    hitTest
    中进行测试,这样我们就可以重复使用它了

    Button clickedButton = null;
    MouseEventArgs ee = null;
    
    void hitTest(Button btn, MouseEventArgs e)
    {
        Size r = btn.BackgroundImage.Size;
        // check that we have hit the image and hit a non-transparent pixel
        if ((e.X < r.Width && e.Y < r.Height) &&
                ((Bitmap)btn.BackgroundImage).GetPixel(e.X, e.Y).A != 0)
        {
            clickedButton = btn;
            ee = new MouseEventArgs(e.Button, e.Clicks, e.X + btn.Left, e.Y + btn.Top, e.Delta);
        }
        else clickedButton = null;
    }
    

    当然,您需要为您的父项对所有四个事件进行编码。
    此外。

    如果您可以获得排除透明区域的图形,则可以限制按钮的区域。不幸的是,你无法将位图转换为路径。是的,我搜索了好几天,但我没有看到解决方案。我真的很想在winform中创建一个自定义按钮,但我所要做的就是将该自定义按钮变成矩形…那么,您的图像看起来像什么?可以制作简单的形状,但复杂的图像无法精确工作。请参阅的开始部分(仅限),以获取将控件的reion限制为形状的示例。它看起来像一片叶子。请参阅我更新的简单答案!非常感谢你!它可以工作,我使用事件鼠标下键鼠标上键而不是点击..但是还有什么方法可以调用按钮的父级鼠标点击/鼠标上键/鼠标下键(所有鼠标事件)?。我希望所有的鼠标事件都转到父级,而不仅仅是鼠标点击。不,我不是说,我已经做了,我的意思是,即使它的父级没有这些鼠标事件句柄,我怎么能让它仍然在透明区域接收鼠标动作?我不明白。父母是什么?您只能调用代码中存在的事件..我不想调用事件。我的意思是,我们可以在某个位置调用单击“操作”,控件将检查该位置是否有单击事件。
    Button clickedButton = null;
    MouseEventArgs ee = null;
    
    void hitTest(Button btn, MouseEventArgs e)
    {
        Size r = btn.BackgroundImage.Size;
        // check that we have hit the image and hit a non-transparent pixel
        if ((e.X < r.Width && e.Y < r.Height) &&
                ((Bitmap)btn.BackgroundImage).GetPixel(e.X, e.Y).A != 0)
        {
            clickedButton = btn;
            ee = new MouseEventArgs(e.Button, e.Clicks, e.X + btn.Left, e.Y + btn.Top, e.Delta);
        }
        else clickedButton = null;
    }
    
    private void Button1_MouseDown(object sender, MouseEventArgs e)
    {
        hitTest(sender as Button, e);
        if (sender != clickedButton)
            yourParent_MouseDown((sender as Button).Parent, ee);
        else // do your stuff
    }
    
    private void Button1_Click(object sender, EventArgs e)
    {
        if (sender != clickedButton)
            yourParent_Click((sender as Button).Parent, e);
        else // do your stuff
    }
    
    private void Button1_MouseClick(object sender, MouseEventArgs e)
    {
        if (sender != clickedButton)
            yourParent_MouseClick((sender as Button).Parent, ee);
        else // do your stuff
    }
    
    private void Button1_MouseUp(object sender, MouseEventArgs e)
    {
        if (sender != clickedButton)
            yourParent_MouseUp((sender as Button).Parent, ee);
        else // do your stuff
    }