c#winform程序实现锁存的功能

当C#窗口程序退出后,下一次打开后程序可以恢复到上一次的运行状态?

只能自定义保存一些必要的状态到数据库或本地,下次打开的时候读取恢复

【以下回答由 GPT 生成】

解决方案:使用配置文件保存窗口位置、大小和控件状态

第一步:添加配置文件类

首先,我们需要创建一个用于保存窗口位置、大小和控件状态的配置文件类。可以和主窗口类放在同一个文件中,或者单独创建一个文件。

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace WinFormApp
{
    public class AppConfig
    {
        private const string ConfigFile = "config.txt";  // 配置文件的文件名

        public int WindowLeft { get; set; }
        public int WindowTop { get; set; }
        public int WindowWidth { get; set; }
        public int WindowHeight { get; set; }
        public Dictionary<string, bool> ControlStates { get; set; }

        public void Save()
        {
            string configFile = GetConfigFilePath();
            string configData = ToConfigData();
            File.WriteAllText(configFile, configData, Encoding.UTF8);
        }

        public void Load()
        {
            string configFile = GetConfigFilePath();
            if (File.Exists(configFile))
            {
                string configData = File.ReadAllText(configFile, Encoding.UTF8);
                FromConfigData(configData);
            }
        }

        private string GetConfigFilePath()
        {
            return Path.Combine(Application.StartupPath, ConfigFile);
        }

        private string ToConfigData()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(WindowLeft.ToString());
            sb.AppendLine(WindowTop.ToString());
            sb.AppendLine(WindowWidth.ToString());
            sb.AppendLine(WindowHeight.ToString());
            sb.AppendLine(ControlStates.Count.ToString());
            foreach (var kvp in ControlStates)
            {
                sb.AppendLine($"{kvp.Key},{kvp.Value}");
            }
            return sb.ToString();
        }

        private void FromConfigData(string configData)
        {
            string[] lines = configData.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            WindowLeft = int.Parse(lines[0]);
            WindowTop = int.Parse(lines[1]);
            WindowWidth = int.Parse(lines[2]);
            WindowHeight = int.Parse(lines[3]);
            int count = int.Parse(lines[4]);
            ControlStates = new Dictionary<string, bool>(count);
            for (int i = 0; i < count; i++)
            {
                string[] parts = lines[5 + i].Split(',');
                string controlName = parts[0];
                bool controlState = bool.Parse(parts[1]);
                ControlStates.Add(controlName, controlState);
            }
        }
    }
}

这个类中有一些属性用于保存窗口位置、大小和控件状态的信息,并提供了保存和加载配置的方法。

第二步:保存配置

在程序退出时,我们需要保存窗口位置、大小和控件状态的信息到配置文件中。可以在窗口的FormClosing事件中执行保存操作。

private AppConfig _appConfig;

public Form1()
{
    InitializeComponent();
    _appConfig = new AppConfig();
    _appConfig.Load();

    // 根据配置文件恢复窗口位置和大小
    this.Left = _appConfig.WindowLeft;
    this.Top = _appConfig.WindowTop;
    this.Width = _appConfig.WindowWidth;
    this.Height = _appConfig.WindowHeight;

    // 遍历控件字典,根据配置文件设置控件的状态
    foreach (var kvp in _appConfig.ControlStates)
    {
        Control control = this.Controls.Find(kvp.Key, true).FirstOrDefault();
        if (control != null)
        {
            control.Visible = kvp.Value;
        }
    }
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    // 保存窗口位置和大小
    _appConfig.WindowLeft = this.Left;
    _appConfig.WindowTop = this.Top;
    _appConfig.WindowWidth = this.Width;
    _appConfig.WindowHeight = this.Height;

    // 遍历控件字典,保存控件的状态
    _appConfig.ControlStates = new Dictionary<string, bool>();
    foreach (Control control in this.Controls)
    {
        _appConfig.ControlStates.Add(control.Name, control.Visible);
    }

    // 保存配置
    _appConfig.Save();
}

在这段代码中,我们通过实例化AppConfig类来加载和保存配置。在窗口的FormClosing事件中,我们保存当前窗口位置和大小,并遍历窗口中的控件,保存它们的可见性状态。

第三步:恢复配置

在程序启动时,我们需要从配置文件中加载保存的配置,并恢复到上一次的运行状态。可以在窗口的构造函数中加载配置,并根据配置恢复窗口位置、大小和控件状态。

private AppConfig _appConfig;

public Form1()
{
    InitializeComponent();
    _appConfig = new AppConfig();
    _appConfig.Load();

    // 根据配置文件恢复窗口位置和大小
    this.Left = _appConfig.WindowLeft;
    this.Top = _appConfig.WindowTop;
    this.Width = _appConfig.WindowWidth;
    this.Height = _appConfig.WindowHeight;

    // 遍历控件字典,根据配置文件设置控件的状态
    foreach (var kvp in _appConfig.ControlStates)
    {
        Control control = this.Controls.Find(kvp.Key, true).FirstOrDefault();
        if (control != null)
        {
            control.Visible = kvp.Value;
        }
    }
}

在这段代码中,我们实例化了AppConfig类,并调用Load方法加载配置。然后,根据配置文件中保存的窗口位置和大小恢复窗口的状态,再根据控件的状态字典恢复控件的可见性。

结束语

通过以上三步,我们可以实现C# WinForm程序的锁存功能,即在程序退出后,下一次打开程序时能够恢复到上一次的运行状态。在配置文件中保存窗口位置、大小和控件状态的信息,然后在窗口的FormClosing事件中保存配置,再在窗口的构造函数中加载配置并恢复状态。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^