Files
aiData/WordAddIn/WordAddIn/ImageOptionsForm.cs

320 lines
13 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-29 08:22:08 +08:00
private RadioButton rbClipboard;
private RadioButton rbInput;
private TextBox txtInput;
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;
2026-01-29 08:22:08 +08:00
private Label lblSource;
2026-01-24 18:05:24 +08:00
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-28 21:46:06 +08:00
foreach (var key in _styleTemplates.Keys)
{
this.cmbStyles.Items.Add(key);
}
2026-01-24 15:44:20 +08:00
}
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-29 08:22:08 +08:00
this.Text = "生成配图设置";
this.Size = new Size(500, 650);
this.MinimumSize = new Size(400, 500);
2026-01-26 08:00:40 +08:00
this.StartPosition = FormStartPosition.CenterParent;
2026-01-29 08:22:08 +08:00
this.AutoScaleMode = AutoScaleMode.Dpi;
this.Font = new System.Drawing.Font("Microsoft YaHei UI", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.FormBorderStyle = FormBorderStyle.Sizable;
this.MaximizeBox = true;
2026-01-28 16:54:11 +08:00
this.MinimizeBox = true;
2026-01-26 08:00:40 +08:00
this.lblStyle = new Label();
this.cmbStyles = new ComboBox();
2026-01-29 08:22:08 +08:00
this.lblSource = new Label();
this.rbClipboard = new RadioButton();
this.rbInput = new RadioButton();
this.txtInput = new TextBox();
2026-01-26 08:00:40 +08:00
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-29 08:22:08 +08:00
this.lblStyle.Text = "1. 图片风格:";
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);
2026-01-29 08:22:08 +08:00
this.cmbStyles.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
2026-01-26 08:00:40 +08:00
this.cmbStyles.SelectedIndexChanged += CmbStyles_SelectedIndexChanged;
2026-01-29 08:22:08 +08:00
// lblSource
this.lblSource.AutoSize = true;
this.lblSource.Location = new Point(20, 85);
this.lblSource.Text = "2. 内容来源:";
// rbClipboard
this.rbClipboard.AutoSize = true;
this.rbClipboard.Location = new Point(40, 110);
this.rbClipboard.Text = "使用剪贴板内容";
this.rbClipboard.CheckedChanged += RbSource_CheckedChanged;
// rbInput
this.rbInput.AutoSize = true;
this.rbInput.Location = new Point(200, 110);
this.rbInput.Text = "手动录入/编辑";
this.rbInput.CheckedChanged += RbSource_CheckedChanged;
// txtInput
this.txtInput.Location = new Point(40, 140);
this.txtInput.Multiline = true;
this.txtInput.ScrollBars = ScrollBars.Vertical;
this.txtInput.Size = new Size(420, 100);
this.txtInput.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
this.txtInput.TextChanged += TxtInput_TextChanged;
2026-01-24 18:05:24 +08:00
// lblPrompt
this.lblPrompt.AutoSize = true;
2026-01-29 08:22:08 +08:00
this.lblPrompt.Location = new Point(20, 255);
this.lblPrompt.Text = "3. 最终提示词预览 (可直接修改):";
2026-01-26 08:00:40 +08:00
// txtFinalPrompt
2026-01-29 08:22:08 +08:00
this.txtFinalPrompt.Location = new Point(20, 280);
2026-01-26 08:00:40 +08:00
this.txtFinalPrompt.Multiline = true;
this.txtFinalPrompt.ScrollBars = ScrollBars.Vertical;
2026-01-29 08:22:08 +08:00
this.txtFinalPrompt.Size = new Size(440, 220);
this.txtFinalPrompt.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
2026-01-26 08:00:40 +08:00
// chkOptimize
this.chkOptimize.AutoSize = true;
2026-01-29 08:22:08 +08:00
this.chkOptimize.Location = new Point(20, 520);
this.chkOptimize.Size = new Size(400, 24);
2026-01-26 08:00:40 +08:00
this.chkOptimize.Text = "使用 AI 优化提示词 (自动翻译成英文并丰富细节)";
2026-01-28 16:54:11 +08:00
this.chkOptimize.Checked = true;
2026-01-29 08:22:08 +08:00
this.chkOptimize.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
2026-01-26 08:00:40 +08:00
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;
2026-01-29 08:22:08 +08:00
this.btnOk.Location = new Point(260, 560);
this.btnOk.Size = new Size(100, 35);
this.btnOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
2026-01-26 08:00:40 +08:00
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;
2026-01-29 08:22:08 +08:00
this.btnCancel.Location = new Point(380, 560);
this.btnCancel.Size = new Size(80, 35);
this.btnCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
2026-01-26 08:00:40 +08:00
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);
2026-01-29 08:22:08 +08:00
this.Controls.Add(this.lblSource);
this.Controls.Add(this.rbClipboard);
this.Controls.Add(this.rbInput);
this.Controls.Add(this.txtInput);
2026-01-26 08:00:40 +08:00
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);
2026-01-29 08:22:08 +08:00
// Initialize Source Logic
// If SourceText is provided (from Selection), populate txtInput and select rbInput
if (!string.IsNullOrWhiteSpace(this.SourceText))
{
this.txtInput.Text = this.SourceText;
this.rbInput.Checked = true;
}
else
{
// No selection, check clipboard
this.rbClipboard.Checked = true;
// Trigger logic to load clipboard if possible
RbSource_CheckedChanged(this.rbClipboard, EventArgs.Empty);
}
// Default Style Selection
2026-01-26 08:00:40 +08:00
if (this.cmbStyles.Items.Count > 0)
{
int index = this.cmbStyles.FindString("逻辑架构图");
this.cmbStyles.SelectedIndex = index >= 0 ? index : 0;
}
else
{
2026-01-29 08:22:08 +08:00
UpdatePrompt();
}
}
private void RbSource_CheckedChanged(object sender, EventArgs e)
{
if (rbClipboard.Checked)
{
this.txtInput.Enabled = false;
try
{
if (Clipboard.ContainsText())
{
this.SourceText = Clipboard.GetText().Trim();
}
else
{
this.SourceText = "";
}
}
catch
{
this.SourceText = "";
}
}
else if (rbInput.Checked)
{
this.txtInput.Enabled = true;
this.SourceText = this.txtInput.Text;
}
UpdatePrompt();
}
private void TxtInput_TextChanged(object sender, EventArgs e)
{
if (rbInput.Checked)
{
this.SourceText = this.txtInput.Text;
2026-01-26 08:00:40 +08:00
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
}
}
}