c# - 在透明面板后面添加图片框

标签 c# winforms panel picturebox

我有两个面板叠在一起。下面的比上面的大一点。我正在使用最顶部面板上的 CreateGraphics() 方法绘制图像。 (需要明确的是,此图像是一个连接四个带有透明孔的网格)。现在我需要做的是将图片框添加到底部面板并让它从网格后面显示。

我正在将图片框控件添加到底部网格。我也在使用 BringToFront() 方法。我该怎么做才能让图片框显示在网格下方?

以下代码中:chipHolder为底部面板,grid为最顶部面板,picBox为图片框

public void addControl()
{
   chipHolder.Controls.Add(picBox);
   picBox.BringToFront();
}

// This piece of code is in a mouse_click event of grid 
Graphics g = grid.CreateGraphics();
addControl();

// to make the picture move downwards
for (int i = 0; i < newYloc; i++)
{
     picBox.Location = new Point(newXloc, picBox.Top + 1);
     picBox.Show();
}

// drawing the grid image on the grid panel
protected virtual void grid_Paint(object sender, PaintEventArgs e)
{
     Image img = Properties.Resources.grid_fw;

     gridGraphics = grid.CreateGraphics();
     gridGraphics.DrawImage(img, 0, 0, 650, 550);
}

为了获得更好的图片,这就是我的面板的样子。选择的是chipHolder面板。

enter image description here

最佳答案

您可以尝试不同的方法:不要使用Panel并使用单个PictureBox,这样您就可以在该PictureBox中绘制所有内容>。因此,您使用 PictureBox 的 MouseDown 事件处理程序来计算用户单击的(虚拟)单元格(您需要执行简单的除法),然后在 PictureBox 上绘制芯片。 >。如果您想显示芯片掉落的情况,则需要保存当前位图的副本(PictureBoxImage属性)并在不同的 y 坐标(从 0 到其在网格上的最终位置)上绘制芯片,这就像双缓冲技术一样。

这是一个小示例(您需要一个带有PictureBox表单,在本例中它被命名为“pictureBox2”):

public partial class Form1 : Form
{
    Bitmap chip = new Bitmap(40, 40, PixelFormat.Format32bppArgb);

    public Form1()
    {
        InitializeComponent();
        using (Graphics g = Graphics.FromImage(chip))
        {
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.FillEllipse(new SolidBrush(Color.FromArgb(128, 255, 80, 0)), 1, 1, 38, 38);
        }
        pictureBox2.Image = new Bitmap(pictureBox2.Width, pictureBox2.Height, PixelFormat.Format32bppArgb);
        using (Graphics g = Graphics.FromImage(pictureBox2.Image))
        {
            g.Clear(Color.Yellow);
        }
    }

    private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            Text = e.Location.ToString();
            using (Graphics g = Graphics.FromImage(pictureBox2.Image))
            {
                g.DrawImage(chip, e.Location.X - 20, e.Location.Y - 20);
            }
            pictureBox2.Invalidate();
        }
    }
}

如果您想要具有真正透明度的控件,您应该使用WPF(它提供更好的图形并使用硬件加速)。

关于c# - 在透明面板后面添加图片框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14348036/

相关文章:

c# - 获取 WebBrowser 控件的后退/前进历史记录

c# - 隐藏 TableLayoutPanel 中两个组合单元格之间的边框

javascript - 访问 WordPress 面板中的子菜单

c# - 为什么我的位图保存为两边都有黑条的 jpg?

c# - WCF,用我的IP替换“localhost”还是应该使用NetTcpBinding?

vb.net - 序列化/反序列化二维数组

c# - 在 Roslyn 中解析嵌入式表达式

java - 如何将面板拆分为子面板?

c# - 意外的@using 关键字

c# - WPF Caliburn MEF 应用程序和 DI