[英]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.