读书人

动态加载的picturebox为什么改变不了

发布时间: 2013-03-13 10:56:58 作者: rapoo

动态加载的picturebox,为什么改变不了背景,请高手指点,谢谢!
直接上代码了

 public partial class TestForm : Form
{
public TestForm()
{
InitializeComponent();
LoadTheTopMenu();
}
#region
private void LoadTheTopMenu()
{
int w = 0; //增加宽度

int wl = 0;
//int hl = 0;

int i = 1;
for (; i < 3; )
{
PictureBox pic_xt_01 = new PictureBox();
pic_xt_01.Location = new System.Drawing.Point(36 + w, 20);
pic_xt_01.Name = "pic_xt_0" + i;
pic_xt_01.Size = new System.Drawing.Size(78, 72);
pic_xt_01.TabIndex = 2;
pic_xt_01.TabStop = false;
pic_xt_01.Image = Properties.Resources.topMenu_01;
pic_xt_01.Cursor = System.Windows.Forms.Cursors.Hand;
pic_xt_01.MouseEnter += pic_xt_01_MouseEnter;
pic_xt_01.MouseLeave += pic_xt_01_MouseLeave;
pic_xt_01.Click += new EventHandler(pic_xt_01_Click);
Label lab_xt_01 = new Label();
lab_xt_01.AutoSize = true;
lab_xt_01.BackColor = System.Drawing.Color.Transparent;
lab_xt_01.Location = new System.Drawing.Point(62 + wl, 75);
lab_xt_01.Name = "lab_xt_0" + i;
lab_xt_01.Size = new System.Drawing.Size(89, 12);


lab_xt_01.TabIndex = 1;
lab_xt_01.Text = "测试";
lab_xt_01.Cursor = System.Windows.Forms.Cursors.Hand;

panelhead.Controls.Add(lab_xt_01);
panelhead.Controls.Add(pic_xt_01);
w = w + 100;
wl = wl + 100;
i++;
}
}
private void pic_xt_01_MouseEnter(object sender, EventArgs e)
{
setControlBackground((sender as PictureBox), Properties.Resources.topMenu_02);
}
private void pic_xt_01_MouseLeave(object sender, EventArgs e)
{
setControlBackground((sender as PictureBox), Properties.Resources.topMenu_01);
}
private void pic_xt_01_Click(object sender, EventArgs e)
{
setControlBackground((sender as PictureBox), Properties.Resources.topMenu_02);
}
private void setControlBackground(Control col, Bitmap image)
{
col.BackgroundImage = image;
}
#endregion
}


如果要实现背景透明为何不用Panel呢?Panel就可以背景透明,但像这种东西,个人感觉还是自定义控件比较好,最起码效率高,然后自己想要什么样的效率就都可以实现,不受自带控件的限制。
下以文章可以作为参考:
http://bbs.csdn.net/topics/390332398

读书人网 >C#

热点推荐