C# 将文件拖放到程序中
我正在使用MonoDevelop工具C# 将文件拖放到程序中,c#,drag-and-drop,gtk#,C#,Drag And Drop,Gtk#,我正在使用MonoDevelop工具 我想用GTK制作如下程序 1.用户将文件拖动到程序的listView、tableView或其他任何位置 2.拖动文件的列表打印在程序窗口上 但我对GTK几乎是个新手,我知道如何拖放, 所以我搜索了有关它的信息,找到了如下链接 这是我试图使程序链接成为建议的代码 (这个链接解释C++中的拖放,而我必须让它变成C语言) 使用Gtk; 使用制度; 使用系统集合; 使用System.Collections.Generic namespace DragAndD
我想用GTK制作如下程序
1.用户将文件拖动到程序的listView、tableView或其他任何位置
2.拖动文件的列表打印在程序窗口上
但我对GTK几乎是个新手,我知道如何拖放,
所以我搜索了有关它的信息,找到了如下链接
这是我试图使程序链接成为建议的代码
(这个链接解释C++中的拖放,而我必须让它变成C语言) 使用Gtk; 使用制度; 使用系统集合; 使用System.Collections.Generic
namespace DragAndDrop
{
public class SharpApp: Window
{
Button btnDrag;
Label lblDrop;
HBox hbox;
public SharpApp (): base("Title")
{
btnDrag = new Button("Drag Here");
lblDrop = new Label("Drop Here");
hbox = new HBox();
SetDefaultSize(250,200);
SetPosition (Gtk.WindowPosition.Center);
DeleteEvent += (o, args) => Application.Quit ();
// Targets
List<TargetEntry> list
= new List<TargetEntry> ();
list.Add (new TargetEntry
("STRING", TargetFlags.Widget, 0));
list.Add (new TargetEntry
("text/plain", TargetFlags.Widget, 0));
// Drag site -----
// Make btnDrag a DnD drag source:
TargetEntry[] entries = list.ToArray();
TargetEntry[] se = new TargetEntry[] {entries[0]};
Drag.SourceSet (btnDrag, Gdk.ModifierType.ModifierMask,
se, Gdk.DragAction.Copy);
// Connect signals
btnDrag.DragDataGet += delegate
(object o, DragDataGetArgs args) {
Console.WriteLine ("Test");
OnDragDataGet(args.Context,
args.SelectionData,
args.Info,
args.Time);
};
hbox.PackStart (btnDrag);
// Drop site -----
// Make lblDrop a DnD drop destination:
TargetEntry[] de = new TargetEntry[] {entries[1]};
Drag.DestSet (lblDrop, DestDefaults.Drop,
de, Gdk.DragAction.Copy);
// Connect signals
lblDrop.DragDataReceived += delegate
(object o, DragDataReceivedArgs args) {
Console.WriteLine ("Test");
OnDragDataReceived(args.Context,
args.X,
args.Y,
args.SelectionData,
args.Info,
args.Time);
};
// hbox
hbox.PackStart (lblDrop);
Add (hbox);
ShowAll ();
}
// event handlers
protected override void OnDragDataGet
(Gdk.DragContext context, SelectionData sdata,
uint info, uint time)
{
Console.WriteLine ("OnDragDataGet");
string tmp = "I'm data!";
byte[] b = new byte[tmp.Length];
for (int i=0; i<tmp.Length; ++i)
b[i] = (byte)tmp[i];
sdata.Set(sdata.Target, 8, b, tmp.Length);
}
protected override void OnDragDataReceived
(Gdk.DragContext context, int x, int y,
SelectionData sdata, uint info, uint time)
{
Console.WriteLine ("OnDragDataReceived");
int length = sdata.Length;
if ((length>=0) && (sdata.Format==8))
{
Console.WriteLine ("Received \"{0}\" in label",
sdata.Data.ToString());
}
Drag.Finish (context, false, false, time);
}
// main
public static void Main (string[] args)
{
Application.Init ();
new SharpApp();
Application.Run ();
}
}
}
名称空间拖放
{
公共类应用程序:窗口
{
按钮btnDrag;
标签lblDrop;
HBox-HBox;
公共SharpApp():基础(“标题”)
{
btnDrag=新按钮(“拖动到此处”);
lblDrop=新标签(“放在这里”);
hbox=新的hbox();
设置默认大小(250200);
设置位置(Gtk.WindowPosition.Center);
DeleteEvent+=(o,args)=>Application.Quit();
//目标
列表
=新列表();
列表。添加(新的TargetEntry)
(“字符串”,TargetFlags.Widget,0));
列表。添加(新的TargetEntry)
(“text/plain”,TargetFlags.Widget,0));
//拖动站点-----
//使btnDrag成为DnD拖动源:
TargetEntry[]条目=list.ToArray();
TargetEntry[]se=新的TargetEntry[]{entries[0]};
Drag.SourceSet(btnDrag、Gdk.ModifierType.ModifierMask、,
se、Gdk、DragAction、Copy);
//连接信号
btnDrag.DragDataGet+=委托
(对象o,拖动标记args){
Console.WriteLine(“测试”);
OnDragDataGet(args.Context,
args.SelectionData,
args.Info,
参数(时间);
};
hbox.PackStart(btnDrag);
//降落点-----
//将lblDrop设为DnD投递目的地:
TargetEntry[]de=新的TargetEntry[]{entries[1]};
Drag.DestSet(lblDrop、DestDefaults.Drop、,
de、Gdk、DragAction、Copy);
//连接信号
lblDrop.DragDataReceived+=委托
(对象o,DragDataReceivedArgs参数){
Console.WriteLine(“测试”);
OnDragDataReceived(args.Context,
args.X,
args.Y,
args.SelectionData,
args.Info,
参数(时间);
};
//hbox
hbox.PackStart(lblDrop);
添加(hbox);
ShowAll();
}
//事件处理程序
受保护的覆盖无效OnDragDataGet
(Gdk.DragContext上下文,SelectionData sdata,
单位信息,单位时间)
{
Console.WriteLine(“OnDragDataGet”);
string tmp=“我是数据!”;
字节[]b=新字节[tmp.Length];
对于(inti=0;i=0)和&(sdata.Format==8))
{
Console.WriteLine(“收到标签中的\“{0}\”,
sdata.Data.ToString());
}
Drag.Finish(上下文、false、false、时间);
}
//主要
公共静态void Main(字符串[]args)
{
Application.Init();
新的SharpApp();
Application.Run();
}
}
}
但是结果告诉我我可能错了。。我以为按钮会被移动当我拖动它时,按钮一点也不动。有人能解决我的问题吗?自我回答
我在这里找到了有关拖放的教程:
我自己做了一个拖放的教程
这是放置文件教程
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 ();
}
}
}
自我回答我在这里找到了有关拖放的教程:
我自己做了一个拖放的教程
这是放置文件教程
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 ();
}
}
}
以下示例将放置在标签上的文件的完整路径打印到标签(称为lblPath)中:
public partial class MainWindow: Gtk.Window
{
/** Add a label to your project called lblPath **/
public MainWindow () : base (Gtk.WindowType.Toplevel)
{
Build ();
//media type we'll accept
Gtk.TargetEntry [ ] target_table =
new TargetEntry [ ] {
new TargetEntry ("text/uri-list", 0, 0),
new TargetEntry ("application/x-monkey", 0, 1),
};
Gtk.Drag.DestSet (lblPath, DestDefaults.All, target_table, Gdk.DragAction.Copy);
lblPath.DragDataReceived += new Gtk.DragDataReceivedHandler(OnLabelDragDataReceived);
}
void OnLabelDragDataReceived (object sender, Gtk.DragDataReceivedArgs args)
{
if (args.SelectionData.Length > 0) {
byte[] data = args.SelectionData.Data;
string files = System.Text.Encoding.UTF8.GetString (data);
string file;
string[] fileArray = files.Split ('\n');
for (int i = 0; i < fileArray.Length - 1; i++) { //the last element is empty
file = fileArray [i].Replace ("\r", "");
if (file.StartsWith("file://"))
file = file.Substring(7); //for Windows should be 8
lblPath.Text += file + "\n";
}
}
}
}
公共部分类主窗口:Gtk.Window
{
/**在项目中添加一个名为lblPath的标签**/
公共主窗口():基本(Gtk.WindowType.Toplevel)
{
构建();
//我们接受媒体类型
Gtk.TargetEntry[]目标表格=
新目标中心[]{
新TargetEntry(“文本/uri列表”,0,0),
新TargetEntry(“应用程序/x-monkey”,0,1),
};
Gtk.Drag.DestSet(lblPath,DestDefaults.All,target_table,Gdk.DragAction.Copy);
lblPath.DragDataReceived+=新的Gtk.DragDataReceivedHandler(仅接收到标记的RagDataReceived);
}
void OnLabelDragDataReceived(对象发送方,Gtk.DragDataReceivedArgs args)
{
如果(args.SelectionData.Length>0){
字节[]数据=args.SelectionData.data;
字符串文件=System.Text.Encoding.UTF8.GetString(数据);
字符串文件;
字符串[]fileArray=files.Split('\n');
对于(inti=0;i
以下示例将放置在标签上的文件的完整路径打印到标签(称为lblPath)中:
public partial class MainWindow: Gtk.Window
{
/** Add a label to your project called lblPath **/
public MainWindow () : base (Gtk.WindowType.Toplevel)
{
Build ();
//media type we'll accept
Gtk.TargetEntry [ ] target_table =
new TargetEntry [ ] {
new TargetEntry ("text/uri-list", 0, 0),
new TargetEntry ("application/x-monkey", 0, 1),
};
Gtk.Drag.DestSet (lblPath, DestDefaults.All, target_table, Gdk.DragAction.Copy);
lblPath.DragDataReceived += new Gtk.DragDataReceivedHandler(OnLabelDragDataReceived);
}
void OnLabelDragDataReceived (object sender, Gtk.DragDataReceivedArgs args)
{
if (args.SelectionData.Length > 0) {
byte[] data = args.SelectionData.Data;
string files = System.Text.Encoding.UTF8.GetString (data);
string file;
string[] fileArray = files.Split ('\n');
for (int i = 0; i < fileArray.Length - 1; i++) { //the last element is empty
file = fileArray [i].Replace ("\r", "");
if (file.StartsWith("file://"))
file = file.Substring(7); //for Windows should be 8
lblPath.Text += file + "\n";
}
}
}
}
公共部分类主窗口:Gtk.Window
{
/**在项目中添加一个名为lblPath的标签**/
公共主窗口():基本(Gtk.WindowType.Toplevel)
{
构建();
//媒体类型我们将