public partial class Main : Form
{
private int currentFormIndex = 1;//当前所在的tab页
//防止闪屏
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000;
return cp;
}
}
public Main()
{
InitializeComponent();
SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲,防止闪屏
}
/// <summary>
/// 初始化Tab页面
/// </summary>
/// <param name="fr"></param>
private void InitChildForm(Form fr)
{
fr.FormBorderStyle = FormBorderStyle.None;
fr.Width = 800;
fr.Height = 580;
fr.Location = new Point(108, 106);
fr.TopLevel = false;
this.Controls.Add(fr);
fr.Show();
}
/// <summary>
/// TAB页面转换
/// </summary>
/// <param name="formIndex"></param>
private void SwitchForm(int formIndex)
{
if (currentFormIndex == formIndex)
{
return;
}
else
{
//关闭当前打开的窗口
foreach (Control c in this.Controls)
{
if (c is Form)
{
Form f = (Form)c;
f.Close();
}
}
Form fr = null;
switch (formIndex)
{
case 1:
{
fr = new Fm1();
InitChildForm(fr);
break;
}
case 2:
{
fr = new Fm2();
InitChildForm(fr);
break;
}
case 3:
{
fr = new Fm3();
InitChildForm(fr);
break;
}
case 4:
{
fr = new Fm4();
InitChildForm(fr);
break;
}
default: break;
}
currentFormIndex = formIndex;//将页码设为当前Tab页
}
}
private void Main_Load(object sender, EventArgs e)
{
Fm1 fr = new Fm1();
InitChildForm(fr);
ShowPanel(1);
}
private void btnPanel1_Click(object sender, EventArgs e)
{
ShowPanel(1);
SwitchForm(1);
}
private void btnPanel2_Click(object sender, EventArgs e)
{
ShowPanel(2);
SwitchForm(2);
}
private void btnPanel3_Click(object sender, EventArgs e)
{
ShowPanel(3);
SwitchForm(3);
}
private void btnPanel4_Click(object sender, EventArgs e)
{
ShowPanel(4);
SwitchForm(4);
}
private void ShowPanel(int panelIndex)
{
int minSize = btnPanel1.Height;
switch(panelIndex)
{
case 1:
panel1.Height = 150;
btnPanel1.BackgroundImage = Properties.Resources.btnTitleU;
panel2.Height = minSize;
panel3.Height = minSize;
panel4.Height = minSize;
btnPanel1.BackgroundImage = Properties.Resources.btnTitleU;
btnPanel2.BackgroundImage = Properties.Resources.btnTitleD;
btnPanel3.BackgroundImage = Properties.Resources.btnTitleD;
btnPanel4.BackgroundImage = Properties.Resources.btnTitleD;
break;
case 2:
panel1.Height = minSize;
panel2.Height = 150;
panel3.Height = minSize;
panel4.Height = minSize;
btnPanel1.BackgroundImage = Properties.Resources.btnTitleD;
btnPanel2.BackgroundImage = Properties.Resources.btnTitleU;
btnPanel3.BackgroundImage = Properties.Resources.btnTitleD;
btnPanel4.BackgroundImage = Properties.Resources.btnTitleD;
break;
case 3:
panel1.Height = minSize;
panel2.Height = minSize;
panel3.Height = 150;
panel4.Height = minSize;
btnPanel1.BackgroundImage = Properties.Resources.btnTitleD;
btnPanel2.BackgroundImage = Properties.Resources.btnTitleD;
btnPanel3.BackgroundImage = Properties.Resources.btnTitleU;
btnPanel4.BackgroundImage = Properties.Resources.btnTitleD;
break;
case 4:
panel4.Height = 150;
panel1.Height = minSize;
panel2.Height = minSize;
panel3.Height = minSize;
btnPanel1.BackgroundImage = Properties.Resources.btnTitleD;
btnPanel2.BackgroundImage = Properties.Resources.btnTitleD;
btnPanel3.BackgroundImage = Properties.Resources.btnTitleD;
btnPanel4.BackgroundImage = Properties.Resources.btnTitleU;
break;
default: break;
}
panel2.Location = new Point(panel1.Location.X, panel1.Location.Y + panel1.Height);
panel3.Location = new Point(panel2.Location.X, panel2.Location.Y + panel2.Height);
panel4.Location = new Point(panel3.Location.X, panel3.Location.Y + panel3.Height);
}
将窗口设置成另一个窗口的子窗口并切换,不闪屏
最新推荐文章于 2023-02-27 10:54:47 发布