184 lines
7.3 KiB
C#
184 lines
7.3 KiB
C#
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 Button btnColor; // 颜色选择按钮
|
||
private Panel pnlColorPreview; // 颜色预览面板
|
||
private int _selectedColor = 0; // 存储选中的颜色值 (Word WdColor)
|
||
|
||
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);
|
||
|
||
// 颜色选择
|
||
Label lblColor = new Label { Text = "颜色:", Location = new Point(180, 67), AutoSize = true };
|
||
btnColor = new Button { Text = "选择...", Location = new Point(220, 63), Width = 60, Height = 25 };
|
||
pnlColorPreview = new Panel { Location = new Point(290, 65), Width = 20, Height = 20, BorderStyle = BorderStyle.FixedSingle, BackColor = Color.Black };
|
||
|
||
btnColor.Click += BtnColor_Click;
|
||
|
||
this.Controls.Add(lblColor);
|
||
this.Controls.Add(btnColor);
|
||
this.Controls.Add(pnlColorPreview);
|
||
|
||
// 对齐方式 (可选)
|
||
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);
|
||
}
|
||
}
|
||
|
||
private void BtnColor_Click(object sender, EventArgs e)
|
||
{
|
||
using (ColorDialog colorDialog = new ColorDialog())
|
||
{
|
||
// Set initial color based on current selection
|
||
colorDialog.Color = pnlColorPreview.BackColor;
|
||
|
||
if (colorDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
pnlColorPreview.BackColor = colorDialog.Color;
|
||
|
||
// Convert .NET Color to Word WdColor (int)
|
||
// Word stores colors as BGR (0xBBGGRR) in lower 24 bits
|
||
// But .NET Color.ToArgb() returns ARGB (0xAARRGGBB)
|
||
// WdColor is weird.
|
||
// wdColorBlack = 0
|
||
// wdColorRed = 255 (0x0000FF)
|
||
// It seems to be standard RGB or BGR integer value.
|
||
// Let's use ColorTranslator or manual conversion.
|
||
// Word Interop usually expects BGR int for some properties, or standard OLE color.
|
||
// Actually, WdColor enum has values like wdColorRed = 0xFF.
|
||
// The safest way is to cast the RGB value.
|
||
// However, ColorTranslator.ToOle(c) returns BGR, which is what Office usually expects.
|
||
|
||
_selectedColor = ColorTranslator.ToOle(colorDialog.Color);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <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;
|
||
|
||
_selectedColor = style.Color;
|
||
// Convert OLE Color back to .NET Color for preview
|
||
try
|
||
{
|
||
pnlColorPreview.BackColor = ColorTranslator.FromOle(_selectedColor);
|
||
}
|
||
catch
|
||
{
|
||
pnlColorPreview.BackColor = Color.Black;
|
||
}
|
||
|
||
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;
|
||
style.Color = _selectedColor;
|
||
|
||
if (_showAlign && cmbAlign != null)
|
||
{
|
||
style.Alignment = cmbAlign.SelectedIndex;
|
||
}
|
||
return style;
|
||
}
|
||
}
|
||
}
|