共享代码:Code:Blocks 对话框 选择文件和选择目录示例,该怎么处理
共享代码:Code::Blocks 对话框 选择文件和选择目录示例
http://srgb.googlecode.com/files/GetFilePATH.zip
完整项目下载,GCC 和 VC 都可以编译
mian.cpp
http://srgb.googlecode.com/files/GetFilePATH.zip
完整项目下载,GCC 和 VC 都可以编译
mian.cpp
- C/C++ code
#include <windows.h> #include "resource.h" #include "DirDialog.h" int GetFilePath(HWND hWnd, char *szFile); int GetPath(HWND hWnd, char *pBuffer); HINSTANCE hInst; BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_INITDIALOG: /* * TODO: Add code to initialize the dialog. */ return TRUE; case WM_CLOSE: EndDialog(hwndDlg, 0); return TRUE; case WM_COMMAND: switch (LOWORD(wParam)) { /* * TODO: Add more control ID's, when needed. */ case IDC_BTN_QUIT: EndDialog(hwndDlg, 0); return TRUE; case IDC_BTN_TEST: MessageBox(hwndDlg, "You clicked \"Test\" button!", "Information", MB_ICONINFORMATION); return TRUE; case IDC_BTN_OPEN: { char szFile[260]; // buffer for file name if (! GetFilePath(hwndDlg, szFile)) return FALSE; MessageBox(NULL, szFile, "选择文件", 0); SetWindowText(GetDlgItem(hwndDlg, IDC_EDIT_FILE), szFile); SetWindowText(GetDlgItem(hwndDlg, IDC_TEXT), szFile); } return TRUE; case IDC_GETPATH: { char szDirPath[260]; // buffer for Dir Path if (! GetPath(hwndDlg, szDirPath)) return FALSE; SetWindowText(GetDlgItem(hwndDlg, IDC_EDIT_FILE), szDirPath); SetWindowText(GetDlgItem(hwndDlg, IDC_TEXT), szDirPath); } return TRUE; case IDC_DIRBROWSER: { CDirDialog dir; if (dir.DoBrowse(hwndDlg)) { SetWindowText(GetDlgItem(hwndDlg, IDC_EDIT_FILE), dir.GetPath()); MessageBox(NULL, dir.GetPath(), "选择文件夹", NULL); } } return TRUE; } } return FALSE; } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { hInst = hInstance; // The user interface is a modal dialog box return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc); } // 选择一个目录 int GetPath(HWND hWnd, char *pBuffer) { BROWSEINFO bf; LPITEMIDLIST lpitem; memset(&bf, 0, sizeof(BROWSEINFO)); bf.hwndOwner = hWnd; bf.lpszTitle = "选择路径 "; bf.ulFlags = BIF_RETURNONLYFSDIRS; //属性你可自己选择 lpitem = SHBrowseForFolder(&bf); if (lpitem == NULL) //如果没有选择路径则返回 0 return 0; //如果选择了路径则复制路径,返回路径长度 SHGetPathFromIDList(lpitem, pBuffer); return lstrlen(pBuffer); } // 选择一个文件 int GetFilePath(HWND hWnd, char *szFile) { OPENFILENAME ofn; // common dialog box structure // 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 = 260; // 本来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. GetOpenFileName(&ofn); return lstrlen(szFile); }
- C/C++ code
///////////////////////////////////////////////// // DirDialog.h文件 #ifndef __DIRDIALOG_H_ #define __DIRDIALOG_H_ #include <shlobj.h> class CDirDialog { public: CDirDialog(); // 显示对话框 BOOL DoBrowse(HWND hWndParent, LPCTSTR pszTitle = NULL); // 取得用户选择的目录名称 LPCTSTR GetPath() { return m_szPath; } protected: BROWSEINFOA m_bi; // 用来接受用户选择目录的缓冲区 char m_szDisplay[MAX_PATH]; char m_szPath[MAX_PATH]; }; CDirDialog::CDirDialog() { memset(&m_bi, 0, sizeof(m_bi)); m_bi.hwndOwner = NULL; m_bi.pidlRoot = NULL; m_bi.pszDisplayName = m_szDisplay; m_bi.lpszTitle = NULL; m_bi.ulFlags = BIF_RETURNONLYFSDIRS; m_szPath[0] = '\0'; } BOOL CDirDialog::DoBrowse(HWND hWndParent, LPCTSTR pszTitle) { if(pszTitle == NULL) m_bi.lpszTitle = "选择目标文件夹"; else m_bi.lpszTitle = pszTitle; m_bi.hwndOwner = hWndParent; LPITEMIDLIST pItem = ::SHBrowseForFolder(&m_bi); if(pItem != 0) { ::SHGetPathFromIDList(pItem, m_szPath); return TRUE; } return FALSE; } #endif //__DIRDIALOG_H_