我被要求编写一个允许对象堆叠在容器内的 GUI。用户将从自上而下的角度查看这些框。他们还希望能够看到里面堆放着什么。我解决这个问题的第一个想法是透明度。我已经阅读了一些关于透明度的帖子,但是我没有发现任何可以解决两个图像都是透明的问题,因此如果它们堆叠在一起,你可以看到它们。
如何使控件与顶部的其他控件透明。如果这真的不可能;解决这个问题的更好方法是什么?
我正在使用自定义控件(公共部分类 MyControl:Control)来解决该问题。我终于让控件上的图像透明了。在父窗体上绘制的任何内容都通过图像显示(我使用 onPaint 为父窗体绘制椭圆和正方形),但是放置在它上面的其他控件不再透明。
我用来完成此操作的代码如下:
public Image Image
{
get { return m_Image; }
set { m_Image = value; }
}
private void GridControl_Paint(object sender, PaintEventArgs e)
{
if (Image != null)
{
Graphics g = e.Graphics;
Bitmap bitMap = new Bitmap(Image);
//This is not working... the color array always comes back empty
//This is how I would rather making things transparent...
Color[] colorArray = bitMap.Palette.Entries;
if (colorArray.Length > 0)
{
ColorMap[] colorMap = new ColorMap[colorArray.Length];
for (int index = 0; index < colorArray.Length; index++)
{
colorMap[index] = new ColorMap();
colorMap[index].OldColor = colorArray[index];
colorMap[index].NewColor = Color.Transparent;
}
}
//Ok fine so the above is not working... let me force it.
//Get each pixel in the image and change the color.
else
{
for (int x = 0; x < bitMap.Width; x++)
{
for (int y = 0; y < bitMap.Height; y++)
{
Color pixelColor = bitMap.GetPixel(x, y);
Color newColor = Color.FromArgb(100, pixelColor);
bitMap.SetPixel(x, y, newColor);
}
}
}
bitMap.MakeTransparent();
g.DrawImage(bitMap, this.ClientRectangle);
}
}