345 lines
11 KiB
C#
345 lines
11 KiB
C#
/******************************
|
|
* 说明:自定义备注框
|
|
* 创建人:龚宇超
|
|
* 创建日期:2017-08-07
|
|
* 修改人:
|
|
* 修改日期:
|
|
* 修改备注:
|
|
* 版本:1.0.0.0
|
|
******************************/
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Drawing;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using DevExpress.XtraEditors;
|
|
using System.IO;
|
|
|
|
namespace Lskj.Control
|
|
{
|
|
/// <summary>
|
|
/// 自定义备注框
|
|
/// </summary>
|
|
public partial class LabelRemarkEdit : BaseUserControl
|
|
{
|
|
/// <summary>
|
|
/// 当 lasttext 为空(清空)时触发。
|
|
/// </summary>
|
|
public event EventHandler LastTextEmpty;
|
|
/// <summary>
|
|
/// 文本控件
|
|
/// </summary>
|
|
public MemoEdit TextEdit { get { return txtEdit; } }
|
|
/// <summary>
|
|
/// 存储文件地址(用于保存上传)
|
|
/// </summary>
|
|
public string filepath;
|
|
/// <summary>
|
|
/// 存储原文件名
|
|
/// </summary>
|
|
public string lasttext;
|
|
/// <summary>
|
|
/// 是否变更新增
|
|
/// </summary>
|
|
public bool changeAdd;
|
|
public LabelRemarkEdit()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
public void BandAllowDrop()
|
|
{
|
|
txtEdit.AllowDrop = true;
|
|
txtEdit.DragEnter += OnDragEnter;
|
|
txtEdit.DragDrop += OnDragDrop;
|
|
}
|
|
/// <summary>
|
|
/// 拖拽完成触发
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void OnDragDrop(object sender, DragEventArgs e)
|
|
{
|
|
var file = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
|
|
string filename = Path.GetFileName(file);
|
|
if (!string.IsNullOrEmpty(lasttext) && string.Equals(lasttext, filename, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
MessageUtil.Show("检测到更改文件和原文件完全一样!");
|
|
}
|
|
else
|
|
{
|
|
string prefix1 = GetPrefixBeforeLastS(filename, StringComparison.OrdinalIgnoreCase);
|
|
string prefix2 = GetPrefixBeforeLastS(lasttext, StringComparison.OrdinalIgnoreCase);
|
|
if (!string.IsNullOrEmpty(lasttext) && !string.Equals(prefix1, prefix2, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
MessageUtil.Show("检测到更改文件和原文件不匹配!");
|
|
}
|
|
else
|
|
{
|
|
txtEdit.Text = filename;
|
|
this.filepath = file;
|
|
OnLastTextEmpty();
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 拖拽为空触发回调
|
|
/// </summary>
|
|
protected virtual void OnLastTextEmpty()
|
|
{
|
|
LastTextEmpty?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
/// <summary>
|
|
/// 取出最后一次出现 "-S" 之前的部分(不含扩展名)
|
|
/// </summary>
|
|
private static string GetPrefixBeforeLastS(string fileName, StringComparison comp)
|
|
{
|
|
if (string.IsNullOrEmpty(fileName)) return string.Empty;
|
|
|
|
// 1) 去掉扩展名
|
|
int dot = fileName.LastIndexOf('.');
|
|
string noExt = dot >= 0 ? fileName.Substring(0, dot) : fileName;
|
|
|
|
// 2) 找最后一次出现 "-S"
|
|
int pos = noExt.LastIndexOf("-S", comp);
|
|
return pos >= 0 ? noExt.Substring(0, pos) : noExt;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:移动到目标表格</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-11-13 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="sender">The source of the event.</param>
|
|
/// <param name="e">The <see cref="System.Windows.Forms.DragEventArgs"/> instance containing the event data.</param>
|
|
protected void OnDragEnter(object sender, System.Windows.Forms.DragEventArgs e)
|
|
{
|
|
if (e.Data.GetDataPresent(DataFormats.FileDrop) &&
|
|
((string[])e.Data.GetData(DataFormats.FileDrop)).Length == 1)
|
|
e.Effect = DragDropEffects.Copy;
|
|
else
|
|
e.Effect = DragDropEffects.None;
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>说明:设置控件显示文本</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-08-07 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="text">显示Lebel文本</param>
|
|
public override string LabelText
|
|
{
|
|
get
|
|
{
|
|
return this.lblText.Text;
|
|
}
|
|
set
|
|
{
|
|
this.lblText.Text = value;
|
|
if (FontSize > 0)
|
|
{
|
|
this.plLeft.Dock = DockStyle.Left;
|
|
this.plLeft.AutoSize = false;
|
|
this.lblText.AutoSize = false;
|
|
this.lblText.Dock = System.Windows.Forms.DockStyle.Fill;
|
|
this.lblText.Location = new System.Drawing.Point(0, 0);
|
|
this.lblText.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
|
|
this.txtEdit.Properties.AutoHeight = false;
|
|
this.plLeft.Width = value.Length * GetCharWidth();
|
|
|
|
GetCharWidthMultilingual(this.lblText, this.lblText.Text, this.plLeft);
|
|
}
|
|
else
|
|
{
|
|
Graphics g = this.lblText.CreateGraphics();
|
|
SizeF sizeF = g.MeasureString(value, this.lblText.Font);
|
|
this.plLeft.Width = (int)sizeF.Width + PaddingLeft;
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 字体大小
|
|
/// </summary>
|
|
/// <value>The size of the control.</value>
|
|
public override float FontSize
|
|
{
|
|
get
|
|
{
|
|
return base.FontSize;
|
|
}
|
|
set
|
|
{
|
|
base.FontSize = value;
|
|
if (value > 0)
|
|
{
|
|
this.lblText.Font = new Font(this.lblText.Font.FontFamily, value);
|
|
this.TextEdit.Font = new Font(this.TextEdit.Font.FontFamily, value);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:设置控件值</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-08-07 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
public override string EditText
|
|
{
|
|
get
|
|
{
|
|
return this.txtEdit.Text;
|
|
}
|
|
set
|
|
{
|
|
//this.txtEdit.Text = value;
|
|
this.txtEdit.Text = NormalizeMemoText(value);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 处理换行 (\n的换行不会生效,必须要\r\n才行。这里会把\n转成\r\n)
|
|
/// </summary>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
private static string NormalizeMemoText(string value)
|
|
{
|
|
if (string.IsNullOrEmpty(value)) return value;
|
|
|
|
return value
|
|
.Replace("\r\n", "\n") // 先把 CRLF 压成 LF
|
|
.Replace("\r", "\n") // 再把单独 CR 压成 LF
|
|
.Replace("\n", Environment.NewLine); // 最后统一成 Windows CRLF
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// <para>说明:设置控件提示文本</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-08-07 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <returns>返回控件值</returns>
|
|
public override string NullText
|
|
{
|
|
get
|
|
{
|
|
return this.txtEdit.Properties.NullValuePrompt;
|
|
}
|
|
set
|
|
{
|
|
if (!string.IsNullOrEmpty(value))
|
|
{
|
|
this.TextEdit.Properties.NullValuePromptShowForEmptyValue = true;
|
|
this.TextEdit.Properties.NullValuePrompt = value;
|
|
}
|
|
base.NullText = value;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 只读文本颜色
|
|
/// </summary>
|
|
/// <value>The color of the read only label.</value>
|
|
public override bool ReadOnly
|
|
{
|
|
get
|
|
{
|
|
return base.ReadOnly;
|
|
}
|
|
set
|
|
{
|
|
base.ReadOnly = value;
|
|
this.txtEdit.Properties.ReadOnly = value;
|
|
this.lblText.ForeColor = value ? base.ReadOnlyLabelForceColor : Required ? base.RequiredLabelForceColor : base.DefaultLabelForceColor;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 必填文本颜色
|
|
/// </summary>
|
|
/// <value>The color of the required label.</value>
|
|
public override bool Required
|
|
{
|
|
get
|
|
{
|
|
return base.Required;
|
|
}
|
|
set
|
|
{
|
|
base.Required = value;
|
|
if (value)
|
|
{
|
|
this.lblText.ForeColor = base.RequiredLabelForceColor;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 背景颜色
|
|
/// </summary>
|
|
/// <value>The color of the required label.</value>
|
|
public override Color BackgroundColor
|
|
{
|
|
get
|
|
{
|
|
return TextEdit.Properties.Appearance.BackColor;
|
|
}
|
|
set
|
|
{
|
|
TextEdit.Properties.Appearance.BackColor = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 前景颜色
|
|
/// </summary>
|
|
/// <value>The color of the required label.</value>
|
|
public override Color ForeColor
|
|
{
|
|
get
|
|
{
|
|
return TextEdit.Properties.Appearance.ForeColor;
|
|
}
|
|
set
|
|
{
|
|
TextEdit.Properties.Appearance.ForeColor = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 内容加粗
|
|
/// </summary>
|
|
/// <value>The color of the required label.</value>
|
|
public override bool ContentBold
|
|
{
|
|
get
|
|
{
|
|
return TextEdit.Properties.Appearance.Font.Bold;
|
|
}
|
|
set
|
|
{
|
|
Font oldFont = TextEdit.Properties.Appearance.Font;
|
|
TextEdit.Properties.Appearance.Font = new Font(oldFont.FontFamily, oldFont.Size, value ? FontStyle.Bold : FontStyle.Regular);
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|