How to Drag and Drop Files into an Application

Drag and drop files into program

Self answer

I found tutorials about drag and drop here:

https://github.com/mono/gtk-sharp/blob/master/sample/TestDnd.cs

http://my.safaribooksonline.com/book/programming/mono/0596007922/gtksharp/monoadn-chp-4-sect-8

and I made a tutorial of drag and drop myself.

This is drop file tutorial.

using System;
using System.Collections;
using System.Collections.Generic;

// 1. Drop file tutorial
namespace DropFile
{
public class App: Gtk.Window
{
Gtk.Label lbldrop;

public App (): base ("Drop file")
{
this.SetDefaultSize (250, 200);
this.SetPosition (Gtk.WindowPosition.Center);
this.DeleteEvent += OnTerminated;

this.lbldrop = new Gtk.Label ("Drop here!");
Gtk.Drag.DestSet (this.lbldrop, 0, null, 0);
this.lbldrop.DragDrop
+= new Gtk.DragDropHandler
(OnLabelDragDrop);
this.lbldrop.DragDataReceived
+= new Gtk.DragDataReceivedHandler
(OnLabelDragDataReceived);

Gtk.VBox vbox = new Gtk.VBox ();
vbox.PackStart (this.lbldrop, true, true, 0);

this.Add (vbox);
this.ShowAll ();
}

void OnLabelDragDrop (object sender, Gtk.DragDropArgs args)
{
Gtk.Drag.GetData
((Gtk.Widget)sender, args.Context,
args.Context.Targets[0], args.Time);
}
void OnLabelDragDataReceived
(object sender, Gtk.DragDataReceivedArgs args)
{
if (args.SelectionData.Length > 0
&& args.SelectionData.Format == 8) {

byte[] data = args.SelectionData.Data;
string encoded = System.Text.Encoding.UTF8.GetString (data);

// I don't know what last object is,
// but I tested and noticed that it is not
// a path
List<string> paths
= new List<string> (encoded.Split ('\r', '\n'));
paths.RemoveAll (string.IsNullOrEmpty);
paths.RemoveAt (paths.Count-1);

for (int i=0; i<paths.Count; ++i)
{
Console.WriteLine ("Path {0}: {1}", i, paths[i]);
}
}
}

bool Test (string str)
{
return true;
}

void OnTerminated (object sender, EventArgs args)
{
Gtk.Application.Quit ();
}

public static void Main (string[] args)
{
Gtk.Application.Init ();
new App ();
Gtk.Application.Run ();
}
}
}

And this is drag button tutorial.

using System;

// 2. Drag button tutorial
namespace DragButton
{
public class App: Gtk.Window
{
enum StatusType
{
Checked,
NotChecked
}

Gtk.Button btnDrag;
Gtk.Label lblDrop;
bool isChecked;
Gtk.Statusbar sBar;

public App (): base("Drag And Drop Complete")
{
this.SetDefaultSize (250, 200);
this.SetPosition (Gtk.WindowPosition.Center);
this.DeleteEvent += OnTerminated;

this.isChecked = false;
this.sBar = new Gtk.Statusbar ();
sBar.Push ((uint)StatusType.NotChecked, "Not checked");

this.btnDrag = new Gtk.Button ("Drag here");
Gtk.Drag.SourceSet
(this.btnDrag,
Gdk.ModifierType.Button1Mask
| Gdk.ModifierType.Button3Mask,
null,
Gdk.DragAction.Copy | Gdk.DragAction.Move);
this.btnDrag.DragDataGet
+= new Gtk.DragDataGetHandler
(HandleSourceDragDataGet);
this.btnDrag.DragDataDelete
+= new Gtk.DragDataDeleteHandler
(HandleSourceDragDataDelete);

// set drop label as destination
this.lblDrop = new Gtk.Label ("Drop here");
Gtk.Drag.DestSet (this.lblDrop, 0, null, 0);
this.lblDrop.DragMotion
+= new Gtk.DragMotionHandler
(HandleTargetDragMotion);
this.lblDrop.DragDrop
+= new Gtk.DragDropHandler
(HandleTargetDragDrop);
this.lblDrop.DragDataReceived
+= new Gtk.DragDataReceivedHandler
(this.HandleTargetDragDataReceived);
this.lblDrop.DragDrop
+= new Gtk.DragDropHandler
(this.HandleStatBarDragDrop);

Gtk.MenuBar bar = new Gtk.MenuBar ();
Gtk.MenuItem item = new Gtk.MenuItem ("File");
Gtk.Menu menu = new Gtk.Menu ();
item.Submenu = menu;
bar.Append (item);

// accel key
Gtk.AccelGroup ag = new Gtk.AccelGroup ();
this.AddAccelGroup (ag);
item = new Gtk.MenuItem ("Quit");
item.Activated += OnTerminated;
item.AddAccelerator
("activate", ag, new Gtk.AccelKey
(Gdk.Key.Q, Gdk.ModifierType.ControlMask,
Gtk.AccelFlags.Visible));
menu.Append (item);

Gtk.VBox vbox = new Gtk.VBox();
vbox.PackStart (bar, false, false, 0);

Gtk.HBox hbox = new Gtk.HBox ();
hbox.PackStart (this.btnDrag, true, true, 0);
hbox.PackStart (this.lblDrop, true, true, 0);
vbox.PackStart (hbox, true, true, 0);
vbox.PackStart (sBar, false, false, 0);

this.Add (vbox);
this.ShowAll ();
}

void OnTerminated(object sender, EventArgs args)
{
Gtk.Application.Quit ();
}
void HandleSourceDragDataGet
(object sender, Gtk.DragDataGetArgs args)
{
Console.WriteLine ("Source Drag Data Get");
args.SelectionData.Text = "I'm data!";
}
void HandleSourceDragDataDelete
(object sender, Gtk.DragDataDeleteArgs args)
{
Console.WriteLine ("Source Drag Data Delete");
Console.WriteLine ("Delete the data!");
}
void HandleTargetDragMotion
(object sender, Gtk.DragMotionArgs args)
{
Gdk.Drag.Status (args.Context,
args.Context.SuggestedAction,
args.Time);
args.RetVal = true;
}
void HandleTargetDragDrop
(object sender, Gtk.DragDropArgs args)
{
Console.WriteLine ("drop");
if (args.Context.Targets.Length != 0) {
Gtk.Drag.GetData
((Gtk.Widget)sender, args.Context,
args.Context.Targets[0], args.Time);
args.RetVal = true;
}

args.RetVal = false;
}
void HandleTargetDragDataReceived
(object sender, Gtk.DragDataReceivedArgs args)
{
Console.WriteLine ("received");
if (args.SelectionData.Length >= 0
&& args.SelectionData.Format == 8)
{
Console.WriteLine ("Hi!");

Gtk.Drag.Finish (args.Context, true, false, args.Time);
}
Gtk.Drag.Finish (args.Context, false, false, args.Time);
}
void HandleStatBarDragDrop
(object sender, Gtk.DragDropArgs args)
{
isChecked = !isChecked;
if (isChecked)
sBar.Push ((uint)StatusType.Checked, "Checked");
else
sBar.Pop ((uint)StatusType.Checked);
}

public static void Main (string[] args)
{
Gtk.Application.Init ();
new App ();
Gtk.Application.Run ();
}
}
}

C#: Drag & Drop File on .exe (icon) and get filepath

If I understand correctly, you want to drop a file onto an icon of an exe, not an application itself. If that is so, it can be achieved easily using the parameters passed into the application. If you check your original boilerplate code when you make a console application it has the application entry point:

static void Main(string[] args)

If you drag drop a file onto the icon of the application, the args[0] will hold the name of the file being dropped. If the goal is to drop the file onto an EXE file, there are numerous tutorials on SO about that.



Related Topics



Leave a reply



Submit