我一直在試圖顯示一個具有透明邊框作爲控件背景的圖像。帶透明背景的繪圖控制
不幸的是,透明的區域會在父表孔如下:
在上圖中,形式有哪些我所希望看到我在背後控制紅色背景透明區域。
我使用的代碼如下:
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
if (this.Image != null)
{
Graphics g = Graphics.FromImage(this.Image);
ImageAttributes attr = new ImageAttributes();
//set the transparency based on the top left pixel
attr.SetColorKey((this.Image as Bitmap).GetPixel(0, 0), (this.Image as Bitmap).GetPixel(0, 0));
//draw the image using the image attributes.
Rectangle dstRect = new Rectangle(0, 0, this.Image.Width, this.Image.Height);
e.Graphics.DrawImage(this.Image, dstRect, 0, 0, this.Image.Width, this.Image.Height,
GraphicsUnit.Pixel, attr);
}
else
{
base.OnPaint(e);
}
}
protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
{
//base.OnPaintBackground(e);
}
這類是從一個PictureBox繼承因爲我需要它實現的OnMouseMove和OnMouseUp活動的控制。
我一直在研究大部分時間沒有成功測試出不同的想法,但不幸的是,大多數只能在完整的框架上工作,而不是.Net CF.
任何想法將不勝感激。
哇,謝謝你。一個非常有用和詳細的解釋。你肯定花了一些時間在這個問題上。 –
@ctacke我想出了一個非常接近你的透明度解決方案,它可以在設計器和運行時使用。我最近意識到我的解決方案在嵌套容器控件時不起作用,因爲「Parent.Parent」不起作用。我嘗試將「Parent.Parent」切換到this.TopLevelControl,它在運行時工作,但不是在設計時。在哪裏你可以得到一個解決方案來在Container控件中嵌套透明控件,並且仍然讓設計者使控件變得透明? –
幾年前,我放棄了甚至試圖爲我的控件獲得設計師支持。它從來沒有覺得這對我很重要,而且總是很脆弱。有時候它會起作用,其他時候它不會,我發現自己燃燒了幾天,實際上沒有做出什麼高效率的事情,所以我甚至從來沒有打算控制設計的矩形,甚至從不打擾設計師。 – ctacke