Advertisement
Combreal

LineCounter

May 26th, 2016
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <windows.h>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>
  6. #include <commctrl.h>
  7. #include "resource.h"
  8.  
  9. #define ID_BUTTON 1
  10. #define ID_EDIT 2
  11.  
  12. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  13. static HWND hwndEdit;
  14. void CenterWindow(HWND);
  15. void OpenDialog(HWND);
  16. void LoadFileLinesNumber(LPSTR, std::string passed_string, std::vector<std::string>& passed_vector);
  17.  
  18. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
  19.         MSG  msg;    
  20.         WNDCLASSW wc = {0};
  21.         wc.lpszClassName = L"LC";
  22.         wc.hInstance     = hInstance;
  23.         wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  24.         wc.lpfnWndProc   = WndProc;
  25.         wc.hIcon         = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
  26.         wc.hCursor       = LoadCursor(0, IDC_ARROW);
  27.         RegisterClassW(&wc);
  28.         CreateWindowW(wc.lpszClassName, L"LCo", WS_OVERLAPPEDWINDOW|WS_VISIBLE, 230, 134, 230, 134, 0, 0, hInstance, 0);  
  29.         while (GetMessage(&msg, NULL, 0, 0)) {
  30.             TranslateMessage(&msg);
  31.             DispatchMessage(&msg);
  32.         }
  33.         return (int) msg.wParam;
  34. }
  35.  
  36. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  37.     static wchar_t *lineNb =  L"Number of lines in the file(s) :";
  38.     HWND hwndButton;
  39.     switch(msg) {
  40.     case WM_KEYDOWN:
  41.         if (wParam == VK_ESCAPE) {
  42.             int ret = MessageBoxW(hwnd, L"Are you sure to quit?",
  43.                 L"Message", MB_OKCANCEL);                            
  44.             if (ret == IDOK) {
  45.                 SendMessage(hwnd, WM_CLOSE, 0, 0);
  46.             }
  47.         }    
  48.         break;
  49.     case WM_CREATE:
  50.         hwndButton=CreateWindowW(L"Button", L"Open file(s)", WS_VISIBLE|WS_CHILD , 63, 12, 82, 23, hwnd, (HMENU)ID_BUTTON, NULL, NULL);
  51.         CenterWindow(hwnd);
  52.         CreateWindowW(L"Static", lineNb, WS_CHILD|WS_VISIBLE|SS_LEFT, 10, 40, 197, 30, hwnd, (HMENU)1, NULL, NULL);
  53.         hwndEdit=CreateWindowW(L"Edit", NULL, WS_CHILD|WS_VISIBLE|WS_BORDER, 78, 60, 55, 21, hwnd, (HMENU)ID_EDIT, NULL, NULL);
  54.         break;  
  55.     case WM_COMMAND:
  56.         if (LOWORD(wParam)==ID_BUTTON) {
  57.             OpenDialog(hwnd);
  58.         }
  59.         break;
  60.     case WM_DESTROY:  
  61.         PostQuitMessage(0);
  62.         break;
  63.     }
  64.     return DefWindowProcW(hwnd, msg, wParam, lParam);
  65. }
  66.  
  67. void CenterWindow(HWND hwnd) {
  68.     RECT rc = {0};
  69.     GetWindowRect(hwnd, &rc);
  70.     int win_w = rc.right - rc.left;
  71.     int win_h = rc.bottom - rc.top;
  72.     int screen_w = GetSystemMetrics(SM_CXSCREEN);
  73.     int screen_h = GetSystemMetrics(SM_CYSCREEN);
  74.     SetWindowPos(hwnd, HWND_TOP, (screen_w - win_w)/2, (screen_h - win_h)/2, 0, 0, SWP_NOSIZE);
  75. }
  76.  
  77. void OpenDialog(HWND hwnd) {
  78.     OPENFILENAME ofn;
  79.     TCHAR szFile[MAX_PATH];
  80.     ZeroMemory(&ofn, sizeof(ofn));
  81.     ofn.lStructSize = sizeof(ofn);
  82.     ofn.lpstrFile = szFile;
  83.     ofn.lpstrFile[0] = '\0';
  84.     ofn.hwndOwner = hwnd;
  85.     ofn.nMaxFile = sizeof(szFile);
  86.     ofn.lpstrFilter = TEXT("Text Files\0*.txt\0Any File\0*.*\0");
  87.     ofn.nFilterIndex = 1;
  88.     ofn.lpstrInitialDir = NULL;
  89.     ofn.lpstrFileTitle = NULL;
  90.     ofn.lpstrTitle   = "Select file(s)";
  91.     ofn.Flags = OFN_ALLOWMULTISELECT|OFN_EXPLORER;
  92.     if(GetOpenFileName(&ofn)) {
  93.         std::vector<std::string> fullPath;
  94.         std::string unicFile = ofn.lpstrFile;
  95.         char* str = ofn.lpstrFile;
  96.         while(*str) {
  97.             std::string filename = str;
  98.             str += (filename.length()+1);
  99.             fullPath.push_back(str);
  100.         }
  101.         LoadFileLinesNumber(ofn.lpstrFile, unicFile, fullPath);
  102.     }
  103. }
  104.  
  105. void LoadFileLinesNumber(LPSTR file, std::string passed_string, std::vector<std::string>& passed_vector) {
  106.     if(passed_vector.size() > 1) {
  107.         size_t linesCount = 0;
  108.         for(size_t k=0, size=passed_vector.size();k<size;k++) {
  109.             if(passed_vector.at(k) != "") {
  110.                 std::string path=file;
  111.                 path+='\\';
  112.                 path+=passed_vector.at(k);
  113.                 std::ifstream aFile(path.c_str());  
  114.                 std::string line;
  115.                 while(std::getline(aFile , line)) {
  116.                     ++linesCount;
  117.                 }
  118.                 aFile.close();
  119.             }
  120.         }
  121.         std::stringstream ss;
  122.         ss<<linesCount;
  123.         std::string str = ss.str();
  124.         SetWindowText(hwndEdit, str.c_str());
  125.     }
  126.     else if(passed_vector.size() == 1) {
  127.         std::ifstream aFile(passed_string.c_str());  
  128.         size_t linesCount = 0;
  129.         std::string line;
  130.         while(std::getline(aFile , line)) {
  131.             ++linesCount;
  132.         }
  133.         aFile.close();
  134.         std::stringstream ss;
  135.         ss<<linesCount;
  136.         std::string str = ss.str();
  137.         SetWindowText(hwndEdit, str.c_str());
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement