2016-10-15 154 views
1

我試圖將Windows應用程序轉換爲OSX,現在一切正常,除了這個小功能,從我的應用程序拖放文件到任何其他支持drop的窗口。接收下降很容易,問題是拖動數據的來源。將文件拖入可可OSX中的其他應用程序

我的應用程序只有1個窗口和1個視圖,我在那裏繪製每個控件。所以我簡單地擴展了我的觀點,如@interface NativeView : NSView <NSDraggingSource, NSPasteboardItemDataProvider>

現在代碼的Resto餐廳我到目前爲止應該在我看來工作,但話又說回來,我不知道很多關於可可和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]; 

文件列表是NSString*數組。在你看到view這意味着接口NativeView,它以這種方式實現,因爲這是在C++編碼;

當前,當我嘗試在pbItem中設置pasteboard時,代碼塊會被阻止。我的意思是沒有別的東西會被執行過去。我也試圖擺脫NSPasteboard,並只使用NSPasteboardItem,但我得到一個EXC_BAD_ACCESS運行最後一行:beginDraggingSessionWithItems

我沒有在網上找到任何有關拖動文件的例子,所有這些都是NSImage,我對這種類型的拖動沒有用處。

任何幫助將受到歡迎,謝謝。

回答

0

是的,在線文檔相當缺乏。

嘗試使用以下方法:

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] 
相關問題