gpt4 book ai didi

c# - 关闭屏幕更新并重新打开它(如果开始时是打开的)?

转载 作者:太空狗 更新时间:2023-10-29 22:02:03 24 4
gpt4 key购买 nike

在运行使用 Excel 的程序时,我通常会在程序开始时关闭某些应用程序设置,然后在程序结束时再次打开它们。

关闭和打开应用程序设置的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XLTimeTracker
{
class API
{
public static void TurnAppSettingsOff()
{
AddinModule.CurrentInstance.ExcelApp.EnableEvents = false;
AddinModule.CurrentInstance.ExcelApp.ScreenUpdating = false;
}

public static void TurnAppSettingsOn()
{
if (AddinModule.CurrentInstance.ExcelApp == null) return;

AddinModule.CurrentInstance.ExcelApp.EnableEvents = true;
AddinModule.CurrentInstance.ExcelApp.ScreenUpdating = true;
}
}
}

我按以下方式调用此过程:

API.TurnAppSettingsOff();
// my code
API.TurnAppSettingsOn();

这很有效。

但是假设我只想打开在运行 API.TurnAppSettingsOff() 之前打开的应用程序设置。什么是对此进行编码的好方法?

一些想法:

  • 我想我需要以某种方式保存应用程序设置的先前状态。例如写:

Boolean screenUpdating = AddinModule.CurrentInstance.ExcelApp.ScreenUpdating;

  • 我希望最终结果是正确的,即使关闭和打开应用程序设置的函数也调用了另一个关闭和打开应用程序设置的函数。

  • 我不知道最好使用一个命令(例如 API.TurnAppSettingsOff())来设置所有设置,还是对用户 API.TurnScreenUpdatingOff 更明智()API.TurnEventsOff()

最佳答案

我会创建一个内部管理器类来处理类似于以下内容的整个事情:

public sealed class ApplicationSettingsManager
{
readonly object app;
readonly Dictionary<string, object> appSettings;

public ApplicationSettingsManager(object app)
{
this.app = app;
appSettings = new Dictionary<string, object>();
}

public object Application { get { return app; } }

public void SaveSetting(string settingName)
{
var propInfo = app.GetType().GetProperty(settingName);
if (propInfo == null)
throw new ArgumentException("Specified name is not a valid storable setting.", "setting");

var value = propInfo.GetValue(app);

if (appSettings.ContainsKey(settingName))
{
appSettings[settingName] = value;
}
else
{
appSettings.Add(settingName, value);
}
}

public void SaveAllSettings()
{
var properties = app.GetType().GetProperties().Where(p => p.CanWrite &&
p.CanRead &&
p.SetMethod.IsPublic &&
p.GetMethod.IsPublic);

foreach (var p in properties)
{
var value = p.GetValue(app);

if (appSettings.ContainsKey(p.Name))
{
appSettings[p.Name] = value;
}
else
{
appSettings.Add(p.Name, value);
}
}
}

public void RestoreSetting(string settingName)
{
if (!appSettings.ContainsKey(settingName))
throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName");

var propInfo = app.GetType().GetProperty(settingName);
propInfo.SetValue(app, appSettings[settingName]);
}

public void RestoreAllSettingas()
{
foreach (var p in appSettings)
{
RestoreSetting(p.Key);
}
}
}

这应该可以解决问题。您将按如下方式使用它;

var excelSettingsManager = new ApplicationSettingsManager(AddinModule.CurrentInstance.ExcelApp);

//store all settings you are going to tamper with...
excelSettingsManager.SaveSetting("EnableEvents");
excelSettingsManager.SaveSetting("ScreenUpdating");

//change excel setting and do your thing...
//...
//when done, restore settings

excelSettingsManager.RestoreAllSettings();

大功告成!

关于c# - 关闭屏幕更新并重新打开它(如果开始时是打开的)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31180523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com