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;
- WCHAR szFile[MAX_PATH];
- HWND SitehWnd;
- SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
- string str_filter = string(filter) + string("\0");
- replace(str_filter.begin(), str_filter.end(), '|', '\0');
- string str_fname = fname;
- LPCWSTR cwstr_filter = widen(str_filter).c_str();
- LPCWSTR cwstr_fname = widen(str_fname).c_str();
- wchar_t* wstr_fname = const_cast<wchar_t*>(cwstr_fname);
- ZeroMemory(&ofn, sizeof(ofn));
- ofn.lStructSize = sizeof(ofn);
- ofn.hwndOwner = SitehWnd;
- ofn.lpstrFile = wstr_fname;
- ofn.nMaxFile = sizeof(szFile);
- ofn.lpstrFilter = cwstr_filter;
- ofn.nFilterIndex = 1;
- ofn.lpstrTitle = NULL;
- ofn.lpstrInitialDir = NULL;
- ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES;
- if (GetSaveFileNameW(&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;
- WCHAR szFile[MAX_PATH];
- HWND SitehWnd;
- SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
- string str_filter = string(filter) + string("\0");
- replace(str_filter.begin(), str_filter.end(), '|', '\0');
- string str_fname = fname;
- LPCWSTR cwstr_filter = widen(str_filter).c_str();
- LPCWSTR cwstr_fname = widen(str_fname).c_str();
- wchar_t* wstr_fname = const_cast<wchar_t*>(cwstr_fname);
- ZeroMemory(&ofn, sizeof(ofn));
- ofn.lStructSize = sizeof(ofn);
- ofn.hwndOwner = SitehWnd;
- ofn.lpstrFile = wstr_fname;
- ofn.nMaxFile = sizeof(szFile);
- ofn.lpstrFilter = cwstr_filter;
- ofn.nFilterIndex = 1;
- 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;
- WCHAR szFile[MAX_PATH];
- HWND SitehWnd;
- SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
- string str_filter = string(filter) + string("\0");
- replace(str_filter.begin(), str_filter.end(), '|', '\0');
- string str_fname = fname;
- string str_dir = dir;
- string str_title = title;
- LPCWSTR cwstr_filter = widen(str_filter).c_str();
- LPCWSTR cwstr_fname = widen(str_fname).c_str();
- wchar_t* wstr_fname = const_cast<wchar_t*>(cwstr_fname);
- LPCWSTR cwstr_dir = widen(str_dir).c_str();
- LPCWSTR cwstr_title = widen(str_title).c_str();
- ZeroMemory(&ofn, sizeof(ofn));
- ofn.lStructSize = sizeof(ofn);
- ofn.hwndOwner = SitehWnd;
- ofn.lpstrFile = wstr_fname;
- ofn.nMaxFile = sizeof(szFile);
- ofn.lpstrFilter = cwstr_filter;
- ofn.nFilterIndex = 1;
- ofn.lpstrTitle = cwstr_title;
- ofn.lpstrInitialDir = cwstr_dir;
- ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES;
- if (GetSaveFileNameW(&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;
- WCHAR szFile[MAX_PATH];
- HWND SitehWnd;
- SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
- string str_filter = string(filter) + string("\0");
- replace(str_filter.begin(), str_filter.end(), '|', '\0');
- string str_fname = fname;
- string str_dir = dir;
- string str_title = title;
- LPCWSTR cwstr_filter = widen(str_filter).c_str();
- LPCWSTR cwstr_fname = widen(str_fname).c_str();
- wchar_t* wstr_fname = const_cast<wchar_t*>(cwstr_fname);
- LPCWSTR cwstr_dir = widen(str_dir).c_str();
- LPCWSTR cwstr_title = widen(str_title).c_str();
- ZeroMemory(&ofn, sizeof(ofn));
- ofn.lStructSize = sizeof(ofn);
- ofn.hwndOwner = SitehWnd;
- ofn.lpstrFile = wstr_fname;
- ofn.nMaxFile = sizeof(szFile);
- ofn.lpstrFilter = cwstr_filter;
- ofn.nFilterIndex = 1;
- ofn.lpstrTitle = cwstr_title;
- ofn.lpstrInitialDir = cwstr_dir;
- 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