Advertisement
Guest User

OpenSaveDialogs.cpp

a guest
Mar 13th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <windows.h>
  2. #include <Commdlg.h>
  3. #include <algorithm>
  4. #include <string>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. typedef basic_string<WCHAR> tstring;
  10. tstring widen(string str)
  11. {
  12.     const size_t wchar_count = str.size()+1;
  13.     vector<WCHAR> buf(wchar_count);
  14.     return tstring{buf.data(),(size_t)MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,buf.data(),wchar_count)};
  15. }
  16.  
  17. string shorten(tstring str)
  18. {
  19.     int nbytes = WideCharToMultiByte(CP_UTF8,0,str.c_str(),(int)str.length(),NULL,0,NULL,NULL);
  20.     vector<char> buf((size_t)nbytes);
  21.     return string{buf.data(),(size_t)WideCharToMultiByte(CP_UTF8,0,str.c_str(),(int)str.length(),buf.data(),nbytes,NULL,NULL)};
  22. }
  23.  
  24. extern "C"
  25. {
  26.     char *get_open_filename(char *filter, char *fname)
  27.     {
  28.         OPENFILENAMEW ofn;
  29.         WCHAR szFile[MAX_PATH];
  30.  
  31.         HWND SitehWnd;
  32.         SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
  33.  
  34.         string str_filter = string(filter) + string("\0");
  35.         replace(str_filter.begin(), str_filter.end(), '|', '\0');
  36.         string str_fname = fname;
  37.  
  38.         LPCWSTR cwstr_filter = widen(str_filter).c_str();
  39.         LPCWSTR cwstr_fname = widen(str_fname).c_str();
  40.         wchar_t* wstr_fname = const_cast<wchar_t*>(cwstr_fname);
  41.  
  42.         ZeroMemory(&ofn, sizeof(ofn));
  43.         ofn.lStructSize = sizeof(ofn);
  44.         ofn.hwndOwner = SitehWnd;
  45.         ofn.lpstrFile = wstr_fname;
  46.         ofn.nMaxFile = sizeof(szFile);
  47.         ofn.lpstrFilter = cwstr_filter;
  48.         ofn.nFilterIndex = 1;
  49.         ofn.lpstrTitle = NULL;
  50.         ofn.lpstrInitialDir = NULL;
  51.         ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES;
  52.  
  53.         if (GetSaveFileNameW(&ofn) != 0)
  54.         {
  55.             static string result;
  56.             result = shorten(ofn.lpstrFile);
  57.             return (char *)result.c_str();
  58.         }
  59.  
  60.         return (char *)"";
  61.     }
  62.  
  63.     char *get_save_filename(char *filter, char *fname)
  64.     {
  65.         OPENFILENAMEW ofn;
  66.         WCHAR szFile[MAX_PATH];
  67.  
  68.         HWND SitehWnd;
  69.         SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
  70.  
  71.         string str_filter = string(filter) + string("\0");
  72.         replace(str_filter.begin(), str_filter.end(), '|', '\0');
  73.         string str_fname = fname;
  74.  
  75.         LPCWSTR cwstr_filter = widen(str_filter).c_str();
  76.         LPCWSTR cwstr_fname = widen(str_fname).c_str();
  77.         wchar_t* wstr_fname = const_cast<wchar_t*>(cwstr_fname);
  78.  
  79.         ZeroMemory(&ofn, sizeof(ofn));
  80.         ofn.lStructSize = sizeof(ofn);
  81.         ofn.hwndOwner = SitehWnd;
  82.         ofn.lpstrFile = wstr_fname;
  83.         ofn.nMaxFile = sizeof(szFile);
  84.         ofn.lpstrFilter = cwstr_filter;
  85.         ofn.nFilterIndex = 1;
  86.         ofn.lpstrTitle = NULL;
  87.         ofn.lpstrInitialDir = NULL;
  88.         ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES | OFN_OVERWRITEPROMPT;
  89.  
  90.         if (GetSaveFileNameW(&ofn) != 0)
  91.         {
  92.             static string result;
  93.             result = shorten(ofn.lpstrFile);
  94.             return (char *)result.c_str();
  95.         }
  96.  
  97.         return (char *)"";
  98.     }
  99.  
  100.     char *get_open_filename_ext(char *filter, char *fname, char *dir, char *title)
  101.     {
  102.         OPENFILENAMEW ofn;
  103.         WCHAR szFile[MAX_PATH];
  104.  
  105.         HWND SitehWnd;
  106.         SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
  107.  
  108.         string str_filter = string(filter) + string("\0");
  109.         replace(str_filter.begin(), str_filter.end(), '|', '\0');
  110.         string str_fname = fname;
  111.         string str_dir = dir;
  112.         string str_title = title;
  113.  
  114.         LPCWSTR cwstr_filter = widen(str_filter).c_str();
  115.         LPCWSTR cwstr_fname = widen(str_fname).c_str();
  116.         wchar_t* wstr_fname = const_cast<wchar_t*>(cwstr_fname);
  117.         LPCWSTR cwstr_dir = widen(str_dir).c_str();
  118.         LPCWSTR cwstr_title = widen(str_title).c_str();
  119.  
  120.         ZeroMemory(&ofn, sizeof(ofn));
  121.         ofn.lStructSize = sizeof(ofn);
  122.         ofn.hwndOwner = SitehWnd;
  123.         ofn.lpstrFile = wstr_fname;
  124.         ofn.nMaxFile = sizeof(szFile);
  125.         ofn.lpstrFilter = cwstr_filter;
  126.         ofn.nFilterIndex = 1;
  127.         ofn.lpstrTitle = cwstr_title;
  128.         ofn.lpstrInitialDir = cwstr_dir;
  129.         ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES;
  130.  
  131.         if (GetSaveFileNameW(&ofn) != 0)
  132.         {
  133.             static string result;
  134.             result = shorten(ofn.lpstrFile);
  135.             return (char *)result.c_str();
  136.         }
  137.  
  138.         return (char *)"";
  139.     }
  140.  
  141.     char *get_save_filename_ext(char *filter, char *fname, char *dir, char *title)
  142.     {
  143.         OPENFILENAMEW ofn;
  144.         WCHAR szFile[MAX_PATH];
  145.  
  146.         HWND SitehWnd;
  147.         SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
  148.  
  149.         string str_filter = string(filter) + string("\0");
  150.         replace(str_filter.begin(), str_filter.end(), '|', '\0');
  151.         string str_fname = fname;
  152.         string str_dir = dir;
  153.         string str_title = title;
  154.  
  155.         LPCWSTR cwstr_filter = widen(str_filter).c_str();
  156.         LPCWSTR cwstr_fname = widen(str_fname).c_str();
  157.         wchar_t* wstr_fname = const_cast<wchar_t*>(cwstr_fname);
  158.         LPCWSTR cwstr_dir = widen(str_dir).c_str();
  159.         LPCWSTR cwstr_title = widen(str_title).c_str();
  160.  
  161.         ZeroMemory(&ofn, sizeof(ofn));
  162.         ofn.lStructSize = sizeof(ofn);
  163.         ofn.hwndOwner = SitehWnd;
  164.         ofn.lpstrFile = wstr_fname;
  165.         ofn.nMaxFile = sizeof(szFile);
  166.         ofn.lpstrFilter = cwstr_filter;
  167.         ofn.nFilterIndex = 1;
  168.         ofn.lpstrTitle = cwstr_title;
  169.         ofn.lpstrInitialDir = cwstr_dir;
  170.         ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES | OFN_OVERWRITEPROMPT;
  171.  
  172.         if (GetSaveFileNameW(&ofn) != 0)
  173.         {
  174.             static string result;
  175.             result = shorten(ofn.lpstrFile);
  176.             return (char *)result.c_str();
  177.         }
  178.  
  179.         return (char *)"";
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement