[英]Use image Button with transparent color area
我有PNG的图片,透明和正常的颜色。
我用它来做一个Button:
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;
而当我点击透明区域时,它仍然算作单击按钮。
当我点击彩色区域时,如何才能使此按钮仅调用点击事件?
当我点击透明区域时,我希望它可以算作点击此按钮后面的对象。
您有两种选择:
使用带有Region
的Button
。
使用带有BackgroundImage
的Button
,并检查用户每次Click
如果你可以创建一个带有GraphicsPath
的Region
, 那么第一个选项是可行的,这意味着你需要从Graphics
primitves创建你需要的形状 ,如直线和曲线等。
如果您只有一个具有透明度的Bitmap
,那么最好不要使用带有Region
的Button
。
相反,您可以使用Button1
并在每次单击时检查单击像素的透明度。
如果它是透明的,你可以调用它下面控件的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
}
}
请注意,检查假定您具有正常布局,左上角的按钮图像没有缩放。 如果你需要缩放图像,你应该保留一个缩放的位图来进行检查。但是如果你可以使用非尺度图像,你应该这样做,因为这看起来会更好。
请注意我如何为下面的控件创建正确的MouseEventArgs
参数,因此您也可以访问该按钮或鼠标的位置。
另请注意,使用MouseClick
事件而不是Click
事件更容易,因为它已经具有鼠标位置。
如果您需要/想要使用Click
事件,则可以跳过创建EventArgs
,因为它没有有意义的数据; 从点击中传出e
以下是Click
事件的开始方式:
private void Button1_Click(object sender, EventArgs e)
{
// we need the location of the clicked pixel:
Point clickLocation = Button1.PointToClient(Control.MousePosition);
// the rest can proceed as above, just with simple EventArgs..
如果要检查所有鼠标单击事件并将其中的每一个传递给父项,则必须对它们进行全部编码。
首先让我们看一下MSDN上的事件顺序
- MouseDown事件。
- 点击活动。
- 鼠标点击
- MouseUp事件。
所以我们需要从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;
}
现在我们编写所有四个事件。 我们只需要调用一次hitTest
,然后在Click
事件中传递简单的,未修改的e
参数:
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
}
当然,您还需要为yourParent
编写所有四个事件的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.