Files
aiData/WordAddIn/WordAddIn/ImageOptionsForm.cs

222 lines
9.7 KiB
C#
Raw Normal View History

2026-01-24 15:44:20 +08:00
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using WordAddIn.Properties;
namespace WordAddIn
{
2026-01-24 20:54:49 +08:00
/// <summary>
/// 生成配图设置窗体。
/// 功能描述:
2026-01-26 08:00:40 +08:00
/// 1. 允许用户选择图片风格(包括多种架构图)。
/// 2. 自动根据选择的风格生成提示词模板。
/// 3. 允许用户直接编辑最终发送给 AI 的提示词。
2026-01-24 20:54:49 +08:00
/// </summary>
2026-01-24 15:44:20 +08:00
public class ImageOptionsForm : Form
{
2026-01-24 20:54:49 +08:00
/// <summary>
2026-01-26 08:00:40 +08:00
/// 最终生成的提示词(用户编辑后的版本)。
2026-01-24 20:54:49 +08:00
/// </summary>
2026-01-26 08:00:40 +08:00
public string FinalPrompt { get; private set; }
/// <summary>
/// 是否需要 AI 进一步优化提示词(翻译/润色)。
/// 对于架构图通常为 false(直接使用模板),对于艺术画通常为 true。
/// </summary>
public bool ShouldOptimize { get; private set; }
2026-01-24 20:54:49 +08:00
/// <summary>
2026-01-26 08:00:40 +08:00
/// 源文本(从 Word 选区或剪贴板获取)。
2026-01-24 20:54:49 +08:00
/// </summary>
2026-01-26 08:00:40 +08:00
public string SourceText { get; set; }
/// <summary>
/// 用户选择的风格名称(用于后续可能的引用,虽然后端逻辑已主要依赖 FinalPrompt)
/// </summary>
public string SelectedStyle { get; private set; }
2026-01-24 15:44:20 +08:00
private ComboBox cmbStyles;
2026-01-26 08:00:40 +08:00
private TextBox txtFinalPrompt;
private CheckBox chkOptimize;
2026-01-24 15:44:20 +08:00
private Button btnOk;
private Button btnCancel;
2026-01-24 18:05:24 +08:00
private Label lblStyle;
private Label lblPrompt;
2026-01-24 15:44:20 +08:00
2026-01-26 08:00:40 +08:00
private readonly Dictionary<string, string> _styleTemplates = new Dictionary<string, string>();
2026-01-24 15:44:20 +08:00
public ImageOptionsForm()
{
2026-01-26 08:00:40 +08:00
InitializeTemplates();
2026-01-24 15:44:20 +08:00
InitializeComponent();
}
2026-01-26 08:00:40 +08:00
private void InitializeTemplates()
{
// 基础模板:用于架构图系列
// 注意:{0} 是图表类型,{1} 是内容占位符
string archTemplate =
"帮我绘制{0},必须文字合使用中文,中文一定要清晰可见,不能字体模糊或者创造不存在的汉字!\n" +
"同时要美观,大方。\n\n" +
"内容如下:\n{1}\n\n" +
"根据如下内容创建一个整体上的{0},美观,细节处理仔细,特别需要注意的是文字的准确性,中文不要出现错字或不可识别的乱码!!!" +
"同时,字体除标题使用黑体外,其它一概使用宋体,这样不容易出现字体不完整的情况!!!!";
// 注册所有支持的风格及其处理逻辑
// 架构图类
_styleTemplates.Add("逻辑架构图 (Logical Architecture)", string.Format(archTemplate, "逻辑架构图", "{0}"));
_styleTemplates.Add("业务架构图 (Business Architecture)", string.Format(archTemplate, "业务架构图", "{0}"));
_styleTemplates.Add("应用架构图 (Application Architecture)", string.Format(archTemplate, "应用架构图", "{0}"));
_styleTemplates.Add("数据架构图 (Data Architecture)", string.Format(archTemplate, "数据架构图", "{0}"));
_styleTemplates.Add("技术架构图 (Technical Architecture)", string.Format(archTemplate, "技术架构图", "{0}"));
_styleTemplates.Add("部署架构图 (Deployment Architecture)", string.Format(archTemplate, "部署架构图", "{0}"));
_styleTemplates.Add("思维导图 (Mind Map)", string.Format(archTemplate, "思维导图", "{0}"));
_styleTemplates.Add("流程图 (Flow Chart)", string.Format(archTemplate, "流程图", "{0}"));
_styleTemplates.Add("时序图 (Sequence Diagram)", string.Format(archTemplate, "时序交互图", "{0}"));
_styleTemplates.Add("实施保障图 (Implementation & Safeguard Diagram)", string.Format(archTemplate, "实施保障图", "{0}"));
_styleTemplates.Add("逐步进化路线图 (Evolutionary Roadmap)", string.Format(archTemplate, "逐步进化路线图", "{0}"));
// 艺术风格类 (Value 为 null 表示使用默认 AI 优化逻辑)
_styleTemplates.Add("写实摄影 (Photorealistic)", null);
_styleTemplates.Add("卡通插画 (Cartoon)", null);
_styleTemplates.Add("手绘草图 (Sketch)", null);
_styleTemplates.Add("3D 渲染 (3D Render)", null);
_styleTemplates.Add("水彩画 (Watercolor)", null);
_styleTemplates.Add("赛博朋克 (Cyberpunk)", null);
}
2026-01-24 15:44:20 +08:00
private void InitializeComponent()
{
2026-01-26 08:00:40 +08:00
this.Text = "生成配图选项 - 支持编辑提示词";
this.Size = new Size(500, 520);
this.StartPosition = FormStartPosition.CenterParent;
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.lblStyle = new Label();
this.cmbStyles = new ComboBox();
this.lblPrompt = new Label();
this.txtFinalPrompt = new TextBox();
this.chkOptimize = new CheckBox();
this.btnOk = new Button();
this.btnCancel = new Button();
2026-01-24 18:05:24 +08:00
this.SuspendLayout();
2026-01-26 08:00:40 +08:00
2026-01-24 18:05:24 +08:00
// lblStyle
this.lblStyle.AutoSize = true;
2026-01-26 08:00:40 +08:00
this.lblStyle.Location = new Point(20, 20);
2026-01-24 18:05:24 +08:00
this.lblStyle.Text = "图片风格:";
2026-01-26 08:00:40 +08:00
2026-01-24 18:05:24 +08:00
// cmbStyles
2026-01-26 08:00:40 +08:00
this.cmbStyles.DropDownStyle = ComboBoxStyle.DropDownList;
this.cmbStyles.Location = new Point(20, 45);
this.cmbStyles.Size = new Size(440, 25);
foreach (var key in _styleTemplates.Keys)
{
this.cmbStyles.Items.Add(key);
}
this.cmbStyles.SelectedIndexChanged += CmbStyles_SelectedIndexChanged;
2026-01-24 18:05:24 +08:00
// lblPrompt
this.lblPrompt.AutoSize = true;
2026-01-26 08:00:40 +08:00
this.lblPrompt.Location = new Point(20, 85);
this.lblPrompt.Text = "提示词预览 (您可以直接修改下面的生成指令):";
// txtFinalPrompt
this.txtFinalPrompt.Location = new Point(20, 110);
this.txtFinalPrompt.Multiline = true;
this.txtFinalPrompt.ScrollBars = ScrollBars.Vertical;
this.txtFinalPrompt.Size = new Size(440, 280);
// chkOptimize
this.chkOptimize.AutoSize = true;
this.chkOptimize.Location = new Point(20, 400);
this.chkOptimize.Size = new Size(400, 20);
this.chkOptimize.Text = "使用 AI 优化提示词 (自动翻译成英文并丰富细节)";
this.chkOptimize.Checked = true;
2026-01-24 18:05:24 +08:00
// btnOk
2026-01-26 08:00:40 +08:00
this.btnOk.Text = "开始生成";
this.btnOk.DialogResult = DialogResult.OK;
this.btnOk.Location = new Point(280, 430);
this.btnOk.Size = new Size(90, 30);
this.btnOk.Click += BtnOk_Click;
2026-01-24 18:05:24 +08:00
// btnCancel
this.btnCancel.Text = "取消";
2026-01-26 08:00:40 +08:00
this.btnCancel.DialogResult = DialogResult.Cancel;
this.btnCancel.Location = new Point(380, 430);
this.btnCancel.Size = new Size(80, 30);
2026-01-24 18:05:24 +08:00
this.Controls.Add(this.lblStyle);
2026-01-26 08:00:40 +08:00
this.Controls.Add(this.cmbStyles);
this.Controls.Add(this.lblPrompt);
this.Controls.Add(this.txtFinalPrompt);
this.Controls.Add(this.chkOptimize);
this.Controls.Add(this.btnOk);
this.Controls.Add(this.btnCancel);
2026-01-24 18:05:24 +08:00
this.ResumeLayout(false);
this.PerformLayout();
2026-01-26 08:00:40 +08:00
}
2026-01-24 15:44:20 +08:00
2026-01-26 08:00:40 +08:00
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// 默认选中第一个风格(通常是逻辑架构图,如果 keys 顺序不对可能需要指定)
if (this.cmbStyles.Items.Count > 0)
{
// 尝试找到上次选中的或默认逻辑架构图
int index = this.cmbStyles.FindString("逻辑架构图");
this.cmbStyles.SelectedIndex = index >= 0 ? index : 0;
}
else
{
// Fallback update if no items or default
UpdatePrompt();
}
2026-01-24 15:44:20 +08:00
}
2026-01-26 08:00:40 +08:00
private void CmbStyles_SelectedIndexChanged(object sender, EventArgs e)
{
UpdatePrompt();
}
private void UpdatePrompt()
2026-01-24 15:44:20 +08:00
{
2026-01-26 08:00:40 +08:00
if (cmbStyles.SelectedItem == null) return;
string styleKey = cmbStyles.SelectedItem.ToString();
string template = _styleTemplates.ContainsKey(styleKey) ? _styleTemplates[styleKey] : null;
// 只有当文本框为空,或者文本框内容是上一个模板生成的内容时,才自动覆盖
// 但为了简单起见,这里策略是:只要切换风格,就重置提示词。用户如果修改了,切换风格会丢失修改。
// 这是一个权衡。为了体验更好,可以加个判断,但现在先强制刷新。
2026-01-24 15:44:20 +08:00
2026-01-26 08:00:40 +08:00
if (template != null)
2026-01-24 15:44:20 +08:00
{
2026-01-26 08:00:40 +08:00
// 是架构图类:使用模板
this.txtFinalPrompt.Text = template.Replace("{0}", this.SourceText ?? "");
// 用户要求默认选中 AI 优化
this.chkOptimize.Checked = true;
2026-01-24 15:44:20 +08:00
}
else
{
2026-01-26 08:00:40 +08:00
// 是艺术风格类:直接显示原文本,开启 AI 优化
this.txtFinalPrompt.Text = this.SourceText ?? "";
this.chkOptimize.Checked = true;
2026-01-24 15:44:20 +08:00
}
}
private void BtnOk_Click(object sender, EventArgs e)
{
2026-01-26 08:00:40 +08:00
this.FinalPrompt = this.txtFinalPrompt.Text;
this.ShouldOptimize = this.chkOptimize.Checked;
this.SelectedStyle = this.cmbStyles.SelectedItem?.ToString();
2026-01-24 15:44:20 +08:00
}
}
}