布局效果如下图:
- 首先在主窗体后台代码声明需要打开的窗体变量,代码如下:
1 public partial class Form1 : Form 2 { 3 Button btnShadow;//作为中间寄存button,用于显示 4 Home home; 5 Setting setting; 6 Record record; 7 Query query; 8 Statistics statistics;
- 主窗台构造函数内对实例化需要打开的窗体,并进行熟属性设置,代码如下:
1 public Form1() 2 { 3 InitializeComponent(); 4 //Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 25, 25)); 5 6 //初始化各窗口 7 home = new Home() { TopLevel = false, TopMost = true, Dock = DockStyle.Fill }; 8 setting = new Setting() { TopLevel = false, TopMost = true, Dock = DockStyle.Fill }; 9 record = new Record() { TopLevel = false, TopMost = true, Dock = DockStyle.Fill }; 10 query = new Query() { TopLevel = false, TopMost = true, Dock = DockStyle.Fill }; 11 statistics = new Statistics() { TopLevel = false, TopMost = true, Dock = DockStyle.Fill }; 12 13 //默认进入为主页和主按钮 14 LoadWindow(home); 15 SelectBarAndBtnShadow(this.button1); 16 }
- 创建加载窗体公共方法LoadWindow,具体代码如下:
1 private void LoadWindow(Form form) 2 { 3 4 this.panel3.Controls.Clear(); 5 form.FormBorderStyle = FormBorderStyle.None; 6 this.panel3.Controls.Add(form); 7 form.Show(); 8 }
- Statistics按钮切换页面的后台代码如下:
1 private void button2_Click(object sender, EventArgs e) 2 { 3 this.lblTitle.Text = "Statistics"; 4 SelectBarAndBtnShadow(sender); 5 LoadWindow(statistics); 6 7 }
- 选中侧边栏菜单是,显示选中状态的效果的代码如下:
1 private void SelectBarAndBtnShadow(object btn) 2 { 3 if (btnShadow != null) 4 { 5 btnShadow.BackColor = Color.FromArgb(24, 30, 54); 6 } 7 8 Button button = (Button)btn; 9 this.leftPanel.Height = button.Height; 10 this.leftPanel.Top = button.Top; 11 this.leftPanel.Left = button.Left; 12 13 14 btnShadow = button; 15 btnShadow.BackColor = Color.FromArgb(46, 51, 73); 16 17 }
标签:button,TopMost,TopLevel,切换,new,winform,true,btnShadow,页面 From: https://www.cnblogs.com/davisdabing/p/17810880.html