经过一天的冥思苦想,终于实现了,和大家分享一下:
首先在控件内加入多个picturebox,把它们的backcolor都设成透明的,下面的picturebox其上面picturebox的paraent,这一点很重要,否则就无法实现图像所谓“叠加”的功能了,然后通过程序加载已经是png格式的图像到各个picturebox控件内实现“叠加”。
代码写的有点笨,哈哈,仅供参考:
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Drawing.Imaging;
using
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Cutters_layout
{
public partial class Form1 : Form
{
public PictureBox[] pict = new PictureBox[8];
public Form1()
{
InitializeComponent();
SetStyle(ControlStyles.SupportsTransparentBackColor,true);
picBox_b1.Parent = picBox_back;
picBox_b2.Parent = picBox_b1;
picBox_b3.Parent = picBox_b2;
picBox_b4.Parent = picBox_b3;
picBox_b5.Parent = picBox_b4;
picBox_b6.Parent = picBox_b5;
picBox_b7.Parent = picBox_b6;
picBox_b1.BackColor = Color.Transparent;
picBox_b2.BackColor = Color.Transparent;
picBox_b3.BackColor = Color.Transparent;
picBox_b4.BackColor = Color.Transparent;
picBox_b5.BackColor = Color.Transparent;
picBox_b6.BackColor = Color.Transparent;
picBox_b7.BackColor = Color.Transparent;
}
private void checkbox(PictureBox pictBox, CheckBox checkboxNo)
{
string picRightName = pictBox.Name.ToString().Substring(pictBox.Name.Length - 1, 1);
int pictNo = Convert.ToInt16(picRightName);
if (checkboxNo.Checked == true)
{
pictBox.Image = pict[pictNo].Image;
}
else
{
pictBox.Image = null;
}
}
private void checkBox_B1_CheckedChanged(object sender, EventArgs e)
{
checkbox(picBox_b1, checkBox_B1);
}
private void checkBox_B2_CheckedChanged(object sender, EventArgs e)
{
checkbox(picBox_b2, checkBox_B2);
}
private void checkBox_B3_CheckedChanged(object sender, EventArgs e)
{
checkbox(picBox_b3, checkBox_B3);
}
private void checkBox_B4_CheckedChanged(object sender, EventArgs e)
{
checkbox(picBox_b4, checkBox_B4);
}
private void checkBox_B5_CheckedChanged(object sender, EventArgs e)
{
checkbox(picBox_b5, checkBox_B5);
}
private void checkBox_B6_CheckedChanged(object sender, EventArgs e)
{
checkbox(picBox_b6, checkBox_B6);
}
private void checkBox_B7_CheckedChanged(object sender, EventArgs e)
{
checkbox(picBox_b7, checkBox_B7);
}
private void Form1_Load(object sender, EventArgs e)
{
picBox_b1.Image.Save("pb1.png",ImageFormat.Png);
Image b1 = Image.FromFile("pb1.png");
picBox_b2.Image.Save("pb2.png", ImageFormat.Png);
Image b2 = Image.FromFile("pb2.png");
picBox_b3.Image.Save("pb3.png", ImageFormat.Png);
Image b3 = Image.FromFile("pb3.png");
picBox_b4.Image.Save("pb4.png", ImageFormat.Png);
Image b4 = Image.FromFile("pb4.png");
picBox_b5.Image.Save("pb5.png", ImageFormat.Png);
Image b5 = Image.FromFile("pb5.png");
picBox_b6.Image.Save("pb6.png", ImageFormat.Png);
Image b6 = Image.FromFile("pb6.png");
picBox_b7.Image.Save("pb7.png", ImageFormat.Png);
Image b7 = Image.FromFile("pb7.png");
string imageName = "pb";
for (int i = 1; i <= 7; i++)
{
imageName = "pb" + i.ToString()+".png";
pict[i] = new PictureBox();
pict[i].Image = Image.FromFile(imageName);
}
picBox_b1.Image = null;
picBox_b2.Image = null;
picBox_b3.Image = null;
picBox_b4.Image = null;
picBox_b5.Image = null;
picBox_b6.Image = null;
picBox_b7.Image = null;
}
}
}