繁体   English   中英

堆叠控件(Winforms C#)(透明)

[英]Stacking Controls (Winforms C#) (Transparent)

我被要求编写一个GUI,该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);
     }

  } 

您是否被锁定使用WinForms? 如果您需要做一些中等难度的图形工作(例如您所描述的),并且无法重新设计应用程序以解决问题,则可以迈入WPF。

自从我玩过GDI +图形以来已经有一段时间了,所以这是一个主意(但没有代码;)):

您堆叠的控件实际上需要工作吗? 如果不是,则可以将其外观复制到位图,将其隐藏,然后在自己的绘制例程中使用位图。 当控件需要工作时,您可以取消隐藏它。

顺便说一句,使用bitmap.GetPixel或SetPixel非常慢。 这是一些我操纵过的32位位图的旧代码(蓝色,绿色,红色和alpha是字节):

BitmapData bmpData = bitmap.LockBits(pixelRect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

unsafe
{
    byte* bytes = (byte*)bmpData.Scan0;
    for (int row = 0; row < pixelRect.Height; row++)
    {
        for (int col = 0; col < pixelRect.Width * 4; ) // * 4: 4 bytes per pixel 
        {
            bytes[row * bmpData.Stride + col++] = blue;
            bytes[row * bmpData.Stride + col++] = green;
            bytes[row * bmpData.Stride + col++] = red;
            bytes[row * bmpData.Stride + col++] = alpha;
        }
    }
}

// Unlock the bits.
bitmap.UnlockBits(bmpData);

我无法找到解决此问题的好方法。 我发现做到这一点的唯一方法是通过创建自定义控件并使用大量自定义代码(我仍然无法充分工作)。 我将关闭该线程,答案是:

在此论坛中回答太复杂了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM