Combreal

pathSaver.cpp

Mar 25th, 2018
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.86 KB | None | 0 0
  1. #include <windows.h>
  2. #include <string>
  3. #include <commctrl.h>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <Lmcons.h>
  7. #include <shlobj.h>
  8. #include <algorithm>
  9. #include <tchar.h>
  10.  
  11. #define ID_BUTTON 1
  12. #define ID_EDIT 2
  13. #define ID_BUTTONB 3
  14. #define ID_BUTTONC 4
  15. #define IDM_ABOUT 5
  16. #define IDM_LABEL 6
  17. #define ID_EDITB 7
  18. #define ID_BUTTOND 8
  19.  
  20. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  21. LRESULT CALLBACK WndProcB(HWND, UINT, WPARAM, LPARAM);
  22. DWORD dwRet;
  23. TCHAR NPath[MAX_PATH];
  24. TCHAR username[UNLEN+1];
  25. DWORD username_len;
  26. bool showWindowB;
  27. HWND hWindowa, hWindowb, hwndText, hwndEdit, hwndLabel, hwndButton, hwndButtonB, hwndButtonC, hwndEditB, hwndButtonD;
  28. void CenterWindow(HWND);
  29. std::string newPath;
  30. std::string lpwstrToString(LPWSTR var);
  31. LPWSTR stringToLPWSTR(const std::string& instr);
  32. int CALLBACK BrowseForFolderCallback(HWND hwnd,UINT uMsg,LPARAM lp, LPARAM pData);
  33. BOOL BrowseFolders(HWND hwnd, LPSTR lpszFolder, LPSTR lpszTitle);
  34. void displayExcludeList(HWND hwndEdit, std::string passedString);
  35. std::ofstream file("backuprc", std::ios::out | std::ios_base::app);
  36. //CreateProcessW(xx,command,x...)
  37.  
  38. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
  39. {
  40.     showWindowB = false;
  41.     MSG msg;    
  42.     WNDCLASSW wc = {0};
  43.     wc.lpszClassName = L"Test101";
  44.     wc.hInstance = hInstance;
  45.     wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  46.     wc.lpfnWndProc = WndProc;
  47.     wc.hCursor = LoadCursor(0, IDC_ARROW);
  48.     RegisterClassW(&wc);
  49.     hWindowa = CreateWindowW(wc.lpszClassName, L"PathSaver", WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX | WS_VISIBLE, 260, 134, 288, 104, 0, 0, hInstance, 0);  //WS_OVERLAPPEDWINDOW //WS_OVERLAPPED //WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX | WS_VISIBLE
  50.     WNDCLASSW wcB = {0};
  51.     wcB.lpszClassName = L"EditText";
  52.     wcB.hInstance = hInstance;
  53.     wcB.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  54.     wcB.lpfnWndProc = WndProcB;
  55.     wcB.hCursor = LoadCursor(0, IDC_ARROW);
  56.     RegisterClassW(&wcB);
  57.     hWindowb = CreateWindowW(wcB.lpszClassName, L"Liste d'exclusion", WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX, 360, 234, 500, 212, 0, 0, hInstance, 0);
  58.     while(GetMessage(&msg, NULL, 0, 0))
  59.     {
  60.         TranslateMessage(&msg);
  61.         DispatchMessage(&msg);
  62.         if(showWindowB)
  63.         {
  64.             ShowWindow(hWindowb, nCmdShow);
  65.         }
  66.         else if(!showWindowB)
  67.         {
  68.             ShowWindow(hWindowb, SW_HIDE);
  69.         }
  70.     }
  71.     return (int) msg.wParam;
  72. }
  73.  
  74. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  75. {
  76.     static wchar_t *dest =  L"Destination :";
  77.     std::string desty = "/user/";
  78.     std::string buildy = "/home";
  79.     dwRet = GetCurrentDirectory(MAX_PATH, NPath);
  80.     username_len = UNLEN+1;
  81.     static wchar_t *pathy = L"C:\\";
  82.     static wchar_t *info = L"Selectionner le dossier pour la sauvegarde";
  83.     switch(msg)
  84.     {
  85.     case WM_KEYDOWN:
  86.         if (wParam == VK_ESCAPE)
  87.         {
  88.             int ret = MessageBoxW(hwnd, L"\x210tes vous sur de vouloir quitter?",
  89.             L"Message", MB_OKCANCEL);                            
  90.             if (ret == IDOK)
  91.             {
  92.                 SendMessage(hwnd, WM_CLOSE, 0, 0);
  93.             }
  94.         }    
  95.         break;
  96.     case WM_CREATE:
  97.         CenterWindow(hwnd);
  98.         hwndText = CreateWindowW(L"Static", dest, WS_CHILD | WS_VISIBLE | SS_LEFT | WS_CLIPSIBLINGS, 10, 12, 90, 30, hwnd, (HMENU)IDM_LABEL, GetModuleHandle(NULL), NULL);
  99.         hwndEdit = CreateWindowW(L"Edit", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | WS_CLIPSIBLINGS, 100, 9, 142, 23, hwnd, (HMENU)ID_EDIT, GetModuleHandle(NULL), NULL);
  100.         hwndButtonB = CreateWindowW(L"Button", L"", WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS, 250, 9, 21, 21, hwnd, (HMENU)ID_BUTTONB, GetModuleHandle(NULL), NULL);
  101.         hwndButtonC = CreateWindowW(L"Button", L"Liste d'exclusion", WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS, 10, 41, 125, 21, hwnd, (HMENU)ID_BUTTONC, GetModuleHandle(NULL), NULL);
  102.         hwndButton = CreateWindowW(L"Button", L"Generer", WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS, 172, 41, 62, 21, hwnd, (HMENU)ID_BUTTON, GetModuleHandle(NULL), NULL);
  103.         GetUserName(username, &username_len);
  104.         desty = desty + lpwstrToString(username) + buildy;
  105.         SetWindowText(hwndEdit, stringToLPWSTR(desty));
  106.         break;  
  107.     case WM_COMMAND:
  108.     {
  109.         switch(LOWORD(wParam))
  110.         {
  111.             case ID_BUTTON:
  112.                 TCHAR buff[1024];
  113.                 GetWindowTextW(hwndEdit, buff, 1024);
  114.                 file<<lpwstrToString(buff);
  115.                 file<<"\n";
  116.                 file.close();
  117.                 break;
  118.             case ID_BUTTONB:
  119.                 if(BrowseFolders(hwnd, (LPSTR)pathy, (LPSTR)info))
  120.                 {
  121.                     SetWindowText(hwndEdit, stringToLPWSTR(newPath));
  122.                     RedrawWindow(hwndEdit, NULL, NULL, RDW_ERASE);
  123.                 }
  124.                 break;
  125.             case ID_BUTTONC:
  126.                 showWindowB = true;
  127.                 SetActiveWindow(hWindowb);
  128.                 break;
  129.         }
  130.         break;
  131.     }
  132.     case WM_DESTROY:  
  133.         PostQuitMessage(0);
  134.         break;
  135.     }
  136.     return DefWindowProcW(hwnd, msg, wParam, lParam);
  137. }
  138.  
  139. LRESULT CALLBACK WndProcB(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  140. {
  141.     std::string excCont;
  142.     switch(msg)
  143.     {
  144.         case WM_KEYDOWN:
  145.             if (wParam == VK_ESCAPE)
  146.             {
  147.                 SendMessage(hwnd, WM_CLOSE, 0, 0);
  148.             }    
  149.             break;
  150.         case WM_CREATE:
  151.             CenterWindow(hwnd);
  152.             hwndEditB = CreateWindowW(L"Edit", NULL, ES_MULTILINE | ES_WANTRETURN | WS_HSCROLL | WS_VSCROLL | WS_CHILD | WS_VISIBLE | WS_BORDER | WS_CLIPSIBLINGS, 11, 10, 472, 145, hwnd, (HMENU)ID_EDITB, GetModuleHandle(NULL), NULL); //ES_AUTOVSCROLL //ES_WANTRETURN
  153.             hwndButtonD = CreateWindowW(L"Button", L"Sauvegarder", WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS, 195, 158, 95, 21, hwnd, (HMENU)ID_BUTTOND, GetModuleHandle(NULL), NULL);
  154.             displayExcludeList(hwndEditB, excCont);
  155.             break;
  156.         case WM_COMMAND:
  157.             if(LOWORD(wParam)==ID_BUTTOND)
  158.             {
  159.                 std::ofstream fileB("excludelist.txt", std::ios::out | std::ios::trunc);//std::ios::trunc //std::ios_base::app
  160.                 TCHAR buffy[1024];
  161.                 GetWindowTextW(hwndEditB, buffy, 1024);
  162.                 std::string excludelist = lpwstrToString(buffy);
  163.                 //excludelist.erase(std::remove(excludelist.begin(), excludelist.end(), '\n'), excludelist.end());
  164.                 fileB<<excludelist;
  165.                 fileB<<"\r\n";
  166.                 fileB.close();
  167.                 SendMessage(hwnd, WM_CLOSE, 0, 0);
  168.             }
  169.             break;
  170.         case WM_CLOSE:
  171.             showWindowB = false;
  172.             SetActiveWindow(hWindowa);
  173.             return 0;
  174.             break;
  175.         default:
  176.             break;
  177.     }
  178.     return DefWindowProcW(hwnd, msg, wParam, lParam);
  179. }
  180.  
  181. void CenterWindow(HWND hwnd)
  182. {
  183.     RECT rc = {0};
  184.     GetWindowRect(hwnd, &rc);
  185.     int win_w = rc.right - rc.left;
  186.     int win_h = rc.bottom - rc.top;
  187.     int screen_w = GetSystemMetrics(SM_CXSCREEN);
  188.     int screen_h = GetSystemMetrics(SM_CYSCREEN);
  189.     SetWindowPos(hwnd, HWND_TOP, (screen_w - win_w)/2, (screen_h - win_h)/2, 0, 0, SWP_NOSIZE);
  190. }
  191.  
  192. std::string lpwstrToString(LPWSTR input)
  193. {
  194.     int cSize = WideCharToMultiByte (CP_ACP, 0, input, wcslen(input), NULL, 0, NULL, NULL);
  195.     std::string output(static_cast<size_t>(cSize), '\0');
  196.     WideCharToMultiByte (CP_ACP, 0, input, wcslen(input), reinterpret_cast<char*>(&output[0]), cSize, NULL, NULL);
  197.     return output;
  198. }
  199.  
  200. LPWSTR stringToLPWSTR(const std::string& instr)
  201. {
  202.     int bufferlen = ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), NULL, 0);
  203.     LPWSTR widestr = new WCHAR[bufferlen + 1];
  204.     ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), widestr, bufferlen);
  205.     widestr[bufferlen] = 0;
  206.     return widestr;
  207. }
  208.  
  209. int CALLBACK BrowseForFolderCallback(HWND hwnd,UINT uMsg,LPARAM lp, LPARAM pData)
  210. {
  211.     TCHAR szPath[MAX_PATH];
  212.     switch(uMsg)
  213.     {
  214.         case BFFM_INITIALIZED:
  215.             SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData);
  216.             break;
  217.         case BFFM_SELCHANGED:
  218.             if (SHGetPathFromIDList((LPITEMIDLIST) lp ,szPath))
  219.             {
  220.                 SendMessage(hwnd, BFFM_SETSTATUSTEXT,0,(LPARAM)szPath);
  221.             }
  222.             break;
  223.     }
  224.     return 0;
  225. }
  226.  
  227. BOOL BrowseFolders(HWND hwnd, LPSTR lpszFolder, LPSTR lpszTitle)
  228. {
  229.     char szPath[MAX_PATH + 1];
  230.     BROWSEINFO bi;
  231.     LPITEMIDLIST pidl;
  232.     BOOL bResult = FALSE;
  233.     LPMALLOC pMalloc;
  234.     if (SUCCEEDED(SHGetMalloc(&pMalloc)))
  235.     {
  236.         bi.hwndOwner = hwnd;
  237.         bi.pidlRoot = NULL;
  238.         bi.pszDisplayName = NULL;
  239.         bi.lpszTitle = (LPWSTR)lpszTitle;
  240.         bi.ulFlags = BIF_STATUSTEXT;
  241.         bi.lpfn = BrowseForFolderCallback;
  242.         bi.lParam = (LPARAM)lpszFolder;    
  243.         pidl = SHBrowseForFolder(&bi);
  244.         if (pidl)
  245.         {
  246.             if (SHGetPathFromIDList(pidl,(LPWSTR)szPath))
  247.             {
  248.                 bResult = TRUE;
  249.                 OutputDebugString((LPWSTR)szPath);
  250.                 newPath = lpwstrToString((LPWSTR)szPath);
  251.             }
  252.            pMalloc->Free(pidl);
  253.            pMalloc->Release();                     
  254.         }
  255.     }
  256.     return bResult;
  257. }
  258.  
  259. void displayExcludeList(HWND hwndEdit, std::string passedString)
  260. {
  261.     std::ifstream inFileB("excludelist.txt");//, std::ios::in
  262.     while(getline(inFileB, passedString, '\n'))
  263.     {
  264.         OutputDebugString(stringToLPWSTR(passedString));
  265.         int outLength = GetWindowTextLength(hwndEdit) + lstrlen(stringToLPWSTR(passedString)) + 1;
  266.         TCHAR *buf = (TCHAR *) GlobalAlloc(GPTR, outLength *sizeof(TCHAR));
  267.         GetWindowText(hwndEdit, buf, outLength);
  268.         _tcscat_s(buf, outLength, stringToLPWSTR(passedString));
  269.         _tcscat_s(buf, outLength+1, (TCHAR *)"\n");
  270.         SetWindowText(hwndEdit, buf);
  271.         GlobalFree(buf);   
  272.     }
  273.     inFileB.close();
  274. }
Add Comment
Please, Sign In to add comment