Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /www/wwwroot/fawdlstty.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383
一个比较有年代的东西,适合收藏。效果如下:
调用稍微麻烦了点,已将其封装为库,首先引用hSelect.hpp,代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | #pragma once #ifndef __HSELECT_HPP__ #define __HSELECT_HPP__ #ifndef _WINDOWS_ #include <windows.h> #endif #include <tchar.h> #include <ShlObj.h> class selFile { public: selFile (HWND hWnd) { ofn.lStructSize = sizeof (OPENFILENAME); ofn.hwndOwner = hWnd; ofn.hInstance = NULL; ofn.lpstrFilter = _T("Portable Network Graphics(*.png)\0*.png\0") _T ("Joint Photographic experts Group(*.jpg)\0*.jpg\0") _T ("Graphics Interchange Format(*.gif)\0*.gif\0") _T ("Bitmap Files(*.bmp)\0*.bmp\0") _T ("All Files\0*.*\0\0"); ofn.lpstrCustomFilter = NULL; ofn.nMaxCustFilter = 0; ofn.nFilterIndex = 0; ofn.lpstrFile = NULL; ofn.nMaxFile = MAX_PATH; ofn.lpstrFileTitle = NULL; ofn.nMaxFileTitle = MAX_PATH; ofn.lpstrInitialDir = NULL; ofn.lpstrTitle = NULL; ofn.Flags = 0; ofn.nFileOffset = 0; ofn.nFileExtension = 0; ofn.lpstrDefExt = TEXT ("jpg"); ofn.lCustData = 0; ofn.lpfnHook = NULL; ofn.lpTemplateName = NULL; } LPCTSTR open () { buf [0] = _T ('\0'); TCHAR tTitle[MAX_PATH] = _T ("打开文件"); ofn.lpstrFile = buf; ofn.lpstrFileTitle = tTitle; ofn.Flags = 0; if (GetOpenFileName (&ofn)) return buf; return NULL; } LPCTSTR save () { buf [0] = _T ('\0'); TCHAR tTitle [MAX_PATH] = _T ("另存为"); ofn.lpstrFile = buf; ofn.lpstrFileTitle = tTitle; ofn.Flags = OFN_OVERWRITEPROMPT; if (GetSaveFileName (&ofn)) return buf; return NULL; } private: OPENFILENAME ofn; TCHAR buf [MAX_PATH]; }; class selFolder { public: selFolder (HWND hWnd) { bi.hwndOwner = hWnd; bi.pidlRoot = NULL; bi.pszDisplayName = buf;//此参数如为NULL则不能显示对话框 bi.lpszTitle = _T("指定目录"); bi.ulFlags = 0; bi.lpfn = NULL; bi.iImage = NULL; bi.lParam = 0; } LPCTSTR get () { BOOL bRet = FALSE; LPITEMIDLIST pIDList = SHBrowseForFolder (&bi);//调用显示选择对话框 if (pIDList) { SHGetPathFromIDList (pIDList, buf); bRet = TRUE; } LPMALLOC lpMalloc; if (FAILED (SHGetMalloc (&lpMalloc))) return FALSE; //释放内存 lpMalloc->Free (pIDList); lpMalloc->Release (); if (bRet) return buf; return NULL; } private: BROWSEINFO bi; TCHAR buf [MAX_PATH]; }; #endif //__HSELECT_HPP__ |
由于设置比较麻烦,所以打开文件、保存文件的类型就直接固定了,各位看情况修改。调用示例:
1 2 3 4 5 6 7 8 | #include "hSelect.hpp" int main(int argc, char* argv[]) { selFile f (NULL); LPCTSTR p = f.open (); if (p) MessageBox (NULL, p, _T("test"), MB_ICONINFORMATION); return 0; } |
此头文件包含两个类,selFile与selFolder,分别对应文件和文件夹。创建时统一传递一个参数,当前窗口句柄,如果不是很懂那直接传NULL就可以了。然后,selFile提供了两个公开方法,open与save,分别对应打开文件和保存文件;selFolder提供了一个公开方法,get,表示获取用户选择的目录,这仨返回的都是LPCTSTR类型,如果成功即为选择的路径,如果失败那么统一返回NULL。
注:selFile或selFolder对象释放时,返回的指针同时失效,使用时注意下。
这种文件夹选择对话框不好用,还有一种更好用的文件夹选择框,在MFC下特别好用,代码如下:
1 2 3 | CFolderPickerDialog dlg (path, 0, this); if (IDOK == dlg.DoModal ()) path = dlg.GetFolderPath (); |