Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <string>
- #include <commctrl.h>
- #include <fstream>
- #include <sstream>
- #include <Lmcons.h>
- #include <shlobj.h>
- #include <algorithm>
- #include <tchar.h>
- #define ID_BUTTON 1
- #define ID_EDIT 2
- #define ID_BUTTONB 3
- #define ID_BUTTONC 4
- #define IDM_ABOUT 5
- #define IDM_LABEL 6
- #define ID_EDITB 7
- #define ID_BUTTOND 8
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- LRESULT CALLBACK WndProcB(HWND, UINT, WPARAM, LPARAM);
- DWORD dwRet;
- TCHAR NPath[MAX_PATH];
- TCHAR username[UNLEN+1];
- DWORD username_len;
- bool showWindowB;
- HWND hWindowa, hWindowb, hwndText, hwndEdit, hwndLabel, hwndButton, hwndButtonB, hwndButtonC, hwndEditB, hwndButtonD;
- void CenterWindow(HWND);
- std::string newPath;
- std::string lpwstrToString(LPWSTR var);
- LPWSTR stringToLPWSTR(const std::string& instr);
- int CALLBACK BrowseForFolderCallback(HWND hwnd,UINT uMsg,LPARAM lp, LPARAM pData);
- BOOL BrowseFolders(HWND hwnd, LPSTR lpszFolder, LPSTR lpszTitle);
- void displayExcludeList(HWND hwndEdit, std::string passedString);
- std::ofstream file("backuprc", std::ios::out | std::ios_base::app);
- //CreateProcessW(xx,command,x...)
- int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
- {
- showWindowB = false;
- MSG msg;
- WNDCLASSW wc = {0};
- wc.lpszClassName = L"Test101";
- wc.hInstance = hInstance;
- wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
- wc.lpfnWndProc = WndProc;
- wc.hCursor = LoadCursor(0, IDC_ARROW);
- RegisterClassW(&wc);
- 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
- WNDCLASSW wcB = {0};
- wcB.lpszClassName = L"EditText";
- wcB.hInstance = hInstance;
- wcB.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
- wcB.lpfnWndProc = WndProcB;
- wcB.hCursor = LoadCursor(0, IDC_ARROW);
- RegisterClassW(&wcB);
- hWindowb = CreateWindowW(wcB.lpszClassName, L"Liste d'exclusion", WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX, 360, 234, 500, 212, 0, 0, hInstance, 0);
- while(GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- if(showWindowB)
- {
- ShowWindow(hWindowb, nCmdShow);
- }
- else if(!showWindowB)
- {
- ShowWindow(hWindowb, SW_HIDE);
- }
- }
- return (int) msg.wParam;
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- static wchar_t *dest = L"Destination :";
- std::string desty = "/user/";
- std::string buildy = "/home";
- dwRet = GetCurrentDirectory(MAX_PATH, NPath);
- username_len = UNLEN+1;
- static wchar_t *pathy = L"C:\\";
- static wchar_t *info = L"Selectionner le dossier pour la sauvegarde";
- switch(msg)
- {
- case WM_KEYDOWN:
- if (wParam == VK_ESCAPE)
- {
- int ret = MessageBoxW(hwnd, L"\x210tes vous sur de vouloir quitter?",
- L"Message", MB_OKCANCEL);
- if (ret == IDOK)
- {
- SendMessage(hwnd, WM_CLOSE, 0, 0);
- }
- }
- break;
- case WM_CREATE:
- CenterWindow(hwnd);
- hwndText = CreateWindowW(L"Static", dest, WS_CHILD | WS_VISIBLE | SS_LEFT | WS_CLIPSIBLINGS, 10, 12, 90, 30, hwnd, (HMENU)IDM_LABEL, GetModuleHandle(NULL), NULL);
- hwndEdit = CreateWindowW(L"Edit", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | WS_CLIPSIBLINGS, 100, 9, 142, 23, hwnd, (HMENU)ID_EDIT, GetModuleHandle(NULL), NULL);
- hwndButtonB = CreateWindowW(L"Button", L"", WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS, 250, 9, 21, 21, hwnd, (HMENU)ID_BUTTONB, GetModuleHandle(NULL), NULL);
- 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);
- hwndButton = CreateWindowW(L"Button", L"Generer", WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS, 172, 41, 62, 21, hwnd, (HMENU)ID_BUTTON, GetModuleHandle(NULL), NULL);
- GetUserName(username, &username_len);
- desty = desty + lpwstrToString(username) + buildy;
- SetWindowText(hwndEdit, stringToLPWSTR(desty));
- break;
- case WM_COMMAND:
- {
- switch(LOWORD(wParam))
- {
- case ID_BUTTON:
- TCHAR buff[1024];
- GetWindowTextW(hwndEdit, buff, 1024);
- file<<lpwstrToString(buff);
- file<<"\n";
- file.close();
- break;
- case ID_BUTTONB:
- if(BrowseFolders(hwnd, (LPSTR)pathy, (LPSTR)info))
- {
- SetWindowText(hwndEdit, stringToLPWSTR(newPath));
- RedrawWindow(hwndEdit, NULL, NULL, RDW_ERASE);
- }
- break;
- case ID_BUTTONC:
- showWindowB = true;
- SetActiveWindow(hWindowb);
- break;
- }
- break;
- }
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- }
- return DefWindowProcW(hwnd, msg, wParam, lParam);
- }
- LRESULT CALLBACK WndProcB(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- std::string excCont;
- switch(msg)
- {
- case WM_KEYDOWN:
- if (wParam == VK_ESCAPE)
- {
- SendMessage(hwnd, WM_CLOSE, 0, 0);
- }
- break;
- case WM_CREATE:
- CenterWindow(hwnd);
- 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
- hwndButtonD = CreateWindowW(L"Button", L"Sauvegarder", WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS, 195, 158, 95, 21, hwnd, (HMENU)ID_BUTTOND, GetModuleHandle(NULL), NULL);
- displayExcludeList(hwndEditB, excCont);
- break;
- case WM_COMMAND:
- if(LOWORD(wParam)==ID_BUTTOND)
- {
- std::ofstream fileB("excludelist.txt", std::ios::out | std::ios::trunc);//std::ios::trunc //std::ios_base::app
- TCHAR buffy[1024];
- GetWindowTextW(hwndEditB, buffy, 1024);
- std::string excludelist = lpwstrToString(buffy);
- //excludelist.erase(std::remove(excludelist.begin(), excludelist.end(), '\n'), excludelist.end());
- fileB<<excludelist;
- fileB<<"\r\n";
- fileB.close();
- SendMessage(hwnd, WM_CLOSE, 0, 0);
- }
- break;
- case WM_CLOSE:
- showWindowB = false;
- SetActiveWindow(hWindowa);
- return 0;
- break;
- default:
- break;
- }
- return DefWindowProcW(hwnd, msg, wParam, lParam);
- }
- void CenterWindow(HWND hwnd)
- {
- RECT rc = {0};
- GetWindowRect(hwnd, &rc);
- int win_w = rc.right - rc.left;
- int win_h = rc.bottom - rc.top;
- int screen_w = GetSystemMetrics(SM_CXSCREEN);
- int screen_h = GetSystemMetrics(SM_CYSCREEN);
- SetWindowPos(hwnd, HWND_TOP, (screen_w - win_w)/2, (screen_h - win_h)/2, 0, 0, SWP_NOSIZE);
- }
- std::string lpwstrToString(LPWSTR input)
- {
- int cSize = WideCharToMultiByte (CP_ACP, 0, input, wcslen(input), NULL, 0, NULL, NULL);
- std::string output(static_cast<size_t>(cSize), '\0');
- WideCharToMultiByte (CP_ACP, 0, input, wcslen(input), reinterpret_cast<char*>(&output[0]), cSize, NULL, NULL);
- return output;
- }
- LPWSTR stringToLPWSTR(const std::string& instr)
- {
- int bufferlen = ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), NULL, 0);
- LPWSTR widestr = new WCHAR[bufferlen + 1];
- ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), widestr, bufferlen);
- widestr[bufferlen] = 0;
- return widestr;
- }
- int CALLBACK BrowseForFolderCallback(HWND hwnd,UINT uMsg,LPARAM lp, LPARAM pData)
- {
- TCHAR szPath[MAX_PATH];
- switch(uMsg)
- {
- case BFFM_INITIALIZED:
- SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData);
- break;
- case BFFM_SELCHANGED:
- if (SHGetPathFromIDList((LPITEMIDLIST) lp ,szPath))
- {
- SendMessage(hwnd, BFFM_SETSTATUSTEXT,0,(LPARAM)szPath);
- }
- break;
- }
- return 0;
- }
- BOOL BrowseFolders(HWND hwnd, LPSTR lpszFolder, LPSTR lpszTitle)
- {
- char szPath[MAX_PATH + 1];
- BROWSEINFO bi;
- LPITEMIDLIST pidl;
- BOOL bResult = FALSE;
- LPMALLOC pMalloc;
- if (SUCCEEDED(SHGetMalloc(&pMalloc)))
- {
- bi.hwndOwner = hwnd;
- bi.pidlRoot = NULL;
- bi.pszDisplayName = NULL;
- bi.lpszTitle = (LPWSTR)lpszTitle;
- bi.ulFlags = BIF_STATUSTEXT;
- bi.lpfn = BrowseForFolderCallback;
- bi.lParam = (LPARAM)lpszFolder;
- pidl = SHBrowseForFolder(&bi);
- if (pidl)
- {
- if (SHGetPathFromIDList(pidl,(LPWSTR)szPath))
- {
- bResult = TRUE;
- OutputDebugString((LPWSTR)szPath);
- newPath = lpwstrToString((LPWSTR)szPath);
- }
- pMalloc->Free(pidl);
- pMalloc->Release();
- }
- }
- return bResult;
- }
- void displayExcludeList(HWND hwndEdit, std::string passedString)
- {
- std::ifstream inFileB("excludelist.txt");//, std::ios::in
- while(getline(inFileB, passedString, '\n'))
- {
- OutputDebugString(stringToLPWSTR(passedString));
- int outLength = GetWindowTextLength(hwndEdit) + lstrlen(stringToLPWSTR(passedString)) + 1;
- TCHAR *buf = (TCHAR *) GlobalAlloc(GPTR, outLength *sizeof(TCHAR));
- GetWindowText(hwndEdit, buf, outLength);
- _tcscat_s(buf, outLength, stringToLPWSTR(passedString));
- _tcscat_s(buf, outLength+1, (TCHAR *)"\n");
- SetWindowText(hwndEdit, buf);
- GlobalFree(buf);
- }
- inFileB.close();
- }
Add Comment
Please, Sign In to add comment