Advertisement
Guest User

OpenSaveDialogs.cpp

a guest
Mar 13th, 2018
106
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.  
  30.         HWND SitehWnd;
  31.         SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
  32.  
  33.         string str_filter = string(filter).append("||");
  34.         replace(str_filter.begin(), str_filter.end(), '|', '\0');
  35.         string str_fname = fname;
  36.  
  37.         tstring tstr_filter = widen(str_filter);
  38.         tstring tstr_fname = widen(str_fname);
  39.  
  40.         wchar_t wstr_fname[MAX_PATH];
  41.         wcsncpy(wstr_fname, tstr_fname.c_str(), MAX_PATH);
  42.  
  43.         ZeroMemory(&ofn, sizeof(ofn));
  44.         ofn.lStructSize = sizeof(ofn);
  45.         ofn.hwndOwner = SitehWnd;
  46.         ofn.lpstrFile = wstr_fname;
  47.         ofn.nMaxFile = MAX_PATH;
  48.         ofn.lpstrFilter = tstr_filter.c_str();
  49.         ofn.nFilterIndex = 0;
  50.         ofn.lpstrTitle = NULL;
  51.         ofn.lpstrInitialDir = NULL;
  52.         ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES;
  53.  
  54.         if (GetOpenFileNameW(&ofn) != 0)
  55.         {
  56.             static string result;
  57.             result = shorten(ofn.lpstrFile);
  58.             return (char *)result.c_str();
  59.         }
  60.  
  61.         return (char *)"";
  62.     }
  63.  
  64.     char *get_save_filename(char *filter, char *fname)
  65.     {
  66.         OPENFILENAMEW ofn;
  67.  
  68.         HWND SitehWnd;
  69.         SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
  70.  
  71.         string str_filter = string(filter).append("||");
  72.         replace(str_filter.begin(), str_filter.end(), '|', '\0');
  73.         string str_fname = fname;
  74.  
  75.         tstring tstr_filter = widen(str_filter);
  76.         tstring tstr_fname = widen(str_fname);
  77.  
  78.         wchar_t wstr_fname[MAX_PATH];
  79.         wcsncpy(wstr_fname, tstr_fname.c_str(), MAX_PATH);
  80.  
  81.         ZeroMemory(&ofn, sizeof(ofn));
  82.         ofn.lStructSize = sizeof(ofn);
  83.         ofn.hwndOwner = SitehWnd;
  84.         ofn.lpstrFile = wstr_fname;
  85.         ofn.nMaxFile = MAX_PATH;
  86.         ofn.lpstrFilter = tstr_filter.c_str();
  87.         ofn.nFilterIndex = 0;
  88.         ofn.lpstrTitle = NULL;
  89.         ofn.lpstrInitialDir = NULL;
  90.         ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES | OFN_OVERWRITEPROMPT;
  91.  
  92.         if (GetSaveFileNameW(&ofn) != 0)
  93.         {
  94.             static string result;
  95.             result = shorten(ofn.lpstrFile);
  96.             return (char *)result.c_str();
  97.         }
  98.  
  99.         return (char *)"";
  100.     }
  101.  
  102.     char *get_open_filename_ext(char *filter, char *fname, char *dir, char *title)
  103.     {
  104.         OPENFILENAMEW ofn;
  105.  
  106.         HWND SitehWnd;
  107.         SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
  108.  
  109.         string str_filter = string(filter).append("||");
  110.         replace(str_filter.begin(), str_filter.end(), '|', '\0');
  111.         string str_fname = fname;
  112.         string str_dir = dir;
  113.         string str_title = title;
  114.  
  115.         tstring tstr_filter = widen(str_filter);
  116.         tstring tstr_fname = widen(str_fname);
  117.         tstring tstr_dir = widen(str_dir);
  118.         tstring tstr_title = widen(str_title);
  119.  
  120.         wchar_t wstr_fname[MAX_PATH];
  121.         wcsncpy(wstr_fname, tstr_fname.c_str(), MAX_PATH);
  122.  
  123.         ZeroMemory(&ofn, sizeof(ofn));
  124.         ofn.lStructSize = sizeof(ofn);
  125.         ofn.hwndOwner = SitehWnd;
  126.         ofn.lpstrFile = wstr_fname;
  127.         ofn.nMaxFile = MAX_PATH;
  128.         ofn.lpstrFilter = tstr_filter.c_str();
  129.         ofn.nFilterIndex = 0;
  130.         ofn.lpstrTitle = tstr_title.c_str();
  131.         ofn.lpstrInitialDir = tstr_dir.c_str();
  132.         ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES;
  133.  
  134.         if (GetOpenFileNameW(&ofn) != 0)
  135.         {
  136.             static string result;
  137.             result = shorten(ofn.lpstrFile);
  138.             return (char *)result.c_str();
  139.         }
  140.  
  141.         return (char *)"";
  142.     }
  143.  
  144.     char *get_save_filename_ext(char *filter, char *fname, char *dir, char *title)
  145.     {
  146.         OPENFILENAMEW ofn;
  147.  
  148.         HWND SitehWnd;
  149.         SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
  150.  
  151.         string str_filter = string(filter).append("||");
  152.         replace(str_filter.begin(), str_filter.end(), '|', '\0');
  153.         string str_fname = fname;
  154.         string str_dir = dir;
  155.         string str_title = title;
  156.  
  157.         tstring tstr_filter = widen(str_filter);
  158.         tstring tstr_fname = widen(str_fname);
  159.         tstring tstr_dir = widen(str_dir);
  160.         tstring tstr_title = widen(str_title);
  161.  
  162.         wchar_t wstr_fname[MAX_PATH];
  163.         wcsncpy(wstr_fname, tstr_fname.c_str(), MAX_PATH);
  164.  
  165.         ZeroMemory(&ofn, sizeof(ofn));
  166.         ofn.lStructSize = sizeof(ofn);
  167.         ofn.hwndOwner = SitehWnd;
  168.         ofn.lpstrFile = wstr_fname;
  169.         ofn.nMaxFile = MAX_PATH;
  170.         ofn.lpstrFilter = tstr_filter.c_str();
  171.         ofn.nFilterIndex = 0;
  172.         ofn.lpstrTitle = tstr_title.c_str();
  173.         ofn.lpstrInitialDir = tstr_dir.c_str();
  174.         ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES | OFN_OVERWRITEPROMPT;
  175.  
  176.         if (GetSaveFileNameW(&ofn) != 0)
  177.         {
  178.             static string result;
  179.             result = shorten(ofn.lpstrFile);
  180.             return (char *)result.c_str();
  181.         }
  182.  
  183.         return (char *)"";
  184.     }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement