直接上代码了
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
}
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
}
解决方案 »
- gmap.net在Winform下的使用
- DataGridView自定义设置绑定的值
- vs2003 (c#)+SQL2000, winform 中的datagrid添加、修改、删除、查询数据库操作如何做呢?
- ¥4,676.20如何转换成4676.20?
- 这两个方法有什么不同吗????
- Measurement
- AxWebBrowser控件调用excel的问题
- c# webbrowser 取SPAN值
- 关于Antechinus C# Editor的注册码,如正确100分一人独拿
- 一个加急问题!!!怎样取得这个数组的值????
- c#中,如何将实数转化为弧度???
- 点击打印预览里面的打印按钮再弹出打印设置窗口
{ col.Image = image;
}都用Image属性赋值
{ (col as PictureBox).Image = image;
}
this.Controls.Add(pic_xt_01);
lab_xt_01.Parent = pic_xt_01;
lab_xt_01.Location = new System.Drawing.Point(62 + wl, 75);//注意别超出picturebox的size
lab_xt_01.BackColor = System.Drawing.Color.Transparent;
如果要实现背景透明为何不用Panel呢?Panel就可以背景透明,但像这种东西,个人感觉还是自定义控件比较好,最起码效率高,然后自己想要什么样的效率就都可以实现,不受自带控件的限制。
下以文章可以作为参考:
http://bbs.csdn.net/topics/390332398
自己解决问题了吗?没有的话,我给你两个例子,其中一个我曾经模仿QQ的Tab,直接继承TabControl重绘的,另一个是别人写的,直接从ContainerControl类继承写的一个。没解决的话留个邮箱我就发给你