using System;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace WinFormClipboard
{
public partial class FormClipboard : Form
{
#region
private readonly string animals;
private UriBuilder url;
private ChineseLunisolarCalendar lunarCalendar;
#endregion
public FormClipboard()
{
#region
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
animals = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
lunarCalendar = new ChineseLunisolarCalendar(); // 中国阴阳历。
toolComboBoxData.ComboBox.DataSource = Enum.GetValues(typeof(TextDataFormat));
splitContainerInfo.FixedPanel = FixedPanel.Panel1; // 固定面板。
splitContainerInfo.Orientation = Orientation.Vertical; // 垂直放置。
richText.AcceptsTab = true; // Tab ~ Ctrl+Tab。
richText.AllowDrop = true; // 允许拖放操作。
richText.DetectUrls = true; // 自动设置 URL 链接格式。
richText.EnableAutoDragDrop = true; // 在文本、图片和其他数据上启用拖放操作。
richText.HideSelection = false; // 无焦点时,选定文本保持突出显示。
richText.ShortcutsEnabled = true; // 启用定义的快捷方式。
richText.ShowSelectionMargin = true; // 显示选定内容的边距。
richText.DragEnter += new DragEventHandler(richText_DragEnter);
richText.DragDrop += new DragEventHandler(richText_DragDrop);
richText.KeyDown += new KeyEventHandler(richText_KeyDown);
richText.LinkClicked += new LinkClickedEventHandler(richText_LinkClicked);
richText.SelectionChanged += new EventHandler(richText_SelectionChanged);
#endregion
}
#region Timer
private void timerInfo_Tick(object sender, System.EventArgs e)
{
Application.CurrentCulture.ClearCachedData();
DateTime solar = DateTime.Now;
int year = lunarCalendar.GetYear(solar);
int month = lunarCalendar.GetMonth(solar);
int leapMonth = lunarCalendar.GetLeapMonth(year);
if (0 < leapMonth && leapMonth <= month)
--month;
statusLabelTime.Text = string.Format("{0:F} [{1}年 {2} {3:00}]", solar, animals[(year - 4) % 12], DateTimeFormatInfo.CurrentInfo.MonthNames[month - 1], lunarCalendar.GetDayOfMonth(solar));
}
#endregion
#region DragDropFile
private void richText_DragEnter(object sender, DragEventArgs e)
{
richText.EnableAutoDragDrop = !(e.Data as DataObject).ContainsFileDropList();
}
private void richText_DragDrop(object sender, DragEventArgs e)
{
if (richText.EnableAutoDragDrop)
return;
string filePath = (e.Data as DataObject).GetFileDropList()[0];
if ((File.GetAttributes(filePath) & FileAttributes.Directory) != 0)
return;
if (Regex.IsMatch(Path.GetExtension(filePath), @".(bmp|gif|jpg|png)", RegexOptions.IgnoreCase)) // 指定不区分大小写的匹配。
using (Bitmap bmp = new Bitmap(filePath))
{
IDataObject data = Clipboard.GetDataObject();
Clipboard.SetImage(bmp);
if (Clipboard.ContainsImage())
richText.Paste();
Clipboard.SetDataObject(data, true);
}
this.Activate(); // 激活窗体并给予它焦点。
}
private void toolButtonImage_Click(object sender, EventArgs e)
{
if (Clipboard.ContainsImage())
{
richText.Clear();
richText.Paste();
}
}
#endregion
#region SuppressKeyPress
private void richText_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == (Keys.Control | Keys.V))
e.SuppressKeyPress = Clipboard.ContainsFileDropList() && !Clipboard.ContainsImage();
}
#endregion
#region LinkClicked
private void richText_LinkClicked(object sender, LinkClickedEventArgs e)
{
propertyGridURL.SelectedObject = url = new UriBuilder(e.LinkText);
}
#endregion
#region FileDropList
private void toolButtonFileDrop_Click(object sender, EventArgs e)
{
richText.Lines = Clipboard.GetData(DataFormats.FileDrop) as string[];
}
#endregion
#region TextDataFormat
private void toolComboBoxData_SelectedIndexChanged(object sender, EventArgs e)
{
TextDataFormat format = (TextDataFormat)toolComboBoxData.SelectedItem;
string formatText = Clipboard.GetText(format);
if (format == TextDataFormat.Html)
LinkALL(formatText);
else
richText.Text = formatText;
}
#endregion
#region
private void toolButtonSelectAll_Click(object sender, EventArgs e)
{
richText.SelectAll();
}
private void toolButtonCut_Click(object sender, EventArgs e)
{
richText.Cut();
}
private void toolButtonCopy_Click(object sender, EventArgs e)
{
richText.Copy();
}
private void toolButtonPaste_Click(object sender, EventArgs e)
{
if (Clipboard.ContainsFileDropList())
{
if (Clipboard.ContainsImage())
richText.Paste();
}
else if (Clipboard.ContainsText(TextDataFormat.Html))
{
string htm = Clipboard.GetText(TextDataFormat.Html);
richText.SelectedText = Encoding.UTF8.GetString(Encoding.Default.GetBytes(htm));
}
else
richText.Paste();
}
private void toolButtonClear_Click(object sender, EventArgs e)
{
if (MessageBox.Show("确实要从剪贴板中移除所有数据吗?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
Clipboard.Clear();
}
private void richText_SelectionChanged(object sender, EventArgs e)
{
bool flag = (richText.SelectionType != RichTextBoxSelectionTypes.Empty);
toolButtonCopy.Enabled = flag;
toolButtonCut.Enabled = flag;
}
#endregion
#region href
private void LinkALL(string html)
{
try
{
String pattern = @"<a[^>]+href=/s*(?:'(?<href>[^']+)'|""(?<href>[^""]+)""|(?<href>[^>/s]+))/s*[^>]*>(?<text>.*?)</a>"; //提取超链接的正则表达式
Regex info = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
Match rm = info.Match(html);
StringBuilder sb = new StringBuilder();
while (rm.Success)
{
string link = rm.Groups[1].Value;
if (!link.StartsWith("#") && link.Length > 8)
{
if (link.StartsWith(Uri.UriSchemeHttp + "://") || link.StartsWith(Uri.UriSchemeHttps + "://") || link.StartsWith(Uri.UriSchemeFile + "://"))
sb.AppendLine(link);
}
rm = rm.NextMatch();//继续查找超级链接
}
richText.Text = sb.ToString();
sb.Capacity = sb.Remove(0, sb.Length).Length;
sb = null;
}
catch
{
return;
}
}
#endregion
#region 读取源文件
private void toolButtonURL_Click(object sender, EventArgs e)
{
if (url == null)
return;
try
{
HttpWebRequest html = WebRequest.Create(url.ToString()) as HttpWebRequest;
HttpWebResponse wr = html.GetResponse() as HttpWebResponse;
using (StreamReader sr = new StreamReader(wr.GetResponseStream(), Encoding.Default))
{
LinkALL(sr.ReadToEnd());
}
}
catch (WebException we)
{
MessageBox.Show(we.Message);
}
}
#endregion
}
}