设置dragEnter
设置DragDrop
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp25 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { #region 单独设置 textBox1.AllowDrop = true;//设置控件AllowDrop的属性 textBox2.AllowDrop = true; textBox1.DragEnter += C_DragEnter;//设置控件的DragEnter效果 textBox2.DragEnter += C_DragEnter; #endregion #region 批量设置 //foreach (Control c in this.Controls) //{ // if (c is TextBox) // { // c.AllowDrop = true; // c.DragEnter += C_DragEnter; // } //} #endregion textBox1.DragDrop += TextBox1_DragDrop; } private void TextBox1_DragDrop(object sender, DragEventArgs e) { string html = GetHtmlContent(e.Data); string txt = GetStringContent(e.Data); } private void C_DragEnter(object sender, DragEventArgs e) { //设置控件的DragEnter效果 e.Effect = DragDropEffects.All; } /// <summary> /// 获得网页格式的信息 /// </summary> /// <param name="data"></param> /// <returns></returns> public static string GetHtmlContent(IDataObject data) { //先判断数据是否为html格式的数据 if (data.GetDataPresent(DataFormats.Html)) { return data.GetData(DataFormats.Html).ToString(); } else return ""; } /// <summary> /// 获得文本内容 /// </summary> /// <param name="data"></param> /// <returns></returns> public static string GetStringContent(IDataObject data) { //先判断数据是否为html格式的数据 if (data.GetDataPresent(DataFormats.StringFormat)) { return data.GetData(DataFormats.StringFormat).ToString(); } else return ""; } } }
截取网页内容的正则
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace WindowsFormsApp25 { class RegexObject { public static Regex reglink = new Regex(@"[a-z]+x3A//[^x27x22x20x0Ax0D]+", RegexOptions.IgnoreCase); public static Regex regcont = new Regex(@"x3Cx21x2Dx2DStartFragmentx2Dx2Dx3E(?<cont>(w|W)*?)x3Cx21x2Dx2DEndFragmentx2Dx2Dx3E", RegexOptions.IgnoreCase); public static Regex reghtml = new Regex(@"<[^>]*>", RegexOptions.IgnoreCase); public static Regex regimg = new Regex(@"<W*img[^>]+srcW*x3D[x27x22W]*(?<img>(w|W)*?)[x27x22x20x3E]", RegexOptions.IgnoreCase); public static Regex regsource = new Regex(@"sourceURLx3A(?<source>[^ ]*)[ ]", RegexOptions.IgnoreCase); public static Regex regsup = new Regex(@"x3Csupx3E", RegexOptions.IgnoreCase); public static Regex regsub = new Regex(@"x3Csubx3E", RegexOptions.IgnoreCase); public static Regex regsupsubend = new Regex(@"x3C/su(b|p)x3E", RegexOptions.IgnoreCase); public static Regex regemail = new Regex(@"([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,5})+", RegexOptions.IgnoreCase); public static Regex regehttp = new Regex(@"((http|https)://)?(www.)?[A-Za-z0-9]+.(com|net|cn|com.cn|com.net|net.cn)", RegexOptions.IgnoreCase); public static Regex regdate = new Regex(@"((?<!d)((d{2,4}(.|年|/|-))((((0?[13578]|1[02])(.|月|/|-))((3[01])|([12][0-9])|(0?[1-9])))|(0?2(.|月|/|-)((2[0-8])|(1[0-9])|(0?[1-9])))|(((0?[469]|11)(.|月|/|-))((30)|([12][0-9])|(0?[1-9]))))|((([0-9]{2})((0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))(.|年|/|-))0?2(.|月|/|-)29))日?(?!d))"); public static Regex regDomain = new Regex(@"(?<mydomain>(?<Protocol>w+)://(?<Domain>[w.x2D]+))/?S*", RegexOptions.IgnoreCase); public static Regex regWWW = new Regex(@"(?<url>http(s)?://([w-]+.)+[w-]+(/[w- ./?%&=]*)?)", RegexOptions.IgnoreCase); //((http|https)://)?(www.)?[A-Za-z0-9]+.(com|net|cn|com.cn|com.net|net.cn) public static Regex regyear = new Regex(@"d{4}"); public static string GetWWW(string txt) { return regWWW.Match(txt).Value; } public static string GetYear(string txt) { return regyear.Match(txt).Value; } public static string Getdomain(string url) { return regDomain.Match(url).Groups["Domain"].Value; } /// <summary> /// 截取日期 /// </summary> /// <param name="txt"></param> /// <returns></returns> public static string GetDate(string txt) { return regdate.Match(txt).Value.ToString(); } /// <summary> /// 只获得如https://www.baidu.com的部分 /// </summary> /// <param name="txt"></param> /// <returns></returns> public static string GetHttp(string txt) { return regehttp.Match(txt).Value.ToString(); } /// <summary> /// 获取当前拖拽的内容(拖拽文本) /// </summary> /// <param name="text"></param> /// <returns></returns> public static string GetText(string text) { string cont = regcont.Match(text).Groups["cont"].Value; string r = reghtml.Replace(cont, "").Trim(); return r; } /// <summary> /// 获得链接字段的网址(将要打开的网页) /// </summary> /// <param name="text"></param> /// <returns></returns> public static string GetLink(string text) { string cont = regcont.Match(text).Groups["cont"].Value; string r = reglink.Match(cont).Value; return r; } /// <summary> /// 获得链接字段的当前网址(现在的网址) /// </summary> /// <param name="text"></param> /// <returns></returns> public static string GetRefPage(string text) { return regsource.Match(text).Groups["source"].Value.Trim(); } /// <summary> /// 获取网址信息(包含version,源网址和当前拖拽链接的网址信息) /// </summary> /// <param name="text"></param> /// <returns></returns> public static string ReplaceSubSup(string text) { text = regsup.Replace(text, "(↑"); text = regsub.Replace(text, "(↓"); text = regsupsubend.Replace(text, ")").Trim(); return text; } } }