Advertisement
Guest User

main.cpp

a guest
Oct 18th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <tchar.h>
  4. #include <windows.h>
  5. #include <windowsx.h>
  6. #include <string.h>
  7.  
  8. #include "newmyfiles.h"
  9.  
  10. #define VK_C 0x43
  11. #define VK_Q 0x51
  12.  
  13. /* use _CRT_SECURE_NO_WARNINGS !!! */
  14.  
  15. const TCHAR szWinClass[] = _T("Win32SampleApp");
  16. const TCHAR szWinName[] = _T("Win32SampleWindow");
  17. HWND hwnd;                      /* This is the handle for our window */
  18. HBRUSH hBrush;                  /* Current brush */
  19. HPEN hPen;                      /* Current pen */
  20. int hRGB;                       /* Current RGB */
  21. int n = 3;                      /* Grid size */
  22. const char * fname = "my.ini";  /* .ini file name */
  23. bool** pole;
  24. myfiles * mf;
  25.  
  26. bool splitnext(char* &line, char * &name, char * &value) {
  27.     if (line == NULL || strlen(line) <= 0)
  28.         return 0;
  29.  
  30.     char * end;
  31.     while ((end = strchr(line, '\n')) != NULL || strlen(line) > 0 && (end = line + strlen(line))) { // search '\n' or end of file
  32.         char * fext = new char[end - line + 1];
  33.         strncpy(fext, line, end - line);
  34.         fext[end - line] = '\0';
  35.  
  36.         char * r;
  37.         if ((r = strchr(fext, '=')) != NULL) { // search ' = ' and split by name and value
  38.             int len = (r - fext);
  39.             name = new char[len + 1];
  40.             value = new char[strlen(fext) - len];
  41.  
  42.             strncpy(name, fext, len);
  43.             strncpy(value, r + 1, strlen(fext) - len - 1);
  44.  
  45.             name[len] = '\0';
  46.             value[strlen(fext) - len - 1] = '\0';
  47.  
  48.             line = end + 1;
  49.             free(fext);
  50.             return true;
  51.         }
  52.  
  53.         line = end + 1;
  54.         free(fext);
  55.     }
  56.  
  57.     return false;
  58. }
  59.  
  60. void RunNotepad(void)
  61. {
  62.     STARTUPINFO sInfo;
  63.     PROCESS_INFORMATION pInfo;
  64.     ZeroMemory(&sInfo, sizeof(STARTUPINFO));
  65.     CreateProcess(_T("C:\\Windows\\Notepad.exe"), NULL, NULL, NULL, FALSE, 0, NULL, NULL, &sInfo, &pInfo);
  66. }
  67.  
  68. void changeBackground(HWND hwnd)
  69. {
  70.     DeleteObject(hBrush);
  71.     hRGB = RGB(rand() % 255, rand() % 255, rand() % 255);
  72.     hBrush = (HBRUSH)CreateSolidBrush(hRGB);
  73.     SetClassLongPtr(hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)hBrush);
  74.     InvalidateRect(hwnd, NULL, TRUE); //redraw window
  75. }
  76.  
  77. void Save(HWND hwnd) {
  78.     WINDOWPLACEMENT nowPosition;
  79.     nowPosition.length = sizeof(WINDOWPLACEMENT); /* */
  80.     GetWindowPlacement(hwnd, &nowPosition);
  81.    
  82.     char *buffer = new char[100];
  83.     size_t len = snprintf(buffer, 100, "width=%d\nheight=%d\nposx=%d\nposy=%d\nRGB=%d\nshowCmd=%d", nowPosition.rcNormalPosition.right-nowPosition.rcNormalPosition.left, nowPosition.rcNormalPosition.bottom-nowPosition.rcNormalPosition.top, nowPosition.rcNormalPosition.left, nowPosition.rcNormalPosition.top, hRGB, nowPosition.showCmd);
  84.  
  85.     mf->write(buffer, len);
  86.     free(buffer);
  87. }
  88.  
  89. LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  90. {
  91.     HDC hdc;
  92.     PAINTSTRUCT paintstruct;
  93.     RECT c;
  94.     int x, y;
  95.  
  96.     switch (message)
  97.     {
  98.     case WM_SIZE:
  99.         InvalidateRect(hwnd, NULL, TRUE);
  100.         break;
  101.  
  102.     case WM_LBUTTONUP:
  103.         GetClientRect(hwnd, &c);
  104.         x = GET_X_LPARAM(lParam) / (c.right / n);
  105.         y = GET_Y_LPARAM(lParam) / (c.bottom / n);
  106.         if (!pole[x][y]) {
  107.             pole[x][y] = true;
  108.             hdc = GetDC(hwnd);
  109.             SelectObject(hdc, hPen);
  110.             SelectObject(hdc, (HGDIOBJ)GetStockObject(NULL_BRUSH));
  111.             Ellipse(hdc, x*c.right / n + 5, y*c.bottom / n + 5, (x + 1)*c.right / n - 5, (y + 1)*c.bottom / n - 5);
  112.             SelectObject(hdc, hPen);
  113.             ReleaseDC(hwnd, hdc);
  114.         }
  115.         break;
  116.  
  117.     case WM_PAINT:
  118.         hdc = BeginPaint(hwnd, &paintstruct);
  119.         SelectObject(hdc, hPen);
  120.         for (int i = 1; i < n; i++)
  121.         {
  122.             MoveToEx(hdc, 0, paintstruct.rcPaint.bottom / n * i, NULL);
  123.             LineTo(hdc, paintstruct.rcPaint.right, paintstruct.rcPaint.bottom / n * i);
  124.  
  125.             MoveToEx(hdc, paintstruct.rcPaint.right / n * i, 0, NULL);
  126.             LineTo(hdc, paintstruct.rcPaint.right / n * i, paintstruct.rcPaint.bottom);
  127.         }
  128.         SelectObject(hdc, (HGDIOBJ)GetStockObject(NULL_BRUSH));
  129.         SelectObject(hdc, hPen);
  130.         for (int i = 0; i < n; i++)
  131.             for (int j = 0; j < n; j++)
  132.                 if (pole[i][j])
  133.                     Ellipse(hdc, i*paintstruct.rcPaint.right / n + 5, j*paintstruct.rcPaint.bottom / n + 5, (i + 1)*paintstruct.rcPaint.right / n - 5, (j + 1)*paintstruct.rcPaint.bottom / n - 5);
  134.         ReleaseDC(hwnd, hdc);
  135.         EndPaint(hwnd, &paintstruct);
  136.         break;
  137.  
  138.     case WM_HOTKEY:
  139.         switch (wParam) {
  140.         case 1: //Shift+C
  141.             RunNotepad();
  142.             break;
  143.         case 2: //Ctrl+Q
  144.             DestroyWindow(hwnd);
  145.         }
  146.         break;
  147.  
  148.     case WM_KEYDOWN:
  149.         switch (wParam) {
  150.         case VK_RETURN: //Enter
  151.             changeBackground(hwnd);
  152.             break;
  153.         case VK_ESCAPE: //Esc
  154.             DestroyWindow(hwnd);
  155.         }
  156.         break;
  157.  
  158.     case WM_DESTROY:
  159.         Save(hwnd);
  160.         delete mf;
  161.         PostQuitMessage(0);
  162.         return 0;
  163.     }
  164.  
  165.     return DefWindowProc(hwnd, message, wParam, lParam);
  166. }
  167.  
  168. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstanse, LPSTR LpszCmdParam, int nCmdShow)
  169. {
  170.     if (strstr(LpszCmdParam, "-ffs") != NULL)           // fstream
  171.         mf = new myfiles_fs((char *)fname);
  172.     else if (strstr(LpszCmdParam, "-fv") != NULL)       // file variables
  173.         mf = new myfiles_v((char *)fname);
  174.     else if (strstr(LpszCmdParam, "-fwapi") != NULL)    // windows api
  175.         mf = new myfiles_wapi((char *)fname);
  176.     else// if(strstr(LpszCmdParam, "-fmm") != NULL)     // mapping memory file [default]
  177.         mf = new myfiles_mm((char *)fname);
  178.  
  179.     char *buff = 0;
  180.     int lenght;
  181.     mf->read(buff, lenght);
  182.  
  183.     /* default settings */
  184.     int width = 320, height = 240, posx = 0, posy = 0;
  185.     hRGB = RGB(0, 0, 0);
  186.  
  187.     /* Parse settings */
  188.     char * name = NULL, *value = NULL;
  189.     while (splitnext(buff, name, value)) {
  190.         if (strcmp(name, "width") == 0)
  191.             width = atoi(value);
  192.         else if (strcmp(name, "height") == 0)
  193.             height = atoi(value);
  194.         else if (strcmp(name, "posx") == 0)
  195.             posx = atoi(value);
  196.         else if (strcmp(name, "posy") == 0)
  197.             posy = atoi(value);
  198.         else if (strcmp(name, "RGB") == 0)
  199.             hRGB = atoi(value);
  200.         else if (strcmp(name, "showCmd") == 0)
  201.             nCmdShow = atoi(value);
  202.         free(name); free(value);
  203.     }
  204.  
  205.     /* alloc memory */
  206.     pole = new bool*[n];
  207.     for (int i = 0; i < n; i++) {
  208.         pole[i] = new bool[n];
  209.         memset((char*)pole[i], 0, n * sizeof(bool));
  210.     }
  211.  
  212.     BOOL bMessageOk;
  213.     MSG message;                // Here message to the application are saved
  214.     WNDCLASS wincl = { 0 };     // Data structure for the windowclass
  215.  
  216.     /* The Window structure */
  217.     wincl.hInstance = hInstance;
  218.     wincl.lpszClassName = szWinClass;
  219.     wincl.lpfnWndProc = WindowProcedure;    // This function is called by Windows
  220.  
  221.     /* Create brush for background and pen for grid line */
  222.     hBrush = CreateSolidBrush(hRGB);
  223.     hPen = CreatePen(PS_DASHDOT, 3, RGB(255, 102, 102));
  224.     wincl.hbrBackground = hBrush;
  225.  
  226.     /* Register the window class, and if it fails quit the program */
  227.     if (!RegisterClass(&wincl))
  228.         return 0;
  229.  
  230.     /* The class is registered, let's create the program*/
  231.     hwnd = CreateWindow(
  232.         szWinClass,             /* Classname */
  233.         szWinName,              /* Title Text */
  234.         WS_OVERLAPPEDWINDOW,    /* default window */
  235.         posx,                   /* Windows decides the position */
  236.         posy,                   /* where the window ends up on the screen */
  237.         width,                  /* The programs width */
  238.         height,                 /* and height in pixels */
  239.         HWND_DESKTOP,           /* The window is a child-window to desktop */
  240.         NULL,                   /* No menu */
  241.         hInstance,              /* Program Instance handler */
  242.         NULL                    /* No Window Creation data */
  243.     );
  244.  
  245.     /* Make the window visible on the screen */
  246.     ShowWindow(hwnd, nCmdShow);
  247.  
  248.     /* Reg hotkey */
  249.     RegisterHotKey(hwnd, 1, MOD_SHIFT | MOD_NOREPEAT, VK_C);  //Shift+C
  250.     RegisterHotKey(hwnd, 2, MOD_CONTROL | MOD_NOREPEAT, VK_Q);  //Ctrl+Q
  251.  
  252.     /* Run the message loop. It will run until GetMessage() returns 0 */
  253.     while ((bMessageOk = GetMessage(&message, NULL, 0, 0)) != 0)
  254.     {
  255.         /* Yep, fuck logic: BOOL mb not only 1 or 0.
  256.         * See msdn at https://msdn.microsoft.com/en-us/library/windows/desktop/ms644936(v=vs.85).aspx
  257.         */
  258.         if (bMessageOk == -1)
  259.         {
  260.             puts("Suddenly, GetMessage failed! You can call GetLastError() to see what happend");
  261.             break;
  262.         }
  263.         /* Translate virtual-key message into character message */
  264.         TranslateMessage(&message);
  265.         /* Send message to WindowProcedure */
  266.         DispatchMessage(&message);
  267.     }
  268.  
  269.     /* Cleanup stuff */
  270.     DeleteObject(hBrush);
  271.     DeleteObject(hPen);
  272.     DestroyWindow(hwnd);
  273.     UnregisterClass(szWinClass, hInstance);
  274.  
  275.     return 0;
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement