Advertisement
Guest User

OpenSaveDialogs.cpp

a guest
Mar 13th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "stdafx.h"
  2. #include <windows.h>
  3. #include <Commdlg.h>
  4. #include <Shlobj.h>
  5. #include <wchar.h>
  6. #include <algorithm>
  7. #include <string>
  8. #include <vector>
  9.  
  10. using namespace std;
  11. #define DLL extern "C" _declspec(dllexport)
  12.  
  13. typedef basic_string<WCHAR> tstring;
  14. tstring widen(string str)
  15. {
  16.     const size_t wchar_count = str.size() + 1;
  17.     vector<WCHAR> buf(wchar_count);
  18.     return tstring{ buf.data(), (size_t)MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, buf.data(), wchar_count) };
  19. }
  20.  
  21. string shorten(tstring str)
  22. {
  23.     int nbytes = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), (int)str.length(), NULL, 0, NULL, NULL);
  24.     vector<char> buf((size_t)nbytes);
  25.     return string{ buf.data(), (size_t)WideCharToMultiByte(CP_UTF8, 0, str.c_str(), (int)str.length(), buf.data(), nbytes, NULL, NULL) };
  26. }
  27.  
  28. DLL char *get_open_filename(char *filter, char *fname)
  29. {
  30.     OPENFILENAMEW ofn;
  31.  
  32.     HWND SitehWnd;
  33.     SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
  34.  
  35.     string str_filter = string(filter).append("||");
  36.     replace(str_filter.begin(), str_filter.end(), '|', '\0');
  37.     string str_fname = fname;
  38.  
  39.     tstring tstr_filter = widen(str_filter);
  40.     tstring tstr_fname = widen(str_fname);
  41.  
  42.     wchar_t wstr_fname[MAX_PATH];
  43.     wcsncpy_s(wstr_fname, tstr_fname.c_str(), MAX_PATH);
  44.  
  45.     ZeroMemory(&ofn, sizeof(ofn));
  46.     ofn.lStructSize = sizeof(ofn);
  47.     ofn.hwndOwner = SitehWnd;
  48.     ofn.lpstrFile = wstr_fname;
  49.     ofn.nMaxFile = MAX_PATH;
  50.     ofn.lpstrFilter = tstr_filter.c_str();
  51.     ofn.nFilterIndex = 0;
  52.     ofn.lpstrTitle = NULL;
  53.     ofn.lpstrInitialDir = NULL;
  54.     ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES;
  55.  
  56.     if (GetOpenFileNameW(&ofn) != 0)
  57.     {
  58.         static string result;
  59.         result = shorten(ofn.lpstrFile);
  60.         return (char *)result.c_str();
  61.     }
  62.  
  63.     return (char *)"";
  64. }
  65.  
  66. DLL char *get_save_filename(char *filter, char *fname)
  67. {
  68.     OPENFILENAMEW ofn;
  69.  
  70.     HWND SitehWnd;
  71.     SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
  72.  
  73.     string str_filter = string(filter).append("||");
  74.     replace(str_filter.begin(), str_filter.end(), '|', '\0');
  75.     string str_fname = fname;
  76.  
  77.     tstring tstr_filter = widen(str_filter);
  78.     tstring tstr_fname = widen(str_fname);
  79.  
  80.     wchar_t wstr_fname[MAX_PATH];
  81.     wcsncpy_s(wstr_fname, tstr_fname.c_str(), MAX_PATH);
  82.  
  83.     ZeroMemory(&ofn, sizeof(ofn));
  84.     ofn.lStructSize = sizeof(ofn);
  85.     ofn.hwndOwner = SitehWnd;
  86.     ofn.lpstrFile = wstr_fname;
  87.     ofn.nMaxFile = MAX_PATH;
  88.     ofn.lpstrFilter = tstr_filter.c_str();
  89.     ofn.nFilterIndex = 0;
  90.     ofn.lpstrTitle = NULL;
  91.     ofn.lpstrInitialDir = NULL;
  92.     ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES | OFN_OVERWRITEPROMPT;
  93.  
  94.     if (GetSaveFileNameW(&ofn) != 0)
  95.     {
  96.         static string result;
  97.         result = shorten(ofn.lpstrFile);
  98.         return (char *)result.c_str();
  99.     }
  100.  
  101.     return (char *)"";
  102. }
  103.  
  104. DLL char *get_open_filename_ext(char *filter, char *fname, char *dir, char *title)
  105. {
  106.     OPENFILENAMEW ofn;
  107.  
  108.     HWND SitehWnd;
  109.     SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
  110.  
  111.     string str_filter = string(filter).append("||");
  112.     replace(str_filter.begin(), str_filter.end(), '|', '\0');
  113.     string str_fname = fname;
  114.     string str_dir = dir;
  115.     string str_title = title;
  116.  
  117.     tstring tstr_filter = widen(str_filter);
  118.     tstring tstr_fname = widen(str_fname);
  119.     tstring tstr_dir = widen(str_dir);
  120.     tstring tstr_title = widen(str_title);
  121.  
  122.     wchar_t wstr_fname[MAX_PATH];
  123.     wcsncpy_s(wstr_fname, tstr_fname.c_str(), MAX_PATH);
  124.  
  125.     ZeroMemory(&ofn, sizeof(ofn));
  126.     ofn.lStructSize = sizeof(ofn);
  127.     ofn.hwndOwner = SitehWnd;
  128.     ofn.lpstrFile = wstr_fname;
  129.     ofn.nMaxFile = MAX_PATH;
  130.     ofn.lpstrFilter = tstr_filter.c_str();
  131.     ofn.nFilterIndex = 0;
  132.     ofn.lpstrTitle = tstr_title.c_str();
  133.     ofn.lpstrInitialDir = tstr_dir.c_str();
  134.     ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES;
  135.  
  136.     if (GetOpenFileNameW(&ofn) != 0)
  137.     {
  138.         static string result;
  139.         result = shorten(ofn.lpstrFile);
  140.         return (char *)result.c_str();
  141.     }
  142.  
  143.     return (char *)"";
  144. }
  145.  
  146. DLL char *get_save_filename_ext(char *filter, char *fname, char *dir, char *title)
  147. {
  148.     OPENFILENAMEW ofn;
  149.  
  150.     HWND SitehWnd;
  151.     SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
  152.  
  153.     string str_filter = string(filter).append("||");
  154.     replace(str_filter.begin(), str_filter.end(), '|', '\0');
  155.     string str_fname = fname;
  156.     string str_dir = dir;
  157.     string str_title = title;
  158.  
  159.     tstring tstr_filter = widen(str_filter);
  160.     tstring tstr_fname = widen(str_fname);
  161.     tstring tstr_dir = widen(str_dir);
  162.     tstring tstr_title = widen(str_title);
  163.  
  164.     wchar_t wstr_fname[MAX_PATH];
  165.     wcsncpy_s(wstr_fname, tstr_fname.c_str(), MAX_PATH);
  166.  
  167.     ZeroMemory(&ofn, sizeof(ofn));
  168.     ofn.lStructSize = sizeof(ofn);
  169.     ofn.hwndOwner = SitehWnd;
  170.     ofn.lpstrFile = wstr_fname;
  171.     ofn.nMaxFile = MAX_PATH;
  172.     ofn.lpstrFilter = tstr_filter.c_str();
  173.     ofn.nFilterIndex = 0;
  174.     ofn.lpstrTitle = tstr_title.c_str();
  175.     ofn.lpstrInitialDir = tstr_dir.c_str();
  176.     ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES | OFN_OVERWRITEPROMPT;
  177.  
  178.     if (GetSaveFileNameW(&ofn) != 0)
  179.     {
  180.         static string result;
  181.         result = shorten(ofn.lpstrFile);
  182.         return (char *)result.c_str();
  183.     }
  184.  
  185.     return (char *)"";
  186. }
  187.  
  188. DLL char *get_directory_alt(char *capt, char *root)
  189. {
  190.     BROWSEINFOW bi;
  191.  
  192.     HWND SitehWnd;
  193.     SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
  194.  
  195.     string str_capt = capt;
  196.     string str_root = root;
  197.     tstring tstr_capt = widen(str_capt);
  198.     tstring tstr_root = widen(str_root);
  199.     WCHAR szDir[MAX_PATH];
  200.  
  201.     LPITEMIDLIST pstr_root;
  202.     SHParseDisplayName(tstr_root.c_str(), 0, &pstr_root, 0, 0);
  203.  
  204.     ZeroMemory(&bi, sizeof(bi));
  205.     bi.hwndOwner = SitehWnd;
  206.     bi.pidlRoot = pstr_root;
  207.     bi.pszDisplayName = szDir;
  208.     bi.lpszTitle = tstr_capt.c_str();
  209.     bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE | BIF_NONEWFOLDERBUTTON;
  210.  
  211.     LPITEMIDLIST lpItem = SHBrowseForFolderW(&bi);
  212.     if (lpItem != NULL)
  213.     {
  214.         if (SHGetPathFromIDListW(lpItem, szDir) == 0)
  215.             return (char *)"";
  216.         else
  217.         {
  218.             static string result;
  219.             result = shorten(szDir);
  220.             return (char *)result.c_str();
  221.         }
  222.     }
  223.  
  224.     return (char *)"";
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement