111,079
社区成员




public string nowtabName = ""; //当前tab
private void bt_lb_MouseDown(object sender, MouseEventArgs e)
{
this.bt_lb.Image = Properties.Resources.bt3;
this.nowtabName = "bt_lb";
}
private void bt_lb_MouseLeave(object sender, EventArgs e)
{
if (this.nowtabName != "bt_lb") {
this.bt_lb.Image = Properties.Resources.bt1;
}
}
private void bt_lb_MouseEnter(object sender, EventArgs e)
{
if (this.nowtabName != "bt_lb")
{
this.bt_lb.Image = Properties.Resources.bt2;
}
}
//离开
private void img_lb_MouseLeave(object sender, EventArgs e)
{
if (this.nowtabName != "img_lb")
{
this.img_lb.Image = Properties.Resources.img1;
}
}
//进入
private void img_lb_MouseEnter(object sender, EventArgs e)
{
if (this.nowtabName != "img_lb")
{
this.img_lb.Image = Properties.Resources.img2;
}
}
//按下
private void img_lb_MouseDown(object sender, MouseEventArgs e)
{
this.img_lb.Image = Properties.Resources.img3;
this.nowtabName = "img_lb";
}
public class MyLabel : Label
{
public Image ImageOnMouseEnter {get; set;}
public Image ImageOnMouseLeave{get; set;}
public Image ImageOnMouseDown{get; set;}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
this.Image= ImageOnMouseEnter; //<--
}
...
}
private void Form_init()
{
this.bt_lb.MouseLeave += new EventHandler(tab_lb_MouseLeave);
this.bt_lb.MouseEnter += new EventHandler(tab_lb_MouseEnter);
this.bt_lb.MouseDown += new MouseEventHandler(tab_lb_MouseDown);
this.img_lb.MouseLeave += new EventHandler(tab_lb_MouseLeave);
this.img_lb.MouseEnter += new EventHandler(tab_lb_MouseEnter);
this.img_lb.MouseDown += new MouseEventHandler(tab_lb_MouseDown);
}
private void tab_lb_MouseLeave(object sender,EventArgs e)
{
Label lb = (Label)sender;
if (this.nowtabName != lb.Name)
{
if (lb.Name == "bt_lb") { lb.Image = Properties.Resources.bt1; }
if (lb.Name == "img_lb") { lb.Image = Properties.Resources.img1; }
}
}
private void tab_lb_MouseEnter(object sender, EventArgs e)
{
Label lb = (Label)sender;
if (this.nowtabName != lb.Name)
{
if (lb.Name == "bt_lb") { lb.Image = Properties.Resources.bt2; }
if (lb.Name == "img_lb") { lb.Image = Properties.Resources.img2; }
}
}
private void tab_lb_MouseDown(object sender, EventArgs e)
{
Label lb = (Label)sender;
if (this.nowtabName != lb.Name)
{
if (lb.Name == "bt_lb") { lb.Image = Properties.Resources.bt3; }
if (lb.Name == "img_lb") { lb.Image = Properties.Resources.img3; }
if (this.nowtabName == "bt_lb") { this.bt_lb.Image = Properties.Resources.bt1; }
if (this.nowtabName == "img_lb") { this.img_lb.Image = Properties.Resources.img1; }
}
this.nowtabName = lb.Name;
}
我这个写法是对的。唉,就是不能把这些代码单独写到一个类里面去。
public partial class Form1 : Form
{
FlowLayoutPanel layoutPanel = new FlowLayoutPanel() { Dock = DockStyle.Fill};
public Form1()
{
for (int i = 0; i < 20; i++)
{
MyLabel lable = new MyLabel()
{
Text = "hello " + i,
Name = "MyLabel" + i,
};
this.layoutPanel.Controls.Add(lable);
}
this.Controls.Add(layoutPanel);
}
}
public class MyLabel : Label
{
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
this.BackColor = Color.PeachPuff;
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
this.BackColor = SystemColors.Control;
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
this.BackColor = Color.Plum;
}
}
CreateLables(){
for(int i=0;i<10;i++){
Label lb = new Label();
lb.Text = "标签" + (i+1);
lb.Name = "Label" + i;
lb.Click += (s,e) =>{ MessageBox.Show((s as Label).Text); }
}
}
谁便写写的,大概就是这样
Label LabelClick = (Label)sender;
LabelClick.BackColor = SystemColors.Info;
public string nowtabName = ""; //当前选项卡
private void Form_init()
{
this.bt_lb.MouseLeave += new EventHandler(tab_lb_MouseLeave);
this.bt_lb.MouseEnter += new EventHandler(tab_lb_MouseEnter);
this.bt_lb.MouseDown += new MouseEventHandler(tab_lb_MouseDown);
}
private void tab_lb_MouseLeave(object sender,EventArgs e)
{
Label lb = (Label)sender;
if (this.nowtabName != lb.Name)
{
if (lb.Name == "bt_lb") { lb.Image = Properties.Resources.bt1; }
if (lb.Name == "img_lb") { lb.Image = Properties.Resources.img1; }
}
}
private void tab_lb_MouseEnter(object sender, EventArgs e)
{
Label lb = (Label)sender;
if (this.nowtabName != lb.Name)
{
if (lb.Name == "bt_lb") { lb.Image = Properties.Resources.bt2; }
if (lb.Name == "img_lb") { lb.Image = Properties.Resources.img2; }
}
}
private void tab_lb_MouseDown(object sender, EventArgs e)
{
Label lb = (Label)sender;
if (this.nowtabName != lb.Name)
{
if (lb.Name == "bt_lb") { lb.Image = Properties.Resources.bt3; }
if (lb.Name == "img_lb") { lb.Image = Properties.Resources.img3; }
}
this.nowtabName = lb.Name;
}
代码没全贴,大概就是这么个意思。还是在主窗口堆积了很长的代码。我就像放到类里面。但是不想那种全动态创建控件实现,就是利用已经托到窗口上面的控件示例。不知该怎么搞。