我是C++的新手,我無法弄清楚從打開的文件對話框中選擇文件的目錄有多簡單。我試圖使用標準函數,在我的情況下它是GetFullPathName。 這就是我正在試圖做的:從打開文件對話框獲取文件夾
OPENFILENAME ofn; // common dialog box structure
char szFile[260]; // buffer for file name
HANDLE hf; // file handle
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFile = szFile;
// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
//ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
// Display the Open dialog box.
if (GetOpenFileName(&ofn) == TRUE)
hf = CreateFile(ofn.lpstrFile,
GENERIC_READ,
0,
(LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
char buffer[MAX_PATH];
char *buffer2[MAX_PATH];
GetFullPathName(ofn.lpstrFile,
ofn.nMaxFile,
buffer,
buffer2);
//PathRemoveFileSpec(ofn.lpstrFile);
MessageBox(hWnd, buffer, "Tutorial", 0); // and show the path
對於新來C++的人來說,winapi並不是一個很好的地方。例如,'GetFullPathName'需要一個指向C字符串的指針,它具有可以寫入的分配內存。 – chris
您應該可能描述預期的行爲(大概是這種情況下的文件打開對話框,但總是很好指定)以及實際行爲。沒有對話,但其他一切都很好?賽格故障?對話框,但在錯誤的目錄?對話框顯示正常,但不返回答案? –
從用戶請求目錄的推薦對話框是'SHBrowseForFolder'。 –