如何制作透明面板的图片框?

时间:2018-02-05 03:12:24

标签: c# winforms panel transparency

我正在制作一个Windows游戏的2D游戏,我有一个问题...... 所以,我有一个Panel,其中所有的“地面”瓷砖(PictureBox [])都是...但不仅仅是设置PictureBox.Image,我需要在“Ground Tiles”上方有另一个Panel,所以它可以看起来就像玩家背后的东西......但是那个面板需要透明,它的图片框需要看起来才能产生效果,但它没有...我把Tiles(PictureBox)放在Panel里面的原因,它是因为我想让镜头聚焦在玩家身上的幻觉(意味着玩家并没有真正移动,其移动的是面板)。

有人能帮帮我吗?我知道它看起来很复杂......抱歉......

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using World;

namespace World
{

  public partial class LoadWorld : Form
  {

    public int TileSizeX = 30;
    public int TileSizeY = 30;
    public int TileT = 2500;
    public int TileX = 49;
    public int TileY = 49
    //Here's the problematic panel
    public Panel PropPanel = new Panel();
    public PictureBox[] WorldTile = new PictureBox[2500];
    public PictureBox[] PropTile = new PictureBox[2500];
    public static Dictionary<PictureBox, int[]> tileProp = new Dictionary<PictureBox, int[]>();
    public LoadWorld()
    {
        InitializeComponent();

    }
    private void LoadWorld_Load(object sender, EventArgs e)
    {
        //settings of the PropPanel
        PropPanel.BorderStyle = BorderStyle.None;
        PropPanel.BringToFront();
        PropPanel.BackColor = Color.Transparent;
        PropPanel.Visible = true;
        PropPanel.Name = "PropPanel";
        PropPanel.TabIndex = 0;
        int valueA = 0;
        int valueX = 0;
        int valueY = 0;
        foreach (PictureBox pictureBoxValue in WorldTile)
        {
            if(valueY < TileY)
            {
                if(valueX < TileX)
                {
                    WorldTile[valueA] = new PictureBox()
                    {
                        Size = new Size(TileSizeX, TileSizeY),
                        Location = new Point(valueX * 30, valueY * 30),
                        BackgroundImage = new Bitmap("Sprites/null.png"),
                        Visible = true,
                        Name = "tile" + valueA
                    };
                    WorldMap.Controls.Add(WorldTile[valueA]);
                    valueA = valueA + 1;
                    valueX = valueX + 1;
                }
                else if(valueX == TileX)
                {
                    WorldTile[valueA] = new PictureBox()
                    {
                        Size = new Size(TileSizeX, TileSizeY),
                        Location = new Point(valueX * 30, valueY * 30),
                        BackgroundImage = new Bitmap("Sprites/null.png"),
                        Visible = true,
                        Name = "tile" + valueA,
                     };
                    WorldMap.Controls.Add(WorldTile[valueA]);
                    valueA = valueA + 1;
                    valueY = valueY + 1;
                    valueX = 0;
                }
            }
            else if(valueY == TileY)
            {
                if (valueX < TileX)
                {
                    WorldTile[valueA] = new PictureBox()
                    {
                        Size = new Size(TileSizeX, TileSizeY),
                        Location = new Point(valueX * 30, valueY * 30),
                        BackgroundImage = new Bitmap("Sprites/null.png"),
                        Visible = true,
                        Name = "tile" + valueA,
                    };
                    WorldMap.Controls.Add(WorldTile[valueA]);
                    valueX = valueX + 1;
                    valueA = valueA + 1;
                }
                else if (valueX == TileX)
                {
                    WorldTile[valueA] = new PictureBox()
                    {
                        Size = new Size(TileSizeX, TileSizeY),
                        Location = new Point(valueX * 30, valueY * 30),
                        BackgroundImage = new Bitmap("Sprites/null.png"),
                        Visible = true,
                        Name = "tile" + valueA,

                    };
                    WorldMap.Controls.Add(WorldTile[valueA]);
                }
            }
        }
        SetGroundTiles();
        SetPropTiles();
    }

    public void SetGroundTiles()
    {
        SGT(1, "grass");
        SGT(2, "DirtPathVSideL");
        SGT(3, "DirtPathCenter");
        SGT(4, "DirtPathVSideR");

        //Road
        SGT(9, "SidewalkMiddle");
        SGT(10, "Asphalt");
        SGT(11, "Asphalt");
        SGT(12, "Asphalt2YL");
        SGT(13, "Asphalt");
        SGT(14, "Asphalt");
        SGT(15, "GrassConcreteHL");
        SGT(59, "SidewalkMiddle");
        SGT(60, "Asphalt");
        SGT(61, "Asphalt");
        SGT(62, "Asphalt2YL");
        SGT(63, "Asphalt");
        SGT(64, "Asphalt");
        SGT(65, "GrassConcreteHL");
        SGT(109, "SidewalkMiddle");
        SGT(110, "Asphalt");
        SGT(111, "Asphalt");
        SGT(112, "Asphalt2YL");
        SGT(113, "Asphalt");
        SGT(114, "Asphalt");
        SGT(115, "GrassConcreteHL");

    }
    public void SetPropTiles()
    {
        SPT(9, "SmallTree2", true, false);
        SPT(60, "SmallTree1", false, true);
    }
    public void SGT (int valueA, string texture)
    {
        WorldTile[valueA].BackgroundImage = new Bitmap("Sprites/" + texture + ".png");
    }
    public void SPT(int valueA, string texture, bool Collide, bool IsOverlaying)
    {

        PropTile[valueA] = new PictureBox
        {
            Name = "Panel" + valueA,
            Image = new Bitmap("Sprites/" + texture + ".png"),
            Location = WorldTile[valueA].Location,
            Size = WorldTile[valueA].Size,
            Visible = true,
            BackColor = Color.Transparent,

        };
        //Where PropPanel is being used
        PropTile[valueA].BorderStyle = BorderStyle.None;
        PropPanel.BringToFront();
        PropPanel.Controls.Add(PropTile[valueA]);
        WorldMap.Controls.Add(PropPanel);


    }
  }
}

就像在另一个面板上覆盖透明面板一样...... 我试图让第二个面板透明但它的图像没有出现。

view of the tiles

This may help you to understand

我想要做的基本上是3层面板...

PropTextures(就像玩家背后的东西一样)

播放器纹理/ PropTextures(与对象碰撞)

地面纹理(玩家可以走或不走的地方)

1 个答案:

答案 0 :(得分:0)

PictureBox控件不提供透明度,即您尝试使用它的方式。如果你继续使用目前的方法,我认为你不会有太多运气。

所以这里是替代方法的提示。如果您在使用PictureBox控件等操作时遇到困难,那么您可以自己进行渲染。创建一个DoubleBuffered的UserControl,并在Paint事件中,为每个sprite调用Graphics.DrawImage将每个图像渲染到正确的位置。这种技术的好处在于,当以这种方式绘制精灵时,您将获得正确的alpha校正合成。只要务必对它们进行分类,以便从后到前画出它们,然后画上&#34;使用前景精灵的背景精灵。