#include <Cocoa/Cocoa.h> #include <cstdio> #include <string> #include <list> static bool select_folder(std::string & foldername) { foldername.clear(); NSWindow * window = [NSWindow alloc]; NSOpenPanel * panel = [NSOpenPanel openPanel]; [panel setCanChooseFiles:NO]; [panel setCanChooseDirectories:YES]; [panel setAllowsMultipleSelection:NO]; // [panel setMessage:@"Import one or more files or directories."]; [panel beginSheetModalForWindow:window completionHandler:^(NSInteger result) { if (result == NSFileHandlingPanelOKButton) { NSURL * selection = panel.URLs[0]; NSString * path = [selection.path stringByResolvingSymlinksInPath]; foldername = [path cStringUsingEncoding : NSUTF8StringEncoding]; } }]; return(!foldername.empty()); } static bool select_files(std::list<std::string> & filename_list) { filename_list.clear(); NSWindow * window = [NSWindow alloc]; NSOpenPanel * panel = [NSOpenPanel openPanel]; [panel setCanChooseFiles:YES]; [panel setCanChooseDirectories:NO]; [panel setAllowsMultipleSelection:YES]; // [panel setMessage:@"Import one or more files or directories."]; [panel beginSheetModalForWindow:window completionHandler:^(NSInteger result) { if (result == NSFileHandlingPanelOKButton) { for (int index = 0; index < panel.URLs.count; ++index) { NSURL * selection = panel.URLs[0]; NSString * path = [selection.path stringByResolvingSymlinksInPath]; std::string pathname = [path cStringUsingEncoding : NSUTF8StringEncoding]; filename_list.push_back(pathname); } } }]; return(!filename_list.empty()); } int main(int argc, char * argv[]) { std::string foldername; select_folder(foldername); printf("folder: <%s>\n", foldername.c_str()); std::list<std::string> filename_list; select_files(filename_list); printf("filelist:\n"); for (std::list<std::string>::const_iterator iter = filename_list.begin(); filename_list.end() != iter; ++iter) { printf(" <%s>\n", iter->c_str()); } return(0); }
使用Object-C来实现“获取打开文件/目录”的窗口的功能,但编译有问题。(保存为.mm)
解决方案
40
能把报错贴上来么?