Support drag n drop file from task thumbnail

This commit is contained in:
Jaex 2019-05-20 14:51:50 +03:00
parent 7c1ae3488c
commit 7f817082c3
2 changed files with 46 additions and 3 deletions

View file

@ -54,7 +54,6 @@ private void InitializeComponent()
this.pThumbnail.Radius = 5F;
this.pThumbnail.Size = new System.Drawing.Size(256, 256);
this.pThumbnail.TabIndex = 0;
this.pThumbnail.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PbThumbnail_MouseDown);
//
// pbProgress
//
@ -78,7 +77,10 @@ private void InitializeComponent()
this.pbThumbnail.Size = new System.Drawing.Size(246, 246);
this.pbThumbnail.TabIndex = 2;
this.pbThumbnail.TabStop = false;
this.pbThumbnail.QueryContinueDrag += new System.Windows.Forms.QueryContinueDragEventHandler(this.PbThumbnail_QueryContinueDrag);
this.pbThumbnail.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PbThumbnail_MouseDown);
this.pbThumbnail.MouseMove += new System.Windows.Forms.MouseEventHandler(this.PbThumbnail_MouseMove);
this.pbThumbnail.MouseUp += new System.Windows.Forms.MouseEventHandler(this.PbThumbnail_MouseUp);
//
// lblFilename
//

View file

@ -26,6 +26,7 @@
using ShareX.HelpersLib;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace ShareX
@ -91,6 +92,8 @@ public bool ProgressVisible
public Size ThumbnailSize { get; private set; }
public string ThumbnailSourceFilePath { get; private set; }
private Rectangle dragBoxFromMouseDown;
public new event MouseEventHandler MouseDown
{
add
@ -149,7 +152,7 @@ public void ChangeThumbnailSize(Size size)
public void UpdateFilename()
{
Filename = Task.Info.FileName;
Filename = Task.Info?.FileName;
}
public void UpdateThumbnail()
@ -202,7 +205,10 @@ public void UpdateThumbnail()
public void UpdateProgress()
{
Progress = (int)Task.Info.Progress.Percentage;
if (Task.Info != null)
{
Progress = (int)Task.Info.Progress.Percentage;
}
}
public void ClearThumbnail()
@ -220,6 +226,17 @@ public void ClearThumbnail()
private void PbThumbnail_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Size dragSize = SystemInformation.DragSize;
dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);
}
}
private void PbThumbnail_MouseUp(object sender, MouseEventArgs e)
{
dragBoxFromMouseDown = Rectangle.Empty;
if (e.Button == MouseButtons.Left && !string.IsNullOrEmpty(ThumbnailSourceFilePath))
{
pbThumbnail.Enabled = false;
@ -227,5 +244,29 @@ private void PbThumbnail_MouseDown(object sender, MouseEventArgs e)
pbThumbnail.Enabled = true;
}
}
private void PbThumbnail_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y) &&
Task.Info != null && !string.IsNullOrEmpty(Task.Info.FilePath) && File.Exists(Task.Info.FilePath))
{
IDataObject dataObject = new DataObject(DataFormats.FileDrop, new string[] { Task.Info.FilePath });
if (dataObject != null)
{
Program.MainForm.AllowDrop = false;
DoDragDrop(dataObject, DragDropEffects.Copy | DragDropEffects.Move);
}
}
}
private void PbThumbnail_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{
if (e.Action != DragAction.Continue)
{
Program.MainForm.AllowDrop = true;
}
}
}
}