C# 在拖动操作期间检测桌面文件夹鼠标松开?

发布于 2024-08-10 07:05:11 字数 861 浏览 1 评论 0原文

<在发布此场景之前,我已经查看了 SO 显示的“相关问题”

  1. 从 WinForm 中的控件启动拖放。

    注意:所有拖放代码均经过验证、仔细测试。我现在可以使用代码 释放鼠标时创建/写入文件到桌面。

  2. 拖动到窗体之外,鼠标移到桌面或桌面上的某些“项目”上。

[编辑回应 Groo 的评论]

在这种情况下,拖放可能会进入桌面(它将成为一个文件:这种情况已经处理);或者,拖放可能会进入另一个应用程序运行实例中相同类型的控件(与发起拖动的控件相同)(这种情况已处理)。我要问的情况是在桌面文件夹上发生放置的情况:Groo 的评论让我想要测试尝试“动态”修改 DataObject 类型......在拖动过程中......我从未尝试过之前:我不知道这是否可能。

[结束编辑]

我可以使用此代码示例 [1*] 中的 api 调用来获取鼠标在桌面上移动的 IntPtr ID:为任何桌面项目返回相同的 IntPtr,例如文件夹、快捷方式、回收站等。

我想要的是:能够检测鼠标何时在桌面上的文件夹上释放:当然,我想做的是获取鼠标移过的文件夹的路径:所以我可以在该文件夹中创建该文件。

谢谢,比尔

[1*]

“FindWindow 作者:Jörg Bausch”

http:// /www.codeproject.com/KB/dialog/FindWindow.aspx?msg=3262771

< I have reviewed the "related questions" shown by SO before posting this >

Scenario :

  1. Drag and drop initiated from a control within a WinForm.

    note : all drag and drop code validated, carefully tested. I can use the code right now
    to create/write a file to the Desktop when the mouse is released.

  2. Drag goes outside the Form, mouse goes up over the Desktop or some "item" on the Desktop.

[edit in response to comment by Groo]

In this case the drop may go onto the desktop (where it will become a file : that case is already handled); or, the drop may go into a control of the same type (as the control from which the drag was initiated) in another application's running instance (that case is taken care of). The case that I am asking about is where the drop occurs on a desktop Folder : Groo's comments have made want to test trying to modify the DataObject type "on the fly" ... during the drag ... which I've never tried before : I have no idea if it's possible.

[end edit ]

I can use the api calls in this code sample [1*] to get an IntPtr ID for the mouse going up over the Desktop : the same IntPtr is returned for any Desktop item, like a Folder, a Shortcut, the Recycle Bin, etc.

What I want is : to be able to detect when the mouse is released over a Folder on the Desktop : of course what I want to do then is to get the path of the folder the mouse went up over : so I can create the file within that folder.

thanks, Bill

[1*]

"FindWindow By Jörg Bausch"

http://www.codeproject.com/KB/dialog/FindWindow.aspx?msg=3262771

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云悠悠 2024-08-17 07:05:11

如果您的文件在开始拖动之前就已存在(或者您之前可以创建它),则只需在传递给 DoDragDrop 方法的 DataObject 实例中指定源文件路径即可。

像这样的东西(您应该已经有适当的处理程序):

public partial class Form1: Form
{
    public Form1()
    {
       InitializeComponent();
       this.MouseDown += Form1_MouseDown;
    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        string[] files = new string[] { @"C:\SomeTestFile.txt" };
        this.DoDragDrop(new DataObject(DataFormats.FileDrop, files),
          DragDropEffects.Copy);
    }
} 

如果您有兴趣在拖动时添加一些奇特的半透明效果,请尝试 此链接。如果您的文件不存在,并且您确实需要在将其拖放到资源管理器后创建它,请检查此链接

您还可以重写 DataObject 来处理删除对象时调用的 GetData 方法:

public class MyDataObject : DataObject
{
    public MyDataObject(string format, object data)
       : base(format, data) { }

    public override object GetData(string format)
    {
        MessageBox.Show("Format: "+format);
        return base.GetData(format);
    }
}

If your file exists before you start to drag (or you are able to create it before), you can simply specify the source file path in the DataObject instance passed to DoDragDrop method.

Something like this (you should already have the appropriate handlers):

public partial class Form1: Form
{
    public Form1()
    {
       InitializeComponent();
       this.MouseDown += Form1_MouseDown;
    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        string[] files = new string[] { @"C:\SomeTestFile.txt" };
        this.DoDragDrop(new DataObject(DataFormats.FileDrop, files),
          DragDropEffects.Copy);
    }
} 

If you are interested in adding some fancy translucent effects while you are dragging, try this link. If your file doesn't exist and you really need to create it after it has been dropped to explorer, check this link.

You can also override DataObject to handle the GetData method which is called when the object is dropped:

public class MyDataObject : DataObject
{
    public MyDataObject(string format, object data)
       : base(format, data) { }

    public override object GetData(string format)
    {
        MessageBox.Show("Format: "+format);
        return base.GetData(format);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文