Files
aiData/WordAddIn/WordAddIn/ImageOptionsForm.cs
HuangHai 8101a3eafc 'commit'
2026-01-29 08:22:08 +08:00

320 lines
13 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using WordAddIn.Properties;
namespace WordAddIn
{
/// <summary>
/// 生成配图设置窗体。
/// 功能描述:
/// 1. 允许用户选择图片风格(包括多种架构图)。
/// 2. 自动根据选择的风格生成提示词模板。
/// 3. 允许用户直接编辑最终发送给 AI 的提示词。
/// </summary>
public class ImageOptionsForm : Form
{
/// <summary>
/// 最终生成的提示词(用户编辑后的版本)。
/// </summary>
public string FinalPrompt { get; private set; }
/// <summary>
/// 是否需要 AI 进一步优化提示词(翻译/润色)。
/// 对于架构图通常为 false(直接使用模板),对于艺术画通常为 true。
/// </summary>
public bool ShouldOptimize { get; private set; }
/// <summary>
/// 源文本(从 Word 选区或剪贴板获取)。
/// </summary>
public string SourceText { get; set; }
/// <summary>
/// 用户选择的风格名称(用于后续可能的引用,虽然后端逻辑已主要依赖 FinalPrompt)
/// </summary>
public string SelectedStyle { get; private set; }
private ComboBox cmbStyles;
private RadioButton rbClipboard;
private RadioButton rbInput;
private TextBox txtInput;
private TextBox txtFinalPrompt;
private CheckBox chkOptimize;
private Button btnOk;
private Button btnCancel;
private Label lblStyle;
private Label lblSource;
private Label lblPrompt;
private readonly Dictionary<string, string> _styleTemplates = new Dictionary<string, string>();
public ImageOptionsForm()
{
InitializeTemplates();
InitializeComponent();
foreach (var key in _styleTemplates.Keys)
{
this.cmbStyles.Items.Add(key);
}
}
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);
}
private void InitializeComponent()
{
this.Text = "生成配图设置";
this.Size = new Size(500, 650);
this.MinimumSize = new Size(400, 500);
this.StartPosition = FormStartPosition.CenterParent;
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;
this.MinimizeBox = true;
this.lblStyle = new Label();
this.cmbStyles = new ComboBox();
this.lblSource = new Label();
this.rbClipboard = new RadioButton();
this.rbInput = new RadioButton();
this.txtInput = new TextBox();
this.lblPrompt = new Label();
this.txtFinalPrompt = new TextBox();
this.chkOptimize = new CheckBox();
this.btnOk = new Button();
this.btnCancel = new Button();
this.SuspendLayout();
// lblStyle
this.lblStyle.AutoSize = true;
this.lblStyle.Location = new Point(20, 20);
this.lblStyle.Text = "1. 图片风格:";
// cmbStyles
this.cmbStyles.DropDownStyle = ComboBoxStyle.DropDownList;
this.cmbStyles.Location = new Point(20, 45);
this.cmbStyles.Size = new Size(440, 25);
this.cmbStyles.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
this.cmbStyles.SelectedIndexChanged += CmbStyles_SelectedIndexChanged;
// 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;
// lblPrompt
this.lblPrompt.AutoSize = true;
this.lblPrompt.Location = new Point(20, 255);
this.lblPrompt.Text = "3. 最终提示词预览 (可直接修改):";
// txtFinalPrompt
this.txtFinalPrompt.Location = new Point(20, 280);
this.txtFinalPrompt.Multiline = true;
this.txtFinalPrompt.ScrollBars = ScrollBars.Vertical;
this.txtFinalPrompt.Size = new Size(440, 220);
this.txtFinalPrompt.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
// chkOptimize
this.chkOptimize.AutoSize = true;
this.chkOptimize.Location = new Point(20, 520);
this.chkOptimize.Size = new Size(400, 24);
this.chkOptimize.Text = "使用 AI 优化提示词 (自动翻译成英文并丰富细节)";
this.chkOptimize.Checked = true;
this.chkOptimize.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
// btnOk
this.btnOk.Text = "开始生成";
this.btnOk.DialogResult = DialogResult.OK;
this.btnOk.Location = new Point(260, 560);
this.btnOk.Size = new Size(100, 35);
this.btnOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
this.btnOk.Click += BtnOk_Click;
// btnCancel
this.btnCancel.Text = "取消";
this.btnCancel.DialogResult = DialogResult.Cancel;
this.btnCancel.Location = new Point(380, 560);
this.btnCancel.Size = new Size(80, 35);
this.btnCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
this.Controls.Add(this.lblStyle);
this.Controls.Add(this.cmbStyles);
this.Controls.Add(this.lblSource);
this.Controls.Add(this.rbClipboard);
this.Controls.Add(this.rbInput);
this.Controls.Add(this.txtInput);
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);
this.ResumeLayout(false);
this.PerformLayout();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// 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
if (this.cmbStyles.Items.Count > 0)
{
int index = this.cmbStyles.FindString("逻辑架构图");
this.cmbStyles.SelectedIndex = index >= 0 ? index : 0;
}
else
{
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;
UpdatePrompt();
}
}
private void CmbStyles_SelectedIndexChanged(object sender, EventArgs e)
{
UpdatePrompt();
}
private void UpdatePrompt()
{
if (cmbStyles.SelectedItem == null) return;
string styleKey = cmbStyles.SelectedItem.ToString();
string template = _styleTemplates.ContainsKey(styleKey) ? _styleTemplates[styleKey] : null;
// 只有当文本框为空,或者文本框内容是上一个模板生成的内容时,才自动覆盖
// 但为了简单起见,这里策略是:只要切换风格,就重置提示词。用户如果修改了,切换风格会丢失修改。
// 这是一个权衡。为了体验更好,可以加个判断,但现在先强制刷新。
if (template != null)
{
// 是架构图类:使用模板
this.txtFinalPrompt.Text = template.Replace("{0}", this.SourceText ?? "");
// 用户要求默认选中 AI 优化
this.chkOptimize.Checked = true;
}
else
{
// 是艺术风格类:直接显示原文本,开启 AI 优化
this.txtFinalPrompt.Text = this.SourceText ?? "";
this.chkOptimize.Checked = true;
}
}
private void BtnOk_Click(object sender, EventArgs e)
{
this.FinalPrompt = this.txtFinalPrompt.Text;
this.ShouldOptimize = this.chkOptimize.Checked;
this.SelectedStyle = this.cmbStyles.SelectedItem?.ToString();
}
}
}