ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c#-Gtk#剪贴板复制/剪切/粘贴文件

2019-12-08 01:07:18  阅读:333  来源: 互联网

标签:clipboard copy-paste gtk-2 c


如何使用Gtk#将文件复制,剪切和粘贴到剪贴板.我所看到的每个示例仅显示文本被放入剪贴板.如果有人举个例子,我将不胜感激.

解决方法:

如果您的应用程序在Gnome / Unity上运行,则可以使用“ x-special / gnome-copied-files”目标类型在剪贴板上移动/复制文件.复制文件后,您应该可以使用文件管理器将其粘贴,反之亦然.下面是执行此操作的窗口类的示例:

using System;
using System.IO;
using Gtk;

public partial class MainWindow : Gtk.Window
{
    private static Gdk.Atom _atom = Gdk.Atom.Intern("CLIPBOARD", false);
    private Gtk.Clipboard _clipBoard = Gtk.Clipboard.Get(_atom);
    private Gtk.FileChooserButton _fileCopy = null;
    private Gtk.FileChooserButton _folder = null;
    private Gtk.RadioButton _radioCopy = null;
    private Gtk.RadioButton _radioMove = null;
    private System.Text.ASCIIEncoding _encoding = new System.Text.ASCIIEncoding();
    private string _action = null;
    private string _source = null;
    private string _destination = null;

    public MainWindow () : base(Gtk.WindowType.Toplevel)
    {
        SetDefaultSize(200, -1);

        var table = new Gtk.Table(5, 5, true);
        var separator = new Gtk.HSeparator();

        var label0 = new Gtk.Label("Select file to copy/move");
        _fileCopy = new Gtk.FileChooserButton("Select A File", Gtk.FileChooserAction.Open);
        _radioCopy = new Gtk.RadioButton("Copy");
        _radioMove = new Gtk.RadioButton(_radioCopy, "Move");
        var copyButton = new Gtk.Button("Copy");

        Add(table);

        table.Attach(label0, 0, 4, 0, 1);
        table.Attach(_fileCopy, 0, 1, 1, 2);
        table.Attach(_radioCopy, 1, 2, 1, 2);
        table.Attach(_radioMove, 2, 3, 1, 2);
        table.Attach(copyButton, 3, 4, 1, 2);
        table.Attach(separator, 0, 4, 2, 3);

        var label1 = new Gtk.Label("Select destination for file(s)");
        _folder = new Gtk.FileChooserButton("Select A File", Gtk.FileChooserAction.SelectFolder);
        var pasteButton = new Gtk.Button("Paste");

        table.Attach(label1, 0, 4, 3, 4);
        table.Attach(_folder, 0, 1, 4, 5);
        table.Attach(pasteButton, 3, 4, 4, 5);

        DeleteEvent += OnDeleteEvent;
        copyButton.Clicked += OnCopyButtonClick; 
        pasteButton.Clicked += OnPasteButtonClick;

        ShowAll();
    }

    private void ClearGet(Gtk.Clipboard clipboard, Gtk.SelectionData selection, uint info)
    {
        var temp = _action + "\n" + _source;
        selection.Set(selection.Target, 8, _encoding.GetBytes(temp)); 
    }

    private void ClearFunc(Gtk.Clipboard clipboard)
    {
        //???
    }

    private void ReceivedFunc(Gtk.Clipboard clipboard, Gtk.SelectionData selection)
    {
        var temp = _encoding.GetString(selection.Data);
        if (temp==null) return;

        var items = temp.Split();
        for (int i=1; i<items.Length; i++)
        {
            var fileFrom = items[i].Substring("file://".Length);
            var fileTo = System.IO.Path.Combine(_destination, System.IO.Path.GetFileName(fileFrom));
            if (items[0]=="copy")
                File.Copy(fileFrom, fileTo);
            else if (items[1]=="cut")
                File.Move(fileFrom, fileTo);
        }
    }

    private void OnCopyButtonClick(object obj, EventArgs args)
    {
        Console.WriteLine(_fileCopy.Uri);

        _source = _fileCopy.Uri;
        _action = _radioMove.Active ? "cut" : "copy";

        var target0 = new TargetEntry("x-special/gnome-copied-files", 0, 0);
        var target1 = new TargetEntry("text/uri-list", 0, 0);

        _clipBoard.SetWithData(new TargetEntry[] {target0, target1}, ClearGet, ClearFunc);
    }

    private void OnPasteButtonClick(object obj, EventArgs args)
    {
        _destination = _folder.Filename;
        _clipBoard.RequestContents(Gdk.Atom.Intern("x-special/gnome-copied-files", false), ReceivedFunc);
    }

    protected void OnDeleteEvent (object sender, DeleteEventArgs a)
    {
        Application.Quit ();
        a.RetVal = true;
    }
}

希望这会有所帮助,问候

标签:clipboard,copy-paste,gtk-2,c
来源: https://codeday.me/bug/20191208/2087850.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有