共享代码:Code::Blocks 对话框 选择文件和选择目录示例
http://srgb.googlecode.com/files/GetFilePATH.zip
完整项目下载,GCC 和 VC 都可以编译
mian.cpp
#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);}/////////////////////////////////////////////////// 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_
// Generated by ResEdit 1.5.9// Copyright (C) 2006-2011// http://www.resedit.net#include <windows.h>#include <commctrl.h>#include <richedit.h>#include "resource.h"//// Dialog resources//LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRALDLG_MAIN DIALOGEX 6, 5, 194, 106STYLE DS_3DLOOK | DS_CENTER | DS_SETFONT | WS_CAPTION | WS_VISIBLE | WS_GROUP | WS_THICKFRAME | WS_SYSMENUCAPTION "Code::Blocks 选择文件和选择目录示例"FONT 9, "宋体", 0, 0, 1{ PUSHBUTTON "测试", IDC_BTN_TEST, 44, 12, 46, 14 PUSHBUTTON "退出", IDC_BTN_QUIT, 99, 12, 46, 14 EDITTEXT IDC_EDIT_FILE, 7, 55, 180, 17, ES_AUTOHSCROLL PUSHBUTTON "选择文件", IDC_BTN_OPEN, 10, 37, 46, 14 PUSHBUTTON "选择目录(类)", IDC_DIRBROWSER, 133, 37, 53, 14 PUSHBUTTON "选择目录", IDC_GETPATH, 72, 37, 51, 14 LTEXT "Code::Blocks 选择文件和选择目录示例", IDC_TEXT, 8, 81, 179, 18, SS_LEFT}#ifndef IDC_STATIC#define IDC_STATIC (-1)#endif#define DLG_MAIN 101#define IDC_BTN_TEST 1001#define IDC_BTN_QUIT 1002#define IDC_GETPATH 1003#define IDC_BTN_OPEN 1004#define IDC_EDIT_FILE 1005#define IDC_DIRBROWSER 1007#define IDC_TEXT 1008