old_flsystem/应用/TBAppraisalTools/Forms/EditTextForm.cs

232 lines
8.4 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TBAppraisalTools.Properties;
using DevExpress.XtraRichEdit.UI;
using DevExpress.XtraRichEdit.API.Native;
using UI.Framework.Forms;
using DevExpress.XtraBars;
using System.Text.RegularExpressions;
namespace TBAppraisalTools.Forms
{
public partial class EditTextForm : BaseForm
{
public TbConfig tbConfig;
string fontName = "宋体";//字体名称
float fontSize = 12f;//字体大小
bool fontBold = false;//字体加粗/正常
bool fontItalic = false;//字体斜体/正常
bool fontUnderline = false;//字体下划线/正常
bool fontStrikeout = false;//字体删除线/正常
Color fontColor = Color.Black;
public EditTextForm(TbConfig tbConfig = null)
{
InitializeComponent();
this.Text = tbConfig == null ? Resources.AddTextTitle : Resources.EditTextTitle;
this.tbConfig = tbConfig;
}
private void EditTextForm_Load(object sender, EventArgs e)
{
if (tbConfig != null)
{
richEditControl1.Document.Text = tbConfig.T_Text;
changeFontNameItem2.EditValue = fontName = tbConfig.T_Font.Name;
changeFontSizeItem2.EditValue = fontSize = tbConfig.T_Font.Size;
barEditItem1.EditValue = fontColor = tbConfig.T_ForeColor;
fontBold = tbConfig.T_Font.Bold;
fontItalic = tbConfig.T_Font.Italic;
fontUnderline = tbConfig.T_Font.Underline;
fontStrikeout = tbConfig.T_Font.Strikeout;
}
}
/// <summary>
/// 样式有变化时.
/// </summary>
private void Changed()
{
try
{
var doc = richEditControl1.Document;
doc.BeginUpdate();
var cp = doc.BeginUpdateCharacters(0, doc.Text.Length);
cp.FontName = fontName;
cp.FontSize = fontSize;
cp.Bold = fontBold;
cp.Italic = fontItalic;
cp.Underline = fontUnderline ? UnderlineType.Single : UnderlineType.None;
cp.Strikeout = fontStrikeout ? StrikeoutType.Single : StrikeoutType.None;
cp.ForeColor = fontColor;
doc.EndUpdateCharacters(cp);
doc.EndUpdate();
}
catch (Exception ex)
{
ShowError(ex);
}
}
/// <summary>
/// 窗体关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void EditTextForm_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
if (string.IsNullOrWhiteSpace(richEditControl1.Document.Text)) throw new Exception("内容不能为空");
if (MessageBox.Show("是否保存?", "温馨提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
if (tbConfig == null)
{
tbConfig = new TbConfig()
{
T_Font = new Font(fontName, fontSize, (fontBold ? FontStyle.Bold : FontStyle.Regular) | (fontItalic ? FontStyle.Italic : FontStyle.Regular) | (fontUnderline ? FontStyle.Underline : FontStyle.Regular) | (fontStrikeout ? FontStyle.Strikeout : FontStyle.Regular)),
T_ForeColor = fontColor,
T_Text = richEditControl1.Document.Text
};
}
else
{
tbConfig.T_Text = richEditControl1.Document.Text;
tbConfig.T_ForeColor = fontColor;
tbConfig.T_Font = new Font(fontName, fontSize, (fontBold ? FontStyle.Bold : FontStyle.Regular) | (fontItalic ? FontStyle.Italic : FontStyle.Regular) | (fontUnderline ? FontStyle.Underline : FontStyle.Regular) | (fontStrikeout ? FontStyle.Strikeout : FontStyle.Regular));
}
this.DialogResult = DialogResult.OK;
}
}
catch (Exception ex)
{
ShowError(ex);
}
}
/// <summary>
/// 字体名字
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void changeFontNameItem2_EditValueChanged(object sender, EventArgs e)
{
if (changeFontNameItem2.EditValue != null)
{
fontName = changeFontNameItem2.EditValue.ToString();
Changed();
}
}
/// <summary>
/// 字体大小
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void changeFontSizeItem2_EditValueChanged(object sender, EventArgs e)
{
if (changeFontSizeItem2.EditValue != null)
{
fontSize = float.Parse(changeFontSizeItem2.EditValue.ToString());
Changed();
}
}
/// <summary>
/// 字体颜色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void barEditItem1_EditValueChanged(object sender, EventArgs e)
{
if (barEditItem1.EditValue != null)
{
var barEditItem = barEditItem1.EditValue;
if (barEditItem != null)
{
var fontColorStr = barEditItem1.EditValue.ToString();
var reg = Regex.Match(fontColorStr, @"Color \[A=(?<A>\d+?), R=(?<R>\d+?), G=(?<G>\d+?), B=(?<B>\d+?)\]");
if (reg.Success)
{
fontColor = Color.FromArgb(int.Parse(reg.Groups["A"].Value), int.Parse(reg.Groups["R"].Value), int.Parse(reg.Groups["G"].Value), int.Parse(reg.Groups["B"].Value));
richEditControl1.Document.DefaultCharacterProperties.ForeColor = fontColor;
Changed();
}
}
}
}
/// <summary>
/// 字体粗体/正常
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toggleFontBoldItem1_ItemClick(object sender, ItemClickEventArgs e)
{
var toggleFontBoldItem = e.Item as ToggleFontBoldItem;
fontBold = toggleFontBoldItem.Checked;
Changed();
}
/// <summary>
/// 文字斜体/正常
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toggleFontItalicItem1_ItemClick(object sender, ItemClickEventArgs e)
{
var toggleFontItalicItem = e.Item as ToggleFontItalicItem;
fontItalic = toggleFontItalicItem.Checked;
Changed();
}
/// <summary>
/// 文字下划线/正常
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toggleFontUnderlineItem1_ItemClick(object sender, ItemClickEventArgs e)
{
var toggleFontUnderlineItem = e.Item as ToggleFontUnderlineItem;
fontUnderline = toggleFontUnderlineItem.Checked;
Changed();
}
/// <summary>
/// 文字删除线/正常
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toggleFontStrikeoutItem2_ItemClick(object sender, ItemClickEventArgs e)
{
var toggleFontStrikeoutItem = e.Item as ToggleFontStrikeoutItem;
fontStrikeout = toggleFontStrikeoutItem.Checked;
Changed();
}
private void label_Click(object sender, EventArgs e)
{
try
{
if (sender.GetType() == typeof(Label))
{
var label = sender as Label;
richEditControl1.Text += label.Text;
}
}
catch (Exception ex)
{
ShowError(ex);
}
}
}
}