Combreal icon

LineCounter

Combreal | PRO | 05/26/16 02:19:45 PM UTC | 0 ⭐ | 10671 👁️ | Never ⏰ | []
C++ (WinAPI) |

0 B

|

None

|

0 👍

/

0 👎

#include <windows.h>
#include <fstream>
#include <sstream> 
#include <string>
#include <vector>
#include <commctrl.h>
#include "resource.h"
 
#define ID_BUTTON 1
#define ID_EDIT 2
 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static HWND hwndEdit;
void CenterWindow(HWND);
void OpenDialog(HWND);
void LoadFileLinesNumber(LPSTR, std::string passed_string, std::vector<std::string>& passed_vector);
 
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
        MSG  msg;    
        WNDCLASSW wc = {0};
        wc.lpszClassName = L"LC";
        wc.hInstance     = hInstance;
        wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
        wc.lpfnWndProc   = WndProc;
        wc.hIcon         = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
        wc.hCursor       = LoadCursor(0, IDC_ARROW);
        RegisterClassW(&wc);
        CreateWindowW(wc.lpszClassName, L"LCo", WS_OVERLAPPEDWINDOW|WS_VISIBLE, 230, 134, 230, 134, 0, 0, hInstance, 0);  
        while (GetMessage(&msg, NULL, 0, 0)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return (int) msg.wParam;
}
 
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    static wchar_t *lineNb =  L"Number of lines in the file(s) :";
    HWND hwndButton;
    switch(msg) {
    case WM_KEYDOWN:
        if (wParam == VK_ESCAPE) {
            int ret = MessageBoxW(hwnd, L"Are you sure to quit?", 
                L"Message", MB_OKCANCEL);                            
            if (ret == IDOK) {
                SendMessage(hwnd, WM_CLOSE, 0, 0);
            }
        }     
        break; 
    case WM_CREATE: 
        hwndButton=CreateWindowW(L"Button", L"Open file(s)", WS_VISIBLE|WS_CHILD , 63, 12, 82, 23, hwnd, (HMENU)ID_BUTTON, NULL, NULL);
        CenterWindow(hwnd);
        CreateWindowW(L"Static", lineNb, WS_CHILD|WS_VISIBLE|SS_LEFT, 10, 40, 197, 30, hwnd, (HMENU)1, NULL, NULL);
        hwndEdit=CreateWindowW(L"Edit", NULL, WS_CHILD|WS_VISIBLE|WS_BORDER, 78, 60, 55, 21, hwnd, (HMENU)ID_EDIT, NULL, NULL);
        break;   
    case WM_COMMAND:
        if (LOWORD(wParam)==ID_BUTTON) {
            OpenDialog(hwnd);
        }
        break;
    case WM_DESTROY:  
        PostQuitMessage(0);
        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);
}
 
void OpenDialog(HWND hwnd) {
    OPENFILENAME ofn;
    TCHAR szFile[MAX_PATH];
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.lpstrFile = szFile;
    ofn.lpstrFile[0] = '\0';
    ofn.hwndOwner = hwnd;
    ofn.nMaxFile = sizeof(szFile);
    ofn.lpstrFilter = TEXT("Text Files\0*.txt\0Any File\0*.*\0");
    ofn.nFilterIndex = 1;
    ofn.lpstrInitialDir = NULL;
    ofn.lpstrFileTitle = NULL;
    ofn.lpstrTitle   = "Select file(s)";
    ofn.Flags = OFN_ALLOWMULTISELECT|OFN_EXPLORER;
    if(GetOpenFileName(&ofn)) {
        std::vector<std::string> fullPath;
        std::string unicFile = ofn.lpstrFile;
        char* str = ofn.lpstrFile;
        while(*str) {
            std::string filename = str;
            str += (filename.length()+1);
            fullPath.push_back(str);
        }
        LoadFileLinesNumber(ofn.lpstrFile, unicFile, fullPath);
    }
}
 
void LoadFileLinesNumber(LPSTR file, std::string passed_string, std::vector<std::string>& passed_vector) {
    if(passed_vector.size() > 1) {
        size_t linesCount = 0;
        for(size_t k=0, size=passed_vector.size();k<size;k++) {
            if(passed_vector.at(k) != "") {
                std::string path=file;
                path+='\\';
                path+=passed_vector.at(k);
                std::ifstream aFile(path.c_str());   
                std::string line;
                while(std::getline(aFile , line)) {
                    ++linesCount;
                }
                aFile.close();
            }
        }
        std::stringstream ss;
        ss<<linesCount;
        std::string str = ss.str();
        SetWindowText(hwndEdit, str.c_str());
    }
    else if(passed_vector.size() == 1) {
        std::ifstream aFile(passed_string.c_str());   
        size_t linesCount = 0;
        std::string line;
        while(std::getline(aFile , line)) {
            ++linesCount;
        }
        aFile.close();
        std::stringstream ss;
        ss<<linesCount;
        std::string str = ss.str();
        SetWindowText(hwndEdit, str.c_str());
    }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎