将文件拖到可可OSX中的其他应用程序中 [英] Dragging files into other applications in cocoa OSX

查看:149
本文介绍了将文件拖到可可OSX中的其他应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Windows应用程序转换为OSX,现在一切正常,除了这个小功能,文件从我的应用程序拖放到其他任何支持拖放的窗口中。接收拖放很容易,问题出在要拖动的数据源。

I'm trying to convert a windows application into OSX, everything is working now, except this small feature, the drag&drop of files from my app into any other window that supports drops. Receiving drops it's easy, the problem is being the source of the data to drag.

我的应用程序只有1个带有1个视图的窗口,我自己绘制了每个控件。因此,我只是简单地扩展了这样的视图,例如 @interface NativeView:NSView< NSDraggingSource,NSPasteboardItemDataProvider>

My application only has 1 window with 1 view, I draw every control myself in there. So I simply extended my view like this @interface NativeView : NSView <NSDraggingSource, NSPasteboardItemDataProvider>.

现在在我看来,到目前为止的代码的其余部分应该可以正常工作,但是我对可可和OSX的了解也不多:

Now the resto of the code I have so far should work in my opinion, but then again I don't know that much about cocoa and OSX:

NSArray *fileList = [NSArray arrayWithObjects:&pathList[0] count:pathList.size()];

NSPasteboard *pboard = [NSPasteboard pasteboardWithName: NSDragPboard];
[pboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:nil];
[pboard setPropertyList:fileList forType:NSFilenamesPboardType];

NSPasteboardItem *pbItem = [NSPasteboardItem new];
[pbItem setDataProvider:view forTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
[pbItem pasteboard:pboard provideDataForType:NSFilenamesPboardType];

NSDraggingItem *dragItem = [[NSDraggingItem alloc] initWithPasteboardWriter:pbItem];
[dragItem setDraggingFrame:NSMakeRect(0, 0, 10, 10)];


[view beginDraggingSessionWithItems:[NSArray arrayWithObjects:dragItem, nil] event:mouseEvent source:view];

fileList是 NSString * 的数组。在您看到视图的地方,它表示接口 NativeView ,它是用这种方式实现的,因为它是用C ++编码的;

The fileList is an array of NSString*. And where you see view it means the interface NativeView, it's implemented this way because this is coded within C++;

当前,当我尝试在 pbItem 粘贴板时,代码会阻塞c $ c>。我的意思是,在那条线之后没有执行任何其他操作。我还尝试一起摆脱了 NSPasteboard 并仅使用 NSPasteboardItem ,但是我得到了一个EXC_BAD_ACCESS运行最后一行: beginDraggingSessionWithItems

Currently, the code blocks when I try to set the pasteboard in the pbItem. I mean nothing else is executed past that line. I also tried to get rid of the NSPasteboardall together and use the NSPasteboardItemonly, but I get a EXC_BAD_ACCESS running the last line: beginDraggingSessionWithItems.

我没有在网上找到任何有关拖动文件的示例,全部是 NSImage ,对于这种类型的拖动我没有用。

I didn't find any examples online about dragging files,all that is is NSImage, and I have no use for that type of drag.

欢迎任何帮助,谢谢。

Any help will be welcome, thanks.

推荐答案

是的,缺少在线文档。

尝试使用以下方法:

auto* dragItems = [[NSMutableArray alloc] init];
for (auto& path : pathList)
{
    auto* fileURL = [NSURL fileURLWithPath: path];
    auto* dragItem = [[NSDraggingItem alloc] initWithPasteboardWriter: fileURL];
    [dragItem setDraggingFrame: NSMakeRect(0, 0, 10, 10)];
    [dragItems addObject: dragItem];
}

[view beginDraggingSessionWithItems: dragItems
                              event: mouseEvent
                             source: view]

这篇关于将文件拖到可可OSX中的其他应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆