Guest User

OpenSaveDialogs.cpp

a guest
Mar 18th, 2018
135
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. #define DLL extern "C" _declspec(dllexport)
  11. using namespace std;
  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.     string str_fname = fname;
  37.  
  38.     tstring tstr_filter = widen(str_filter);
  39.     replace(tstr_filter.begin(), tstr_filter.end(), '|', '\0');
  40.     tstring tstr_fname = widen(str_fname);
  41.  
  42.     WCHAR 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;
  55.  
  56.     if (GetOpenFileNameW(&ofn) != 0)
  57.     {
  58.         static string result;
  59.         result = shorten(wstr_fname);
  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.     string str_fname = fname;
  75.  
  76.     tstring tstr_filter = widen(str_filter);
  77.     replace(tstr_filter.begin(), tstr_filter.end(), '|', '\0');
  78.     tstring tstr_fname = widen(str_fname);
  79.  
  80.     WCHAR 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_OVERWRITEPROMPT;
  93.  
  94.     if (GetSaveFileNameW(&ofn) != 0)
  95.     {
  96.         static string result;
  97.         result = shorten(wstr_fname);
  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.     string str_fname = fname;
  113.     string str_dir = dir;
  114.     string str_title = title;
  115.  
  116.     tstring tstr_filter = widen(str_filter);
  117.     replace(tstr_filter.begin(), tstr_filter.end(), '|', '\0');
  118.     tstring tstr_fname = widen(str_fname);
  119.     tstring tstr_dir = widen(str_dir);
  120.     tstring tstr_title = widen(str_title);
  121.  
  122.     WCHAR 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;
  135.  
  136.     if (GetOpenFileNameW(&ofn) != 0)
  137.     {
  138.         static string result;
  139.         result = shorten(wstr_fname);
  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.     string str_fname = fname;
  155.     string str_dir = dir;
  156.     string str_title = title;
  157.  
  158.     tstring tstr_filter = widen(str_filter);
  159.     replace(tstr_filter.begin(), tstr_filter.end(), '|', '\0');
  160.     tstring tstr_fname = widen(str_fname);
  161.     tstring tstr_dir = widen(str_dir);
  162.     tstring tstr_title = widen(str_title);
  163.  
  164.     WCHAR 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_OVERWRITEPROMPT;
  177.  
  178.     if (GetSaveFileNameW(&ofn) != 0)
  179.     {
  180.         static string result;
  181.         result = shorten(wstr_fname);
  182.         return (char *)result.c_str();
  183.     }
  184.  
  185.     return (char *)"";
  186. }
  187.  
  188. void ClientResize(HWND hWnd, int nWidth, int nHeight)
  189. {
  190.     RECT rcClient, rcWind;
  191.     POINT ptDiff;
  192.     GetClientRect(hWnd, &rcClient);
  193.     GetWindowRect(hWnd, &rcWind);
  194.     ptDiff.x = (rcWind.right - rcWind.left) - rcClient.right;
  195.     ptDiff.y = (rcWind.bottom - rcWind.top) - rcClient.bottom;
  196.     MoveWindow(hWnd, rcWind.left, rcWind.top, nWidth + ptDiff.x, nHeight + ptDiff.y, TRUE);
  197. }
  198.  
  199. WCHAR wstr_dname[MAX_PATH];
  200.  
  201. UINT APIENTRY OFNHookProcOldStyle(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  202. {
  203.     if (uMsg == WM_INITDIALOG)
  204.     {
  205.         ClientResize(hWnd, 424, 255);
  206.  
  207.         RECT rect;
  208.         GetWindowRect(hWnd, &rect);
  209.         MoveWindow(hWnd,
  210.             (GetSystemMetrics(SM_CXSCREEN) / 2) - ((rect.right - rect.left) / 2),
  211.             (GetSystemMetrics(SM_CYSCREEN) / 2) - ((rect.bottom - rect.top) / 2),
  212.             rect.right - rect.left, rect.bottom - rect.top, TRUE);
  213.  
  214.         HWND bttn1 = GetDlgItem(hWnd, IDOK);
  215.         HWND bttn2 = GetDlgItem(hWnd, IDCANCEL);
  216.         HWND list1 = GetDlgItem(hWnd, 1120);
  217.         HWND list2 = GetDlgItem(hWnd, 1121);
  218.         HWND label1 = GetDlgItem(hWnd, 1090);
  219.         HWND label2 = GetDlgItem(hWnd, 65535);
  220.         HWND label3 = GetDlgItem(hWnd, 1089);
  221.         HWND label4 = GetDlgItem(hWnd, 1091);
  222.         HWND label5 = GetDlgItem(hWnd, 1088);
  223.         HWND cmbbx1 = GetDlgItem(hWnd, 1136);
  224.         HWND cmbbx2 = GetDlgItem(hWnd, 1137);
  225.         HWND txtbx1 = GetDlgItem(hWnd, 1152);
  226.  
  227.         SetWindowText(label1, "&Files: (*.*)");
  228.         SetWindowText(label2, "&Directories:");
  229.         SetWindowText(label3, "Directory &Name:");
  230.         SetWindowText(label4, "D&rives:");
  231.         DestroyWindow(cmbbx1);
  232.         DestroyWindow(txtbx1);
  233.  
  234.         MoveWindow(bttn1, 256, 224, 77, 27, TRUE);
  235.         MoveWindow(bttn2, 340, 224, 77, 27, TRUE);
  236.         MoveWindow(label1, 232, 56, 72, 16, TRUE);
  237.         MoveWindow(label2, 8, 56, 72, 16, TRUE);
  238.         MoveWindow(label3, 8, 8, 93, 16, TRUE);
  239.         MoveWindow(label4, 232, 176, 50, 16, TRUE);
  240.         MoveWindow(label5, 8, 24, 409 * 100, 16, TRUE);
  241.         MoveWindow(list1, 232, 72, 185, 93, TRUE);
  242.         MoveWindow(list2, 8, 72, 213, 123, TRUE);
  243.         MoveWindow(cmbbx2, 232, 192, 185, 19, TRUE);
  244.  
  245.         DlgDirListW(hWnd, wstr_dname, 1120, 1088, DDL_ARCHIVE | DDL_READWRITE | DDL_READONLY);
  246.         PostMessageW(hWnd, WM_SETFOCUS, 0, 0);
  247.     }
  248.  
  249.     if (uMsg == WM_COMMAND && HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == IDOK)
  250.     {
  251.         HWND label1 = GetDlgItem(hWnd, 1090);
  252.         HWND label2 = GetDlgItem(hWnd, 65535);
  253.         HWND label3 = GetDlgItem(hWnd, 1089);
  254.         HWND label4 = GetDlgItem(hWnd, 1091);
  255.         HWND label5 = GetDlgItem(hWnd, 1088);
  256.  
  257.         GetDlgItemTextW(hWnd, 1088, wstr_dname, MAX_PATH);
  258.         PostMessageW(hWnd, WM_COMMAND, IDABORT, 0);
  259.         return TRUE;
  260.     }
  261.  
  262.     if (uMsg == WM_COMMAND && HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == IDCANCEL)
  263.     {
  264.         tstring tstr_dname = widen("");
  265.         wcsncpy_s(wstr_dname, tstr_dname.c_str(), MAX_PATH);
  266.         PostMessageW(hWnd, WM_COMMAND, IDABORT, 0);
  267.         return TRUE;
  268.     }
  269.  
  270.     if (uMsg == WM_CLOSE)
  271.     {
  272.         tstring tstr_dname = widen("");
  273.         wcsncpy_s(wstr_dname, tstr_dname.c_str(), MAX_PATH);
  274.         PostMessageW(hWnd, WM_COMMAND, IDABORT, 0);
  275.         return TRUE;
  276.     }
  277.  
  278.     return FALSE;
  279. }
  280.  
  281. DLL char *get_directory(char *dname)
  282. {
  283.     OPENFILENAMEW ofn;
  284.  
  285.     HWND SitehWnd;
  286.     SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
  287.  
  288.     string str_dname = dname;
  289.  
  290.     tstring tstr_filter = widen("*.*|*.*|");
  291.     replace(tstr_filter.begin(), tstr_filter.end(), '|', '\0');
  292.     tstring tstr_dname = widen(str_dname);
  293.     tstring tstr_title = widen("Select Directory");
  294.     tstring tstr_empty = widen("");
  295.  
  296.     if (tstr_dname == tstr_empty)
  297.         GetCurrentDirectoryW(MAX_PATH, wstr_dname);
  298.     else
  299.         wcsncpy_s(wstr_dname, tstr_dname.c_str(), MAX_PATH);
  300.  
  301.     ZeroMemory(&ofn, sizeof(ofn));
  302.     ofn.lStructSize = sizeof(ofn);
  303.     ofn.hwndOwner = SitehWnd;
  304.     ofn.lpstrFile = NULL;
  305.     ofn.nMaxFile = MAX_PATH;
  306.     ofn.lpstrFilter = tstr_filter.c_str();
  307.     ofn.nFilterIndex = 0;
  308.     ofn.lpstrTitle = tstr_title.c_str();
  309.     ofn.lpstrInitialDir = wstr_dname;
  310.     ofn.Flags = OFN_NONETWORKBUTTON | OFN_ENABLEHOOK | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES;
  311.     ofn.lpfnHook = OFNHookProcOldStyle;
  312.  
  313.     GetOpenFileNameW(&ofn);
  314.  
  315.     DWORD attrib = GetFileAttributesW(wstr_dname);
  316.  
  317.     if (attrib != INVALID_FILE_ATTRIBUTES && (attrib & FILE_ATTRIBUTE_DIRECTORY))
  318.     {
  319.         static string result;
  320.         result = shorten(wstr_dname);
  321.         return (char *)result.c_str();
  322.     }
  323.  
  324.     return (char *)"";
  325. }
  326.  
  327. DLL char *get_directory_alt(char *capt, char *root)
  328. {
  329.     BROWSEINFOW bi;
  330.  
  331.     HWND SitehWnd;
  332.     SitehWnd = GetAncestor(GetActiveWindow(), GA_ROOTOWNER);
  333.  
  334.     string str_capt = capt;
  335.     string str_root = root;
  336.  
  337.     tstring tstr_capt = widen(str_capt);
  338.     tstring tstr_root = widen(str_root);
  339.     tstring tstr_empty = widen("");
  340.     tstring tstr_zero = widen("0");
  341.  
  342.     LPITEMIDLIST pstr_root;
  343.  
  344.     if (tstr_root == tstr_empty)
  345.         SHParseDisplayName(tstr_zero.c_str(), 0, &pstr_root, 0, 0);
  346.     else
  347.         SHParseDisplayName(tstr_root.c_str(), 0, &pstr_root, 0, 0);
  348.  
  349.     WCHAR wstr_dir[MAX_PATH];
  350.  
  351.     ZeroMemory(&bi, sizeof(bi));
  352.     bi.hwndOwner = SitehWnd;
  353.     bi.pidlRoot = pstr_root;
  354.     bi.pszDisplayName = wstr_dir;
  355.     bi.lpszTitle = tstr_capt.c_str();
  356.     bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE | BIF_NONEWFOLDERBUTTON;
  357.  
  358.     LPITEMIDLIST lpItem = SHBrowseForFolderW(&bi);
  359.     if (lpItem != NULL)
  360.     {
  361.         if (SHGetPathFromIDListW(lpItem, wstr_dir) == 0)
  362.             return (char *)"";
  363.         else
  364.         {
  365.             static string result;
  366.             result = shorten(wstr_dir);
  367.             return (char *)result.c_str();
  368.         }
  369.     }
  370.  
  371.     return (char *)"";
  372. }
RAW Paste Data