本帖最后由 Cool_Breeze 于 2021-7-8 09:34 编辑
[C#] 纯文本查看 复制代码 using System;
using System.Windows.Forms;
using System.Drawing;
using System.Text;
using System.IO;
class Program
{
[STAThread]
static void Main()
{
Application.Run(new MyForm());
}
}
class MyForm : Form
{
public MyForm()
{
this.Size = new Size(600, 600);
this.Text = "AllowDrop";
this.StartPosition = FormStartPosition.CenterScreen;
this.AllowDrop = true;
// this.TransparencyKey = Color.Green;
TextBox inputTbox = new TextBox();
inputTbox.Size = new Size(300, 500);
inputTbox.Location = new Point(5, 10);
inputTbox.AllowDrop = true;
inputTbox.Multiline = true;
// 在将对象拖入控件的边界时发生。
inputTbox.DragEnter += (o, e) =>
{
// 指定拖放效果
e.Effect = DragDropEffects.All;
// 移除这些后缀文件
string[] removeFileExtensions = {".lnk", ".url"};
// 解决 66CCFF 报透明色 加上 ff
unchecked{
inputTbox.BackColor = Color.FromArgb((int)0xff66CCFF);
}
var str = new StringBuilder();
string extension = "";
// 文件类型 DataFormats.FileDrop
foreach (var n in (string[])e.Data.GetData(DataFormats.FileDrop))
{
extension = Path.GetExtension(n);
// 判断文件后缀
if (Array.Exists(removeFileExtensions,
ext => { return (ext == extension) ? true:false;}
)
) continue;
str.Append(n);
str.Append("\r\n");
}
inputTbox.Text = str.ToString();
};
// 将对象拖出控件的边界时发生。
inputTbox.DragLeave += (o, e) =>
{
unchecked{
inputTbox.BackColor = Color.FromArgb((int)0xff99CCFF);
}
};
this.Controls.Add(inputTbox);
}
}
|