121 lines
4.9 KiB
C#
121 lines
4.9 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Windows.Forms;
|
|
using WordAddIn.Properties;
|
|
|
|
namespace WordAddIn
|
|
{
|
|
public class FormatOptionsForm : Form
|
|
{
|
|
public string SelectedFontName { get; private set; }
|
|
public float SelectedFontSize { get; private set; }
|
|
public float SelectedLineSpacing { get; private set; }
|
|
|
|
private ComboBox cmbFontName;
|
|
private ComboBox cmbFontSize;
|
|
private ComboBox cmbLineSpacing;
|
|
private Button btnOk;
|
|
private Button btnCancel;
|
|
|
|
public FormatOptionsForm()
|
|
{
|
|
InitializeComponent();
|
|
LoadSettings();
|
|
}
|
|
|
|
private void InitializeComponent()
|
|
{
|
|
this.Text = "格式化设置";
|
|
this.Size = new Size(350, 250);
|
|
this.StartPosition = FormStartPosition.CenterScreen;
|
|
this.FormBorderStyle = FormBorderStyle.FixedDialog;
|
|
this.MaximizeBox = false;
|
|
this.MinimizeBox = false;
|
|
|
|
int labelX = 30;
|
|
int inputX = 120;
|
|
int startY = 30;
|
|
int gapY = 40;
|
|
|
|
// 1. 字体
|
|
var lblFont = new Label { Text = "字体:", Location = new Point(labelX, startY + 5), AutoSize = true };
|
|
cmbFontName = new ComboBox { Location = new Point(inputX, startY), Width = 150, DropDownStyle = ComboBoxStyle.DropDownList };
|
|
// 简单添加几个常用中文字体
|
|
cmbFontName.Items.AddRange(new object[] { "微软雅黑", "宋体", "黑体", "楷体", "Arial", "Times New Roman" });
|
|
|
|
// 2. 字号
|
|
var lblSize = new Label { Text = "字号:", Location = new Point(labelX, startY + gapY + 5), AutoSize = true };
|
|
cmbFontSize = new ComboBox { Location = new Point(inputX, startY + gapY), Width = 150, DropDownStyle = ComboBoxStyle.DropDownList };
|
|
cmbFontSize.Items.AddRange(new object[] { "9", "10", "10.5", "11", "12", "14", "16", "18", "22", "24" });
|
|
|
|
// 3. 行间距
|
|
var lblSpacing = new Label { Text = "行间距(倍):", Location = new Point(labelX, startY + gapY * 2 + 5), AutoSize = true };
|
|
cmbLineSpacing = new ComboBox { Location = new Point(inputX, startY + gapY * 2), Width = 150, DropDownStyle = ComboBoxStyle.DropDownList };
|
|
cmbLineSpacing.Items.AddRange(new object[] { "1.0", "1.25", "1.5", "2.0", "2.5", "3.0" });
|
|
|
|
// Buttons
|
|
btnOk = new Button { Text = "确定", Location = new Point(130, 160), Width = 80, Height = 30, DialogResult = DialogResult.OK };
|
|
btnOk.Click += BtnOk_Click;
|
|
|
|
btnCancel = new Button { Text = "取消", Location = new Point(220, 160), Width = 80, Height = 30, DialogResult = DialogResult.Cancel };
|
|
|
|
this.Controls.Add(lblFont);
|
|
this.Controls.Add(cmbFontName);
|
|
this.Controls.Add(lblSize);
|
|
this.Controls.Add(cmbFontSize);
|
|
this.Controls.Add(lblSpacing);
|
|
this.Controls.Add(cmbLineSpacing);
|
|
this.Controls.Add(btnOk);
|
|
this.Controls.Add(btnCancel);
|
|
|
|
this.AcceptButton = btnOk;
|
|
this.CancelButton = btnCancel;
|
|
}
|
|
|
|
private void LoadSettings()
|
|
{
|
|
// 读取保存的设置,如果没有则使用默认值
|
|
string fontName = Settings.Default.FormatFontName;
|
|
float fontSize = Settings.Default.FormatFontSize;
|
|
float lineSpacing = Settings.Default.FormatLineSpacing;
|
|
|
|
if (cmbFontName.Items.Contains(fontName))
|
|
cmbFontName.SelectedItem = fontName;
|
|
else
|
|
cmbFontName.SelectedIndex = 0;
|
|
|
|
string sizeStr = fontSize.ToString();
|
|
if (cmbFontSize.Items.Contains(sizeStr))
|
|
cmbFontSize.SelectedItem = sizeStr;
|
|
else
|
|
cmbFontSize.SelectedItem = "12";
|
|
|
|
string spacingStr = lineSpacing.ToString();
|
|
if (cmbLineSpacing.Items.Contains(spacingStr))
|
|
cmbLineSpacing.SelectedItem = spacingStr;
|
|
else
|
|
cmbLineSpacing.SelectedItem = "1.5";
|
|
}
|
|
|
|
private void BtnOk_Click(object sender, EventArgs e)
|
|
{
|
|
// 保存设置
|
|
if (cmbFontName.SelectedItem != null)
|
|
Settings.Default.FormatFontName = cmbFontName.SelectedItem.ToString();
|
|
|
|
if (cmbFontSize.SelectedItem != null && float.TryParse(cmbFontSize.SelectedItem.ToString(), out float size))
|
|
Settings.Default.FormatFontSize = size;
|
|
|
|
if (cmbLineSpacing.SelectedItem != null && float.TryParse(cmbLineSpacing.SelectedItem.ToString(), out float spacing))
|
|
Settings.Default.FormatLineSpacing = spacing;
|
|
|
|
Settings.Default.Save();
|
|
|
|
// 更新属性供外部调用
|
|
SelectedFontName = Settings.Default.FormatFontName;
|
|
SelectedFontSize = Settings.Default.FormatFontSize;
|
|
SelectedLineSpacing = Settings.Default.FormatLineSpacing;
|
|
}
|
|
}
|
|
}
|