Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <Commdlg.h>
- #include <algorithm>
- #include <string>
- #include <vector>
- using namespace std;
- typedef basic_string<WCHAR> tstring;
- tstring widen(string str)
- {
- const size_t wchar_count = str.size()+1;
- vector<WCHAR> buf(wchar_count);
- return tstring{buf.data(),(size_t)MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,buf.data(),wchar_count)};
- }
- string shorten(tstring str)
- {
- int nbytes = WideCharToMultiByte(CP_UTF8,0,str.c_str(),(int)str.length(),NULL,0,NULL,NULL);
- vector<char> buf((size_t)nbytes);
- return string{buf.data(),(size_t)WideCharToMultiByte(CP_UTF8,0,str.c_str(),(int)str.length(),buf.data(),nbytes,NULL,NULL)};
- }
- extern "C"
- {
- char *get_open_filename(char *filter, char *fname)
- {
- OPENFILENAMEW ofn;
- HWND SitehWnd;
- SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
- string str_filter = string(filter).append("||");
- replace(str_filter.begin(), str_filter.end(), '|', '\0');
- string str_fname = fname;
- tstring tstr_filter = widen(str_filter);
- tstring tstr_fname = widen(str_fname);
- wchar_t wstr_fname[MAX_PATH];
- wcsncpy(wstr_fname, tstr_fname.c_str(), MAX_PATH);
- ZeroMemory(&ofn, sizeof(ofn));
- ofn.lStructSize = sizeof(ofn);
- ofn.hwndOwner = SitehWnd;
- ofn.lpstrFile = wstr_fname;
- ofn.nMaxFile = MAX_PATH;
- ofn.lpstrFilter = tstr_filter.c_str();
- ofn.nFilterIndex = 0;
- ofn.lpstrTitle = NULL;
- ofn.lpstrInitialDir = NULL;
- ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES;
- if (GetOpenFileNameW(&ofn) != 0)
- {
- static string result;
- result = shorten(ofn.lpstrFile);
- return (char *)result.c_str();
- }
- return (char *)"";
- }
- char *get_save_filename(char *filter, char *fname)
- {
- OPENFILENAMEW ofn;
- HWND SitehWnd;
- SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
- string str_filter = string(filter).append("||");
- replace(str_filter.begin(), str_filter.end(), '|', '\0');
- string str_fname = fname;
- tstring tstr_filter = widen(str_filter);
- tstring tstr_fname = widen(str_fname);
- wchar_t wstr_fname[MAX_PATH];
- wcsncpy(wstr_fname, tstr_fname.c_str(), MAX_PATH);
- ZeroMemory(&ofn, sizeof(ofn));
- ofn.lStructSize = sizeof(ofn);
- ofn.hwndOwner = SitehWnd;
- ofn.lpstrFile = wstr_fname;
- ofn.nMaxFile = MAX_PATH;
- ofn.lpstrFilter = tstr_filter.c_str();
- ofn.nFilterIndex = 0;
- ofn.lpstrTitle = NULL;
- ofn.lpstrInitialDir = NULL;
- ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES | OFN_OVERWRITEPROMPT;
- if (GetSaveFileNameW(&ofn) != 0)
- {
- static string result;
- result = shorten(ofn.lpstrFile);
- return (char *)result.c_str();
- }
- return (char *)"";
- }
- char *get_open_filename_ext(char *filter, char *fname, char *dir, char *title)
- {
- OPENFILENAMEW ofn;
- HWND SitehWnd;
- SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
- string str_filter = string(filter).append("||");
- replace(str_filter.begin(), str_filter.end(), '|', '\0');
- string str_fname = fname;
- string str_dir = dir;
- string str_title = title;
- tstring tstr_filter = widen(str_filter);
- tstring tstr_fname = widen(str_fname);
- tstring tstr_dir = widen(str_dir);
- tstring tstr_title = widen(str_title);
- wchar_t wstr_fname[MAX_PATH];
- wcsncpy(wstr_fname, tstr_fname.c_str(), MAX_PATH);
- ZeroMemory(&ofn, sizeof(ofn));
- ofn.lStructSize = sizeof(ofn);
- ofn.hwndOwner = SitehWnd;
- ofn.lpstrFile = wstr_fname;
- ofn.nMaxFile = MAX_PATH;
- ofn.lpstrFilter = tstr_filter.c_str();
- ofn.nFilterIndex = 0;
- ofn.lpstrTitle = tstr_title.c_str();
- ofn.lpstrInitialDir = tstr_dir.c_str();
- ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES;
- if (GetOpenFileNameW(&ofn) != 0)
- {
- static string result;
- result = shorten(ofn.lpstrFile);
- return (char *)result.c_str();
- }
- return (char *)"";
- }
- char *get_save_filename_ext(char *filter, char *fname, char *dir, char *title)
- {
- OPENFILENAMEW ofn;
- HWND SitehWnd;
- SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
- string str_filter = string(filter).append("||");
- replace(str_filter.begin(), str_filter.end(), '|', '\0');
- string str_fname = fname;
- string str_dir = dir;
- string str_title = title;
- tstring tstr_filter = widen(str_filter);
- tstring tstr_fname = widen(str_fname);
- tstring tstr_dir = widen(str_dir);
- tstring tstr_title = widen(str_title);
- wchar_t wstr_fname[MAX_PATH];
- wcsncpy(wstr_fname, tstr_fname.c_str(), MAX_PATH);
- ZeroMemory(&ofn, sizeof(ofn));
- ofn.lStructSize = sizeof(ofn);
- ofn.hwndOwner = SitehWnd;
- ofn.lpstrFile = wstr_fname;
- ofn.nMaxFile = MAX_PATH;
- ofn.lpstrFilter = tstr_filter.c_str();
- ofn.nFilterIndex = 0;
- ofn.lpstrTitle = tstr_title.c_str();
- ofn.lpstrInitialDir = tstr_dir.c_str();
- ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES | OFN_OVERWRITEPROMPT;
- if (GetSaveFileNameW(&ofn) != 0)
- {
- static string result;
- result = shorten(ofn.lpstrFile);
- return (char *)result.c_str();
- }
- return (char *)"";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement