Files
aiData/WordAddIn/WordAddIn/ReferenceGeneratorForm.cs
HuangHai ea4bc24c4b 'commit'
2026-01-24 20:54:49 +08:00

316 lines
14 KiB
C#

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
namespace WordAddIn
{
/// <summary>
/// 素材写作(参考生成)配置窗体。
/// 功能描述:
/// 1. 允许用户提供参考素材,用于辅助 AI 生成内容。
/// 2. 支持两种输入模式:上传文件 (Word/Txt) 或 直接输入文本。
/// 3. 允许用户设置生成内容的字数上限。
/// </summary>
public class ReferenceGeneratorForm : Form
{
private TabControl tabControl;
private TabPage tabText;
private TabPage tabFile;
private RichTextBox txtReferenceInput;
private TextBox txtFilePath;
private Button btnBrowse;
private NumericUpDown numWordCount;
private Button btnGenerate;
private Button btnCancel;
private Label lblFileTip;
private Label lblWordCount;
private Label lblWordUnit;
/// <summary>
/// 获取直接输入的参考文本内容。
/// </summary>
public string ReferenceContent { get; private set; }
/// <summary>
/// 获取用户设置的字数上限。
/// </summary>
public int WordLimit { get; private set; }
/// <summary>
/// 指示是否选择了文件模式。
/// </summary>
public bool IsFileMode { get; private set; }
/// <summary>
/// 获取用户选择的文件路径。
/// </summary>
public string SelectedFilePath { get; private set; }
public ReferenceGeneratorForm()
{
InitializeComponent();
}
/// <summary>
/// 初始化窗体组件。
/// </summary>
private void InitializeComponent()
{
this.tabControl = new System.Windows.Forms.TabControl();
this.tabFile = new System.Windows.Forms.TabPage();
this.lblFileTip = new System.Windows.Forms.Label();
this.txtFilePath = new System.Windows.Forms.TextBox();
this.btnBrowse = new System.Windows.Forms.Button();
this.tabText = new System.Windows.Forms.TabPage();
this.txtReferenceInput = new System.Windows.Forms.RichTextBox();
this.lblWordCount = new System.Windows.Forms.Label();
this.numWordCount = new System.Windows.Forms.NumericUpDown();
this.lblWordUnit = new System.Windows.Forms.Label();
this.btnGenerate = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.tabControl.SuspendLayout();
this.tabFile.SuspendLayout();
this.tabText.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.numWordCount)).BeginInit();
this.SuspendLayout();
//
// tabControl
//
this.tabControl.Controls.Add(this.tabFile);
this.tabControl.Controls.Add(this.tabText);
this.tabControl.Font = new System.Drawing.Font("微软雅黑", 11F);
this.tabControl.Location = new System.Drawing.Point(25, 25);
this.tabControl.Name = "tabControl";
this.tabControl.SelectedIndex = 0;
this.tabControl.Size = new System.Drawing.Size(585, 300);
this.tabControl.TabIndex = 0;
//
// tabFile
//
this.tabFile.BackColor = System.Drawing.Color.White;
this.tabFile.Controls.Add(this.lblFileTip);
this.tabFile.Controls.Add(this.txtFilePath);
this.tabFile.Controls.Add(this.btnBrowse);
this.tabFile.Location = new System.Drawing.Point(4, 29);
this.tabFile.Name = "tabFile";
this.tabFile.Padding = new System.Windows.Forms.Padding(3);
this.tabFile.Size = new System.Drawing.Size(577, 267);
this.tabFile.TabIndex = 0;
this.tabFile.Text = "上传文档";
//
// lblFileTip
//
this.lblFileTip.AutoSize = true;
this.lblFileTip.Font = new System.Drawing.Font("微软雅黑", 12F);
this.lblFileTip.Location = new System.Drawing.Point(30, 60);
this.lblFileTip.Name = "lblFileTip";
this.lblFileTip.Size = new System.Drawing.Size(255, 21);
this.lblFileTip.TabIndex = 0;
this.lblFileTip.Text = "请选择参考文档 (支持 Word/Txt):";
//
// txtFilePath
//
this.txtFilePath.Font = new System.Drawing.Font("微软雅黑", 11F);
this.txtFilePath.Location = new System.Drawing.Point(30, 100);
this.txtFilePath.Name = "txtFilePath";
this.txtFilePath.ReadOnly = true;
this.txtFilePath.Size = new System.Drawing.Size(400, 27);
this.txtFilePath.TabIndex = 1;
//
// btnBrowse
//
this.btnBrowse.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnBrowse.Font = new System.Drawing.Font("微软雅黑", 10F);
this.btnBrowse.Location = new System.Drawing.Point(440, 98);
this.btnBrowse.Name = "btnBrowse";
this.btnBrowse.Size = new System.Drawing.Size(100, 35);
this.btnBrowse.TabIndex = 2;
this.btnBrowse.Text = "浏览...";
this.btnBrowse.UseVisualStyleBackColor = true;
this.btnBrowse.Click += new System.EventHandler(this.BtnBrowse_Click);
//
// tabText
//
this.tabText.BackColor = System.Drawing.Color.White;
this.tabText.Controls.Add(this.txtReferenceInput);
this.tabText.Location = new System.Drawing.Point(4, 29);
this.tabText.Name = "tabText";
this.tabText.Padding = new System.Windows.Forms.Padding(10);
this.tabText.Size = new System.Drawing.Size(577, 267);
this.tabText.TabIndex = 1;
this.tabText.Text = "直接输入";
//
// txtReferenceInput
//
this.txtReferenceInput.BackColor = System.Drawing.Color.WhiteSmoke;
this.txtReferenceInput.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txtReferenceInput.Dock = System.Windows.Forms.DockStyle.Fill;
this.txtReferenceInput.Font = new System.Drawing.Font("微软雅黑", 11F);
this.txtReferenceInput.Location = new System.Drawing.Point(10, 10);
this.txtReferenceInput.Name = "txtReferenceInput";
this.txtReferenceInput.Size = new System.Drawing.Size(557, 247);
this.txtReferenceInput.TabIndex = 0;
this.txtReferenceInput.Text = "";
//
// lblWordCount
//
this.lblWordCount.AutoSize = true;
this.lblWordCount.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Bold);
this.lblWordCount.Location = new System.Drawing.Point(30, 355);
this.lblWordCount.Name = "lblWordCount";
this.lblWordCount.Size = new System.Drawing.Size(117, 22);
this.lblWordCount.TabIndex = 1;
this.lblWordCount.Text = "生成字数上限:";
//
// numWordCount
//
this.numWordCount.Font = new System.Drawing.Font("微软雅黑", 14F);
this.numWordCount.Increment = new decimal(new int[] {
100,
0,
0,
0});
this.numWordCount.Location = new System.Drawing.Point(200, 350);
this.numWordCount.Maximum = new decimal(new int[] {
10000,
0,
0,
0});
this.numWordCount.Minimum = new decimal(new int[] {
100,
0,
0,
0});
this.numWordCount.Name = "numWordCount";
this.numWordCount.Size = new System.Drawing.Size(120, 32);
this.numWordCount.TabIndex = 2;
this.numWordCount.Value = new decimal(new int[] {
500,
0,
0,
0});
//
// lblWordUnit
//
this.lblWordUnit.AutoSize = true;
this.lblWordUnit.Font = new System.Drawing.Font("微软雅黑", 12F);
this.lblWordUnit.Location = new System.Drawing.Point(330, 355);
this.lblWordUnit.Name = "lblWordUnit";
this.lblWordUnit.Size = new System.Drawing.Size(26, 21);
this.lblWordUnit.TabIndex = 3;
this.lblWordUnit.Text = "字";
//
// btnGenerate
//
this.btnGenerate.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(43)))), ((int)(((byte)(87)))), ((int)(((byte)(154)))));
this.btnGenerate.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnGenerate.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnGenerate.FlatAppearance.BorderSize = 0;
this.btnGenerate.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnGenerate.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Bold);
this.btnGenerate.ForeColor = System.Drawing.Color.White;
this.btnGenerate.Location = new System.Drawing.Point(160, 410);
this.btnGenerate.Name = "btnGenerate";
this.btnGenerate.Size = new System.Drawing.Size(140, 45);
this.btnGenerate.TabIndex = 4;
this.btnGenerate.Text = "开始生成";
this.btnGenerate.UseVisualStyleBackColor = false;
this.btnGenerate.Click += new System.EventHandler(this.BtnGenerate_Click);
//
// btnCancel
//
this.btnCancel.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Font = new System.Drawing.Font("微软雅黑", 12F);
this.btnCancel.Location = new System.Drawing.Point(320, 410);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(140, 45);
this.btnCancel.TabIndex = 5;
this.btnCancel.Text = "取消";
this.btnCancel.UseVisualStyleBackColor = true;
//
// ReferenceGeneratorForm
//
this.AcceptButton = this.btnGenerate;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.White;
this.CancelButton = this.btnCancel;
this.ClientSize = new System.Drawing.Size(650, 520);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnGenerate);
this.Controls.Add(this.lblWordUnit);
this.Controls.Add(this.numWordCount);
this.Controls.Add(this.lblWordCount);
this.Controls.Add(this.tabControl);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "ReferenceGeneratorForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "素材写作 - AI助手";
this.tabControl.ResumeLayout(false);
this.tabFile.ResumeLayout(false);
this.tabFile.PerformLayout();
this.tabText.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.numWordCount)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
/// <summary>
/// 浏览按钮点击事件。
/// 打开文件选择对话框,选择参考文档。
/// </summary>
private void BtnBrowse_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Filter = "Word 文档 (*.docx;*.doc)|*.docx;*.doc|文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*";
ofd.Title = "选择参考文档";
if (ofd.ShowDialog() == DialogResult.OK)
{
txtFilePath.Text = ofd.FileName;
}
}
}
/// <summary>
/// 生成按钮点击事件。
/// 验证输入有效性,并保存用户的选择。
/// </summary>
private void BtnGenerate_Click(object sender, EventArgs e)
{
WordLimit = (int)numWordCount.Value;
if (tabControl.SelectedTab == tabFile)
{
if (string.IsNullOrWhiteSpace(txtFilePath.Text) || !File.Exists(txtFilePath.Text))
{
MessageBox.Show("请选择有效的参考文档。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
IsFileMode = true;
SelectedFilePath = txtFilePath.Text;
ReferenceContent = null; // 由调用方读取
}
else
{
string text = txtReferenceInput.Text.Trim();
if (string.IsNullOrWhiteSpace(text))
{
MessageBox.Show("请输入参考文本内容。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
IsFileMode = false;
ReferenceContent = text;
}
this.DialogResult = DialogResult.OK;
this.Close();
}
}
}