'commit'
This commit is contained in:
126
WordAddIn/WordAddIn/StyleEditorControl.cs
Normal file
126
WordAddIn/WordAddIn/StyleEditorControl.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
using WordAddIn.Models;
|
||||
|
||||
namespace WordAddIn
|
||||
{
|
||||
/// <summary>
|
||||
/// 样式编辑器控件。
|
||||
/// 功能描述:
|
||||
/// 1. 提供可复用的用户界面,用于配置文本样式(字体、字号、加粗、对齐方式)。
|
||||
/// 2. 封装了 WinForms 控件(ComboBox, NumericUpDown, CheckBox),简化样式设置。
|
||||
/// 3. 支持数据绑定:SetStyle() 设置界面状态,GetStyle() 获取用户配置。
|
||||
/// </summary>
|
||||
public class StyleEditorControl : UserControl
|
||||
{
|
||||
private ComboBox cmbFont;
|
||||
private NumericUpDown numSize;
|
||||
private CheckBox chkBold;
|
||||
private ComboBox cmbAlign;
|
||||
private bool _showAlign;
|
||||
|
||||
/// <summary>
|
||||
/// 默认构造函数 (设计器支持)。
|
||||
/// </summary>
|
||||
public StyleEditorControl()
|
||||
{
|
||||
// Default constructor for designer support
|
||||
_showAlign = false;
|
||||
Initialize("样式");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 带参数的构造函数。
|
||||
/// </summary>
|
||||
/// <param name="labelText">分组标题文本 (如 "一级目录")</param>
|
||||
/// <param name="showAlign">是否显示对齐方式选项</param>
|
||||
public StyleEditorControl(string labelText, bool showAlign)
|
||||
{
|
||||
_showAlign = showAlign;
|
||||
Initialize(labelText);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化控件界面。
|
||||
/// </summary>
|
||||
/// <param name="labelText">分组标题</param>
|
||||
private void Initialize(string labelText)
|
||||
{
|
||||
this.Padding = new Padding(20);
|
||||
this.Size = new Size(380, 200); // Set a default size
|
||||
|
||||
// 字体选择
|
||||
Label lblFont = new Label { Text = "字体:", Location = new Point(20, 30), AutoSize = true };
|
||||
cmbFont = new ComboBox { Location = new Point(80, 27), Width = 150, DropDownStyle = ComboBoxStyle.DropDownList };
|
||||
// Add common Chinese fonts
|
||||
cmbFont.Items.AddRange(new object[] { "宋体", "黑体", "微软雅黑", "楷体", "仿宋", "Arial", "Times New Roman" });
|
||||
cmbFont.SelectedIndex = 0;
|
||||
|
||||
// 字号选择
|
||||
Label lblSize = new Label { Text = "字号:", Location = new Point(20, 70), AutoSize = true };
|
||||
numSize = new NumericUpDown { Location = new Point(80, 67), Width = 80, Minimum = 5, Maximum = 72, DecimalPlaces = 1 };
|
||||
|
||||
// 加粗选项
|
||||
chkBold = new CheckBox { Text = "加粗", Location = new Point(80, 110), AutoSize = true };
|
||||
|
||||
this.Controls.Add(lblFont);
|
||||
this.Controls.Add(cmbFont);
|
||||
this.Controls.Add(lblSize);
|
||||
this.Controls.Add(numSize);
|
||||
this.Controls.Add(chkBold);
|
||||
|
||||
// 对齐方式 (可选)
|
||||
if (_showAlign)
|
||||
{
|
||||
Label lblAlign = new Label { Text = "对齐:", Location = new Point(20, 150), AutoSize = true };
|
||||
cmbAlign = new ComboBox { Location = new Point(80, 147), Width = 100, DropDownStyle = ComboBoxStyle.DropDownList };
|
||||
cmbAlign.Items.AddRange(new object[] { "左对齐", "居中", "右对齐" });
|
||||
cmbAlign.SelectedIndex = 0;
|
||||
|
||||
this.Controls.Add(lblAlign);
|
||||
this.Controls.Add(cmbAlign);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 TextStyle 对象应用到界面控件。
|
||||
/// </summary>
|
||||
/// <param name="style">要显示的样式对象</param>
|
||||
public void SetStyle(TextStyle style)
|
||||
{
|
||||
if (style == null) return;
|
||||
|
||||
if (cmbFont.Items.Contains(style.FontName))
|
||||
cmbFont.SelectedItem = style.FontName;
|
||||
else
|
||||
cmbFont.Text = style.FontName; // Fallback
|
||||
|
||||
numSize.Value = (decimal)style.FontSize;
|
||||
chkBold.Checked = style.IsBold;
|
||||
|
||||
if (_showAlign && cmbAlign != null)
|
||||
{
|
||||
if (style.Alignment >= 0 && style.Alignment < cmbAlign.Items.Count)
|
||||
cmbAlign.SelectedIndex = style.Alignment;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从界面控件获取当前的 TextStyle 配置。
|
||||
/// </summary>
|
||||
/// <returns>包含用户配置的样式对象</returns>
|
||||
public TextStyle GetStyle()
|
||||
{
|
||||
TextStyle style = new TextStyle();
|
||||
style.FontName = cmbFont.Text;
|
||||
style.FontSize = (float)numSize.Value;
|
||||
style.IsBold = chkBold.Checked;
|
||||
if (_showAlign && cmbAlign != null)
|
||||
{
|
||||
style.Alignment = cmbAlign.SelectedIndex;
|
||||
}
|
||||
return style;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user