DragDropFile

 using System;
using System.Windows.Forms;
using Microsoft.Win32;


#region FormLoad
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    try
    {
        using (RegistryKey userKey = Application.UserAppDataRegistry)
        {
            RegistryKey pathKey = userKey.CreateSubKey("FileList");
            foreach (string path in pathKey.GetValueNames()) // 加载历史文件路径,并自动清除不存在的文件路径。
            {
                if (File.Exists(path))
                    fileListMenu.DropDownItems.Add(path).MouseUp += new MouseEventHandler(menuItem_MouseUp);
                else
                    pathKey.DeleteValue(path);
            }
        }
    }
    catch (Exception se)
    {
        MessageBox.Show(this, se.Message, "写字板", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
}
#endregion


#region DragDropFile
private void richText_DragEnter(object sender, DragEventArgs e)
{
    DataObject newData = e.Data as DataObject; // 获取拖放的数据对象。
    if (richText.EnableAutoDragDrop = !newData.ContainsFileDropList())
        return;
    if (GetModified())
        return;
    this.LoadFile(newData.GetFileDropList()[0]);
    using (RegistryKey subKey = Application.UserAppDataRegistry.CreateSubKey("FileList"))
    {
        foreach (string filePath in newData.GetFileDropList())
        {
            subKey.SetValue(filePath, "");
            if (fileListMenu.DropDownItems.Count < subKey.ValueCount) // 防止记录重复文件路径。
                fileListMenu.DropDownItems.Add(filePath).MouseUp += new MouseEventHandler(menuItem_MouseUp);
        }
    }
}
#endregion

#region AccessFile
private void recentMenu_Click(object sender, EventArgs e)
{
    string path = Environment.GetFolderPath(Environment.SpecialFolder.Recent);
    Process.Start("explorer.exe", path).Close();
}

private void menuItem_MouseUp(object sender, MouseEventArgs e)
{
    ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
    string filePath = menuItem.Text;
    switch (e.Button)
    {
        case MouseButtons.Left:
            if (GetModified())
                return;
            if (File.Exists(filePath))
            {
                this.LoadFile(filePath);
                foreach (ToolStripMenuItem item in fileListMenu.DropDownItems)
                {
                    item.Checked = item.Equals(menuItem);
                }
            }
            else
                MessageBox.Show(this, string.Format("指定的文件 {0} 不存在。", filePath), "写字板", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            break;
        case MouseButtons.Right:
            fileMenu.HideDropDown();
            if (MessageBox.Show(this, string.Format("确实要删除 {0} 文件路径吗?", filePath), "确认删除项", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                fileListMenu.DropDownItems.Remove(menuItem);
                using (RegistryKey subKey = Application.UserAppDataRegistry.OpenSubKey("FileList", true)) // true, 读/写访问。
                {
                    if (subKey != null)
                        subKey.DeleteValue(filePath, false); // false, 指定的值不存在时不引发异常。
                }
            }
            break;
    }
}

private void fileClearMenu_Click(object sender, EventArgs e)
{
    if (fileListMenu.HasDropDownItems)
    {
        fileListMenu.DropDownItems.Clear();
        using (RegistryKey subKey = Application.UserAppDataRegistry)
        {
            subKey.DeleteSubKey("FileList", false); // false, 指定的子项不存在时不引发异常。
        }
    }
}
#endregion

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值