增加拖放文件打开

This commit is contained in:
ww-rm
2025-03-20 15:32:31 +08:00
parent 66cf0efcb9
commit 9609a2fd5d
2 changed files with 101 additions and 62 deletions

View File

@@ -44,15 +44,15 @@
toolStripMenuItem_MoveTop = new ToolStripMenuItem(); toolStripMenuItem_MoveTop = new ToolStripMenuItem();
toolStripMenuItem_MoveBottom = new ToolStripMenuItem(); toolStripMenuItem_MoveBottom = new ToolStripMenuItem();
toolStripSeparator3 = new ToolStripSeparator(); toolStripSeparator3 = new ToolStripSeparator();
toolStripMenuItem_SelectAll = new ToolStripMenuItem();
toolStripMenuItem_CopyPreview = new ToolStripMenuItem(); toolStripMenuItem_CopyPreview = new ToolStripMenuItem();
toolStripSeparator4 = new ToolStripSeparator();
toolStripMenuItem_ChangeView = new ToolStripMenuItem(); toolStripMenuItem_ChangeView = new ToolStripMenuItem();
toolStripMenuItem_LargeIconView = new ToolStripMenuItem(); toolStripMenuItem_LargeIconView = new ToolStripMenuItem();
toolStripMenuItem_SmallIconView = new ToolStripMenuItem(); toolStripMenuItem_SmallIconView = new ToolStripMenuItem();
toolStripMenuItem_DetailsView = new ToolStripMenuItem(); toolStripMenuItem_DetailsView = new ToolStripMenuItem();
imageList_LargeIcon = new ImageList(components); imageList_LargeIcon = new ImageList(components);
imageList_SmallIcon = new ImageList(components); imageList_SmallIcon = new ImageList(components);
toolStripSeparator4 = new ToolStripSeparator();
toolStripMenuItem_SelectAll = new ToolStripMenuItem();
contextMenuStrip.SuspendLayout(); contextMenuStrip.SuspendLayout();
SuspendLayout(); SuspendLayout();
// //
@@ -76,6 +76,7 @@
listView.ItemDrag += listView_ItemDrag; listView.ItemDrag += listView_ItemDrag;
listView.SelectedIndexChanged += listView_SelectedIndexChanged; listView.SelectedIndexChanged += listView_SelectedIndexChanged;
listView.DragDrop += listView_DragDrop; listView.DragDrop += listView_DragDrop;
listView.DragEnter += listView_DragEnter;
listView.DragOver += listView_DragOver; listView.DragOver += listView_DragOver;
// //
// columnHeader_Name // columnHeader_Name
@@ -88,7 +89,7 @@
contextMenuStrip.ImageScalingSize = new Size(24, 24); contextMenuStrip.ImageScalingSize = new Size(24, 24);
contextMenuStrip.Items.AddRange(new ToolStripItem[] { toolStripMenuItem_Add, toolStripMenuItem_Insert, toolStripMenuItem_Remove, toolStripSeparator1, toolStripMenuItem_BatchAdd, toolStripMenuItem_RemoveAll, toolStripSeparator2, toolStripMenuItem_MoveUp, toolStripMenuItem_MoveDown, toolStripMenuItem_MoveTop, toolStripMenuItem_MoveBottom, toolStripSeparator3, toolStripMenuItem_SelectAll, toolStripMenuItem_CopyPreview, toolStripSeparator4, toolStripMenuItem_ChangeView }); contextMenuStrip.Items.AddRange(new ToolStripItem[] { toolStripMenuItem_Add, toolStripMenuItem_Insert, toolStripMenuItem_Remove, toolStripSeparator1, toolStripMenuItem_BatchAdd, toolStripMenuItem_RemoveAll, toolStripSeparator2, toolStripMenuItem_MoveUp, toolStripMenuItem_MoveDown, toolStripMenuItem_MoveTop, toolStripMenuItem_MoveBottom, toolStripSeparator3, toolStripMenuItem_SelectAll, toolStripMenuItem_CopyPreview, toolStripSeparator4, toolStripMenuItem_ChangeView });
contextMenuStrip.Name = "contextMenuStrip"; contextMenuStrip.Name = "contextMenuStrip";
contextMenuStrip.Size = new Size(329, 421); contextMenuStrip.Size = new Size(329, 388);
contextMenuStrip.Closed += contextMenuStrip_Closed; contextMenuStrip.Closed += contextMenuStrip_Closed;
contextMenuStrip.Opening += contextMenuStrip_Opening; contextMenuStrip.Opening += contextMenuStrip_Opening;
// //
@@ -174,6 +175,14 @@
toolStripSeparator3.Name = "toolStripSeparator3"; toolStripSeparator3.Name = "toolStripSeparator3";
toolStripSeparator3.Size = new Size(325, 6); toolStripSeparator3.Size = new Size(325, 6);
// //
// toolStripMenuItem_SelectAll
//
toolStripMenuItem_SelectAll.Name = "toolStripMenuItem_SelectAll";
toolStripMenuItem_SelectAll.ShortcutKeys = Keys.Control | Keys.A;
toolStripMenuItem_SelectAll.Size = new Size(328, 30);
toolStripMenuItem_SelectAll.Text = "全选";
toolStripMenuItem_SelectAll.Click += toolStripMenuItem_SelectAll_Click;
//
// toolStripMenuItem_CopyPreview // toolStripMenuItem_CopyPreview
// //
toolStripMenuItem_CopyPreview.Name = "toolStripMenuItem_CopyPreview"; toolStripMenuItem_CopyPreview.Name = "toolStripMenuItem_CopyPreview";
@@ -182,6 +191,11 @@
toolStripMenuItem_CopyPreview.Text = "复制预览图 (256x256)"; toolStripMenuItem_CopyPreview.Text = "复制预览图 (256x256)";
toolStripMenuItem_CopyPreview.Click += toolStripMenuItem_CopyPreview_Click; toolStripMenuItem_CopyPreview.Click += toolStripMenuItem_CopyPreview_Click;
// //
// toolStripSeparator4
//
toolStripSeparator4.Name = "toolStripSeparator4";
toolStripSeparator4.Size = new Size(325, 6);
//
// toolStripMenuItem_ChangeView // toolStripMenuItem_ChangeView
// //
toolStripMenuItem_ChangeView.DropDownItems.AddRange(new ToolStripItem[] { toolStripMenuItem_LargeIconView, toolStripMenuItem_SmallIconView, toolStripMenuItem_DetailsView }); toolStripMenuItem_ChangeView.DropDownItems.AddRange(new ToolStripItem[] { toolStripMenuItem_LargeIconView, toolStripMenuItem_SmallIconView, toolStripMenuItem_DetailsView });
@@ -193,7 +207,7 @@
// //
toolStripMenuItem_LargeIconView.Name = "toolStripMenuItem_LargeIconView"; toolStripMenuItem_LargeIconView.Name = "toolStripMenuItem_LargeIconView";
toolStripMenuItem_LargeIconView.ShortcutKeys = Keys.Alt | Keys.D1; toolStripMenuItem_LargeIconView.ShortcutKeys = Keys.Alt | Keys.D1;
toolStripMenuItem_LargeIconView.Size = new Size(270, 34); toolStripMenuItem_LargeIconView.Size = new Size(223, 34);
toolStripMenuItem_LargeIconView.Text = "大图标"; toolStripMenuItem_LargeIconView.Text = "大图标";
toolStripMenuItem_LargeIconView.Click += toolStripMenuItem_LargeIconView_Click; toolStripMenuItem_LargeIconView.Click += toolStripMenuItem_LargeIconView_Click;
// //
@@ -201,7 +215,7 @@
// //
toolStripMenuItem_SmallIconView.Name = "toolStripMenuItem_SmallIconView"; toolStripMenuItem_SmallIconView.Name = "toolStripMenuItem_SmallIconView";
toolStripMenuItem_SmallIconView.ShortcutKeys = Keys.Alt | Keys.D2; toolStripMenuItem_SmallIconView.ShortcutKeys = Keys.Alt | Keys.D2;
toolStripMenuItem_SmallIconView.Size = new Size(270, 34); toolStripMenuItem_SmallIconView.Size = new Size(223, 34);
toolStripMenuItem_SmallIconView.Text = "小图标"; toolStripMenuItem_SmallIconView.Text = "小图标";
toolStripMenuItem_SmallIconView.Click += toolStripMenuItem_SmallIconView_Click; toolStripMenuItem_SmallIconView.Click += toolStripMenuItem_SmallIconView_Click;
// //
@@ -209,7 +223,7 @@
// //
toolStripMenuItem_DetailsView.Name = "toolStripMenuItem_DetailsView"; toolStripMenuItem_DetailsView.Name = "toolStripMenuItem_DetailsView";
toolStripMenuItem_DetailsView.ShortcutKeys = Keys.Alt | Keys.D3; toolStripMenuItem_DetailsView.ShortcutKeys = Keys.Alt | Keys.D3;
toolStripMenuItem_DetailsView.Size = new Size(270, 34); toolStripMenuItem_DetailsView.Size = new Size(223, 34);
toolStripMenuItem_DetailsView.Text = "列表"; toolStripMenuItem_DetailsView.Text = "列表";
toolStripMenuItem_DetailsView.Click += toolStripMenuItem_DetailsView_Click; toolStripMenuItem_DetailsView.Click += toolStripMenuItem_DetailsView_Click;
// //
@@ -225,19 +239,6 @@
imageList_SmallIcon.ImageSize = new Size(48, 48); imageList_SmallIcon.ImageSize = new Size(48, 48);
imageList_SmallIcon.TransparentColor = Color.Transparent; imageList_SmallIcon.TransparentColor = Color.Transparent;
// //
// toolStripSeparator4
//
toolStripSeparator4.Name = "toolStripSeparator4";
toolStripSeparator4.Size = new Size(325, 6);
//
// toolStripMenuItem_SelectAll
//
toolStripMenuItem_SelectAll.Name = "toolStripMenuItem_SelectAll";
toolStripMenuItem_SelectAll.ShortcutKeys = Keys.Control | Keys.A;
toolStripMenuItem_SelectAll.Size = new Size(328, 30);
toolStripMenuItem_SelectAll.Text = "全选";
toolStripMenuItem_SelectAll.Click += toolStripMenuItem_SelectAll_Click;
//
// SpineListView // SpineListView
// //
AutoScaleDimensions = new SizeF(11F, 24F); AutoScaleDimensions = new SizeF(11F, 24F);

View File

@@ -60,7 +60,7 @@ namespace SpineViewer.Controls
var progressDialog = new Dialogs.ProgressDialog(); var progressDialog = new Dialogs.ProgressDialog();
progressDialog.DoWork += BatchAdd_Work; progressDialog.DoWork += BatchAdd_Work;
progressDialog.RunWorkerAsync(openDialog); progressDialog.RunWorkerAsync(openDialog.Result);
progressDialog.ShowDialog(); progressDialog.ShowDialog();
} }
@@ -113,11 +113,20 @@ namespace SpineViewer.Controls
DoDragDrop(e.Item, DragDropEffects.Move); DoDragDrop(e.Item, DragDropEffects.Move);
} }
private void listView_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Serializable))
e.Effect = DragDropEffects.Move;
else if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void listView_DragOver(object sender, DragEventArgs e) private void listView_DragOver(object sender, DragEventArgs e)
{ {
// 检查拖放目标是否有效 if (e.Data.GetDataPresent(DataFormats.Serializable))
e.Effect = DragDropEffects.Move; {
// 获取鼠标位置并确定目标索引 // 获取鼠标位置并确定目标索引
var point = listView.PointToClient(new(e.X, e.Y)); var point = listView.PointToClient(new(e.X, e.Y));
var targetItem = listView.GetItemAt(point.X, point.Y); var targetItem = listView.GetItemAt(point.X, point.Y);
@@ -132,8 +141,11 @@ namespace SpineViewer.Controls
targetItem.BackColor = Color.LightGray; targetItem.BackColor = Color.LightGray;
} }
} }
}
private void listView_DragDrop(object sender, DragEventArgs e) private void listView_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Serializable))
{ {
// 获取拖放源项和目标项 // 获取拖放源项和目标项
var draggedItem = (ListViewItem)e.Data.GetData(typeof(ListViewItem)); var draggedItem = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
@@ -171,6 +183,27 @@ namespace SpineViewer.Controls
item.BackColor = listView.BackColor; item.BackColor = listView.BackColor;
} }
} }
else if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
var validPaths = ((string[])e.Data.GetData(DataFormats.FileDrop)).Where(
path => File.Exists(path) &&
(Path.GetExtension(path).Equals(".skel", StringComparison.OrdinalIgnoreCase) ||
Path.GetExtension(path).Equals(".json", StringComparison.OrdinalIgnoreCase))
).ToArray();
if (validPaths.Length > 1)
{
var progressDialog = new Dialogs.ProgressDialog();
progressDialog.DoWork += BatchAdd_Work;
progressDialog.RunWorkerAsync(new Dialogs.BatchOpenSpineDialogResult(Spine.Version.Auto, validPaths));
progressDialog.ShowDialog();
}
else if (validPaths.Length > 0)
{
Insert(new Dialogs.OpenSpineDialogResult(Spine.Version.Auto, validPaths[0]));
}
}
}
private void contextMenuStrip_Opening(object sender, CancelEventArgs e) private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
{ {
@@ -386,9 +419,14 @@ namespace SpineViewer.Controls
if (dialog.ShowDialog() != DialogResult.OK) if (dialog.ShowDialog() != DialogResult.OK)
return; return;
Insert(dialog.Result, index);
}
private void Insert(Dialogs.OpenSpineDialogResult result, int index = -1)
{
try try
{ {
var spine = Spine.Spine.New(dialog.Version, dialog.SkelPath, dialog.AtlasPath); var spine = Spine.Spine.New(result.Version, result.SkelPath, result.AtlasPath);
// 如果索引无效则在末尾添加 // 如果索引无效则在末尾添加
if (index < 0 || index > listView.Items.Count) if (index < 0 || index > listView.Items.Count)
@@ -410,7 +448,7 @@ namespace SpineViewer.Controls
catch (Exception ex) catch (Exception ex)
{ {
Program.Logger.Error(ex.ToString()); Program.Logger.Error(ex.ToString());
Program.Logger.Error("Failed to load {} {}", dialog.SkelPath, dialog.AtlasPath); Program.Logger.Error("Failed to load {} {}", result.SkelPath, result.AtlasPath);
MessageBox.Show(ex.ToString(), "骨骼加载失败", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show(ex.ToString(), "骨骼加载失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
@@ -420,7 +458,7 @@ namespace SpineViewer.Controls
private void BatchAdd_Work(object? sender, DoWorkEventArgs e) private void BatchAdd_Work(object? sender, DoWorkEventArgs e)
{ {
var worker = sender as BackgroundWorker; var worker = sender as BackgroundWorker;
var arguments = e.Argument as Dialogs.BatchOpenSpineDialog; var arguments = e.Argument as Dialogs.BatchOpenSpineDialogResult;
var skelPaths = arguments.SkelPaths; var skelPaths = arguments.SkelPaths;
var version = arguments.Version; var version = arguments.Version;