Guest User

main.cpp

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