📄 paramform.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text;
namespace CodeTemplate
{
/// <summary>
/// Summary description for ParamForm.
/// </summary>
internal class ParamForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button m_btnOk;
private System.Windows.Forms.Button m_btnCancel;
private System.Windows.Forms.Panel m_panel;
private System.Windows.Forms.Button m_btnPreview;
private System.Windows.Forms.TextBox m_txtPreview;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public ParamForm(String caption, String templateText)
{
InitializeComponent();
this.Text = caption;
m_templateText = templateText;
m_txtPreview.Text = Delim.Unescape(templateText);
m_txtPreview.Font = new Font(Configuration.Preview.FontFamilyName, Configuration.Preview.FontSize);
m_textBoxes = new ArrayList(16);
m_tagNames = new ArrayList(16);
m_tags = new ArrayList(16);
}
public void AddField(Tag tag)
{
m_tags.Add(tag);
if(m_tagNames.Contains(tag.Name)) // Only add field once
return;
LabeledControlBase tb = LabeledControlBase.CreateControl(tag);
tb.Label = tag.Name + ":";
tb.Tag = tag.Name;
m_textBoxes.Add(tb);
m_tagNames.Add(tag.Name);
}
public new String Text
{
get { return (this.DialogResult == DialogResult.OK) ? m_txtPreview.Text : m_templateText; }
set { base.Text = value; }
}
private void ParamForm_Load(Object sender, System.EventArgs e)
{
// Find out widest label
Graphics grfx = CreateGraphics();
int maxWidth = 0;
foreach(LabeledControlBase tb in m_textBoxes)
maxWidth = Math.Max(maxWidth, (int)Math.Ceiling(grfx.MeasureString(tb.Label, this.Font).Width));
grfx.Dispose();
const int margin = 9;
int curY = margin;
int tabIndex = 0;
foreach(LabeledControlBase tb in m_textBoxes)
{
tb.Location = new Point(margin, curY);
tb.LabelWidth = maxWidth;
Size sz = new Size(m_btnOk.Left - (2 * margin), tb.Height);
tb.Size = sz;
tb.TabIndex = tabIndex++;
Controls.Add(tb);
curY += sz.Height + margin;
tb.TagChanged += new TagChangedEventHandler(OnTagChanged);
}
m_btnOk.TabIndex = tabIndex++;
m_btnCancel.TabIndex = tabIndex++;
m_btnPreview.TabIndex = tabIndex++;
if(curY < m_btnCancel.Bottom)
curY = m_btnCancel.Bottom;
else
curY -= margin; // Remove margin (bottom of last control)
m_btnPreview.Location = new Point(m_btnPreview.Left, curY - m_btnPreview.Height);
m_panel.Location = new Point(m_panel.Left, m_btnPreview.Bottom + (margin * 2));
m_txtPreview.TabIndex = tabIndex++;
m_txtPreview.Location = new Point(m_txtPreview.Left, m_panel.Bottom + margin);
m_previewSize = new Size(ClientSize.Width, m_txtPreview.Bottom + (margin * 2));
m_noPreviewSize = new Size(ClientSize.Width, m_panel.Top);
EnablePreview(Configuration.Preview.Enabled);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(Boolean disposing)
{
if(disposing)
{
if(components != null)
components.Dispose();
}
base.Dispose(disposing);
}
private void OnTagChanged(Object sender, TagChangedEventArgs e)
{
StringBuilder sb = new StringBuilder(m_templateText);
foreach(Tag tag in m_tags)
{
if(tag.Name == e.Name)
tag.Value = (e.Value == string.Empty) ? null: e.Value;
if(tag.Value != null)
TemplateFormatter.ReplaceTag(ref sb, tag);
}
m_txtPreview.Text = Delim.Unescape(sb);
}
private void EnablePreview(Boolean enable)
{
m_enablePreview = enable;
if(m_enablePreview)
{
this.ClientSize = m_previewSize;
m_btnPreview.Text = "<< Preview";
}
else
{
this.ClientSize = m_noPreviewSize;
m_btnPreview.Text = "Preview >>";
}
}
private void m_btnPreview_Click(Object sender, System.EventArgs e)
{
EnablePreview(!m_enablePreview);
}
private void m_btnOk_Click(Object sender, System.EventArgs e)
{
// Make sure all Tags are processed
StringBuilder sb = new StringBuilder(m_templateText);
TemplateFormatter.ReplaceTags(ref sb, m_tags);
m_txtPreview.Text = Delim.Unescape(sb);
}
private ArrayList m_textBoxes;
private ArrayList m_tagNames;
private ArrayList m_tags;
private String m_templateText;
private Size m_previewSize;
private Size m_noPreviewSize;
private Boolean m_enablePreview;
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.m_btnOk = new System.Windows.Forms.Button();
this.m_btnCancel = new System.Windows.Forms.Button();
this.m_panel = new System.Windows.Forms.Panel();
this.m_btnPreview = new System.Windows.Forms.Button();
this.m_txtPreview = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// m_btnOk
//
this.m_btnOk.DialogResult = System.Windows.Forms.DialogResult.OK;
this.m_btnOk.Location = new System.Drawing.Point(360, 8);
this.m_btnOk.Name = "m_btnOk";
this.m_btnOk.TabIndex = 2;
this.m_btnOk.Text = "OK";
this.m_btnOk.Click += new System.EventHandler(this.m_btnOk_Click);
//
// m_btnCancel
//
this.m_btnCancel.CausesValidation = false;
this.m_btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.m_btnCancel.Location = new System.Drawing.Point(360, 40);
this.m_btnCancel.Name = "m_btnCancel";
this.m_btnCancel.TabIndex = 3;
this.m_btnCancel.Text = "Cancel";
//
// m_panel
//
this.m_panel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.m_panel.Location = new System.Drawing.Point(8, 208);
this.m_panel.Name = "m_panel";
this.m_panel.Size = new System.Drawing.Size(424, 4);
this.m_panel.TabIndex = 4;
//
// m_btnPreview
//
this.m_btnPreview.Location = new System.Drawing.Point(360, 176);
this.m_btnPreview.Name = "m_btnPreview";
this.m_btnPreview.TabIndex = 5;
this.m_btnPreview.Text = "<< Preview";
this.m_btnPreview.Click += new System.EventHandler(this.m_btnPreview_Click);
//
// m_txtPreview
//
this.m_txtPreview.Location = new System.Drawing.Point(8, 224);
this.m_txtPreview.Multiline = true;
this.m_txtPreview.Name = "m_txtPreview";
this.m_txtPreview.ReadOnly = true;
this.m_txtPreview.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.m_txtPreview.Size = new System.Drawing.Size(424, 208);
this.m_txtPreview.TabIndex = 6;
this.m_txtPreview.Text = "";
this.m_txtPreview.WordWrap = false;
//
// ParamForm
//
this.AcceptButton = this.m_btnOk;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.CancelButton = this.m_btnCancel;
this.ClientSize = new System.Drawing.Size(442, 440);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.m_txtPreview,
this.m_btnPreview,
this.m_panel,
this.m_btnCancel,
this.m_btnOk});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "ParamForm";
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Load += new System.EventHandler(this.ParamForm_Load);
this.ResumeLayout(false);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -