发表于 2024-8-12 10:36:20

C# Winform 多窗体切换方式一

一、简介

在 Winform 开辟中,多窗体的切换是一个常见的需求,比如登录成功后,切换至主界面,在网上查阅相关的资料,你会发现许多都是用 form2.Show();  this.Hide(); 这种方式,这种方式也存在一些问题,由于 Winform 存在一个主线程概念,如果主线程关闭,那么当前软件全部的窗体都会随之关闭,你也可以使用其他的方式,让界面继续表现,但整体上不是特别的优雅,这里保举两种方式,1.在 Form1 内里加载用户控件,并实时改变 Form1 界面巨细和用户控件保持同等,2.用一个窗体作为主线程启动,启动后隐藏界面,添加一个窗体管理类来实现其他的窗体的关闭和表现,如果末了一个表现的窗体关闭了,就关闭主线程退出程序,本篇文章我就使用第一种方式来实现结果。
结果:
https://i-blog.csdnimg.cn/direct/c96b8b3866c644e2bf6a027cbb8ec9c6.gif

二、实现结果

新建一个 Winform 项目,Form1 如下:
https://i-blog.csdnimg.cn/direct/90bce72987394c6394025056130b60a8.png
这里添加了背景颜色,主要作用是在后面切换用户控件时,观察是否严丝合缝。
添加了一个按钮,这个按钮用来切换用户控件。
另外,添加了三个用户控件:
1.Login

模仿登录界面
https://i-blog.csdnimg.cn/direct/57b16829ffbb460fb19ed068a66883e4.png
代码:
using System;
using System.Windows.Forms;

namespace Winform多窗体切换
{
    public partial class Login : UserControl
    {
      public Login()
      {
            InitializeComponent();
            this.ParentChanged += MyUserControl_ParentChanged;
      }

      private void Login_Load(object sender, EventArgs e)
      {
            //Console.WriteLine($"Login 宽度:{this.Size.Width},高度:{this.Size.Height}");
            Console.WriteLine("登录界面 Login_Load 方法");
      }

      private void MyUserControl_ParentChanged(object sender, EventArgs e)
      {
            // 检查控件是否被移除
            if (this.Parent == null)
            {
                // 在这里添加你的清理代码,例如,释放托管资源,取消事件订阅等
                Console.WriteLine("登录界面 Disposed 方法");
            }
            else
            {
                //在 Form1 添加当前用户控件时,这里会被执行
                Console.WriteLine("登录界面 Parent Changed: New Parent = " + this.Parent.Name);
            }
      }
    }
} 由于用户控件没有 FormClosing 方法,这里订阅 ParentChanged 方法来实现这一结果,我试着订阅其他许多方法,都没有用果。

2.Setting

模仿设置界面
https://i-blog.csdnimg.cn/direct/0118ff77215c4da482fcf48956549489.png
这里添加了四个按钮,用来判断用户控件是否表现完整
代码:
using System;
using System.Windows.Forms;

namespace Winform多窗体切换
{
    public partial class Setting : UserControl
    {
      public Setting()
      {
            InitializeComponent();
      }

      private void Setting_Load(object sender, EventArgs e)
      {
            //Console.WriteLine($"Setting 宽度:{this.Size.Width},高度:{this.Size.Height}");
            Console.WriteLine("设置界面 Login_Load 方法");
      }
    }
}
3.Main

用来模仿主界面
https://i-blog.csdnimg.cn/direct/8f8f1ef358c24ec8beb2464b4529e568.png
代码:
using System;
using System.Windows.Forms;

namespace Winform多窗体切换
{
    public partial class Main : UserControl
    {
      public Main()
      {
            InitializeComponent();
      }

      private void Main_Load(object sender, EventArgs e)
      {
            //Console.WriteLine($"Main 宽度:{this.Size.Width},高度:{this.Size.Height}");
            Console.WriteLine("Main 界面 Main_Load 方法");
      }
    }
}
Form1 代码:
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Winform多窗体切换
{
    public partial class Form1 : Form
    {
      public Form1()
      {
            InitializeComponent();
      }

      private int Index = 0;
      //高度页边距
      private int WidthMargins = 16;
      //高度页边距(无边框则为0)
      private int HeightMargins = 39;

      private void Form1_Load(object sender, EventArgs e)
      {

      }

      private void Form1_FormClosing(object sender, FormClosingEventArgs e)
      {

      }

      private void button1_Click(object sender, EventArgs e)
      {
            Index++;

            //防止测试按钮被移除
            foreach (Control c in this.Controls)
            {
                if (c.Name != "button1")
                  this.Controls.Remove(c);
            }

            if (Index == 1)
            {
                Login login = new Login();
                this.Controls.Add(login);
                this.Size = new Size(login.Width + WidthMargins, login.Height + HeightMargins);
            }
            else if (Index == 2)
            {
                Main main = new Main();
                this.Controls.Add(main);
                this.Size = new Size(main.Width + WidthMargins, main.Height + HeightMargins);
            }
            else if (Index == 3)
            {
                Setting setting = new Setting();
                this.Controls.Add(setting);
                this.Size = new Size(setting.Width + WidthMargins, setting.Height + HeightMargins);
            }

            if (Index >= 3)
                Index = 0;
      }
    }
} 运行:
https://i-blog.csdnimg.cn/direct/c96b8b3866c644e2bf6a027cbb8ec9c6.gif
源码不必要积分和付费哦
https://download.csdn.net/download/qq_38693757/89627203

竣事

如果这个帖子对你有所帮助,欢迎 关注 + 点赞 + 留言
end

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: C# Winform 多窗体切换方式一