Files
aiData/WordAddIn/WordAddIn/OutlineSettingsForm.cs
HuangHai b514bd2812 'commit'
2026-01-25 09:21:48 +08:00

178 lines
6.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Drawing;
using System.Windows.Forms;
using WordAddIn.Models;
using WordAddIn.Properties;
using Newtonsoft.Json;
namespace WordAddIn
{
/// <summary>
/// 大纲样式配置窗体。
/// 功能描述:
/// 1. 允许用户为大纲的不同层级(主标题、一级目录、二级目录、三级目录)配置样式。
/// 2. 使用 TabControl 组织不同层级的样式编辑器(StyleEditorControl)。
/// 3. 支持保存配置到本地设置(Settings.Default.OutlineStyles)和加载已有配置。
/// </summary>
public class OutlineSettingsForm : Form
{
private TabControl tabControl;
private Button btnSave;
private Button btnCancel;
private StyleEditorControl mainTitleEditor;
private StyleEditorControl h1Editor;
private StyleEditorControl h2Editor;
private StyleEditorControl h3Editor;
public OutlineSettingsForm()
{
InitializeComponent();
InitializeCustomControls();
LoadSettings();
}
/// <summary>
/// 初始化基本窗体组件。
/// </summary>
private void InitializeComponent()
{
this.tabControl = new System.Windows.Forms.TabControl();
this.btnSave = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// tabControl
//
this.tabControl.Location = new System.Drawing.Point(12, 12);
this.tabControl.Name = "tabControl";
this.tabControl.SelectedIndex = 0;
this.tabControl.Size = new System.Drawing.Size(410, 280);
this.tabControl.TabIndex = 0;
//
// btnSave
//
this.btnSave.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnSave.Location = new System.Drawing.Point(210, 310);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(100, 35);
this.btnSave.TabIndex = 1;
this.btnSave.Text = "保存配置";
this.btnSave.UseVisualStyleBackColor = true;
this.btnSave.Click += new System.EventHandler(this.BtnSave_Click);
//
// btnCancel
//
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Location = new System.Drawing.Point(320, 310);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(100, 35);
this.btnCancel.TabIndex = 2;
this.btnCancel.Text = "取消";
this.btnCancel.UseVisualStyleBackColor = true;
//
// OutlineSettingsForm
//
this.AcceptButton = this.btnSave;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.btnCancel;
this.ClientSize = new System.Drawing.Size(450, 360);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnSave);
this.Controls.Add(this.tabControl);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "OutlineSettingsForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "大纲样式配置";
this.ResumeLayout(false);
}
/// <summary>
/// 初始化自定义样式编辑器控件,并添加到 Tab 页中。
/// </summary>
private void InitializeCustomControls()
{
mainTitleEditor = new StyleEditorControl("主标题", true);
h1Editor = new StyleEditorControl("一级目录", false);
h2Editor = new StyleEditorControl("二级目录", false);
h3Editor = new StyleEditorControl("三级目录", false);
AddTab("主标题", mainTitleEditor);
AddTab("一级目录", h1Editor);
AddTab("二级目录", h2Editor);
AddTab("三级目录", h3Editor);
}
/// <summary>
/// 辅助方法:添加一个新的 Tab 页。
/// </summary>
private void AddTab(string title, Control content)
{
TabPage page = new TabPage(title);
content.Dock = DockStyle.Fill;
page.Controls.Add(content);
tabControl.TabPages.Add(page);
}
/// <summary>
/// 从 Settings 中加载保存的样式配置。
/// </summary>
private void LoadSettings()
{
try
{
string json = Settings.Default.OutlineStyles;
OutlineStyleConfig config = null;
if (!string.IsNullOrEmpty(json))
{
config = JsonConvert.DeserializeObject<OutlineStyleConfig>(json);
}
if (config == null)
{
config = new OutlineStyleConfig(); // Use defaults
}
mainTitleEditor.SetStyle(config.MainTitle);
h1Editor.SetStyle(config.Heading1);
h2Editor.SetStyle(config.Heading2);
h3Editor.SetStyle(config.Heading3);
}
catch (Exception ex)
{
MessageBox.Show("加载配置失败: " + ex.Message);
}
}
/// <summary>
/// 保存按钮点击事件。
/// 将当前配置序列化为 JSON 并保存到 Settings。
/// </summary>
private void BtnSave_Click(object sender, EventArgs e)
{
try
{
OutlineStyleConfig config = new OutlineStyleConfig();
config.MainTitle = mainTitleEditor.GetStyle();
config.Heading1 = h1Editor.GetStyle();
config.Heading2 = h2Editor.GetStyle();
config.Heading3 = h3Editor.GetStyle();
string json = JsonConvert.SerializeObject(config);
Settings.Default.OutlineStyles = json;
Settings.Default.Save();
}
catch (Exception ex)
{
MessageBox.Show("保存配置失败: " + ex.Message);
this.DialogResult = DialogResult.None;
}
}
}
}