Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <tchar.h>
- #include <windows.h>
- #include <windowsx.h>
- #include <string.h>
- #include "newmyfiles.h"
- #define VK_C 0x43
- #define VK_Q 0x51
- /* use _CRT_SECURE_NO_WARNINGS !!! */
- const TCHAR szWinClass[] = _T("Win32SampleApp");
- const TCHAR szWinName[] = _T("Win32SampleWindow");
- HWND hwnd; /* This is the handle for our window */
- HBRUSH hBrush; /* Current brush */
- HPEN hPen; /* Current pen */
- int hRGB; /* Current RGB */
- int n = 3; /* Grid size */
- const char * fname = "my.ini"; /* .ini file name */
- bool** pole;
- myfiles * mf;
- bool splitnext(char* &line, char * &name, char * &value) {
- char * end;
- while ((end = strchr(line, '\n')) != NULL || strlen(line) > 0 && (end = line + strlen(line))) { // search '\n' or end of file
- char * fext = new char[end - line + 1];
- strncpy(fext, line, end - line);
- fext[end - line] = '\0';
- char * r;
- if ((r = strchr(fext, '=')) != NULL) { // search ' = ' and split by name and value
- int len = (r - fext);
- name = new char[len + 1];
- value = new char[strlen(fext) - len];
- strncpy(name, fext, len);
- strncpy(value, r + 1, strlen(fext) - len - 1);
- name[len] = '\0';
- value[strlen(fext) - len - 1] = '\0';
- line = end + 1;
- free(fext);
- return true;
- }
- line = end + 1;
- free(fext);
- }
- return false;
- }
- void RunNotepad(void)
- {
- STARTUPINFO sInfo;
- PROCESS_INFORMATION pInfo;
- ZeroMemory(&sInfo, sizeof(STARTUPINFO));
- CreateProcess(_T("C:\\Windows\\Notepad.exe"), NULL, NULL, NULL, FALSE, 0, NULL, NULL, &sInfo, &pInfo);
- }
- void changeBackground(HWND hwnd)
- {
- DeleteObject(hBrush);
- hRGB = RGB(rand() % 255, rand() % 255, rand() % 255);
- hBrush = (HBRUSH)CreateSolidBrush(hRGB);
- SetClassLongPtr(hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)hBrush);
- InvalidateRect(hwnd, NULL, TRUE); //redraw window
- }
- void Save(HWND hwnd) {
- WINDOWPLACEMENT nowPosition;
- nowPosition.length = sizeof(WINDOWPLACEMENT); /* */
- GetWindowPlacement(hwnd, &nowPosition);
- char *buffer = new char[100];
- 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);
- mf->write(buffer, len);
- free(buffer);
- }
- LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- HDC hdc;
- PAINTSTRUCT paintstruct;
- RECT c;
- int x, y;
- switch (message)
- {
- case WM_DESTROY:
- Save(hwnd);
- delete mf;
- PostQuitMessage(0);
- return 0;
- case WM_SIZE:
- InvalidateRect(hwnd, NULL, TRUE);
- break;
- case WM_LBUTTONUP:
- GetClientRect(hwnd, &c);
- x = GET_X_LPARAM(lParam) / (c.right / n);
- y = GET_Y_LPARAM(lParam) / (c.bottom / n);
- if (!pole[x][y]) {
- pole[x][y] = true;
- hdc = GetDC(hwnd);
- SelectObject(hdc, hPen);
- SelectObject(hdc, (HGDIOBJ)GetStockObject(NULL_BRUSH));
- Ellipse(hdc, x*c.right / n + 5, y*c.bottom / n + 5, (x + 1)*c.right / n - 5, (y + 1)*c.bottom / n - 5);
- SelectObject(hdc, hPen);
- ReleaseDC(hwnd, hdc);
- }
- break;
- case WM_PAINT:
- hdc = BeginPaint(hwnd, &paintstruct);
- SelectObject(hdc, hPen);
- for (int i = 1; i < n; i++)
- {
- MoveToEx(hdc, 0, paintstruct.rcPaint.bottom / n * i, NULL);
- LineTo(hdc, paintstruct.rcPaint.right, paintstruct.rcPaint.bottom / n * i);
- MoveToEx(hdc, paintstruct.rcPaint.right / n * i, 0, NULL);
- LineTo(hdc, paintstruct.rcPaint.right / n * i, paintstruct.rcPaint.bottom);
- }
- SelectObject(hdc, (HGDIOBJ)GetStockObject(NULL_BRUSH));
- SelectObject(hdc, hPen);
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- if (pole[i][j])
- 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);
- ReleaseDC(hwnd, hdc);
- EndPaint(hwnd, &paintstruct);
- break;
- case WM_HOTKEY:
- switch (wParam) {
- case 1: //Shift+C
- RunNotepad();
- break;
- case 2: //Ctrl+Q
- DestroyWindow(hwnd);
- }
- break;
- case WM_KEYDOWN:
- switch (wParam) {
- case VK_RETURN: //Enter
- changeBackground(hwnd);
- break;
- case VK_ESCAPE: //Esc
- DestroyWindow(hwnd);
- }
- break;
- }
- return DefWindowProc(hwnd, message, wParam, lParam);
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstanse, LPSTR LpszCmdParam, int nCmdShow)
- {
- if (strstr(LpszCmdParam, "-ffs") != NULL) // fstream
- mf = new myfiles_fs((char *)fname);
- else if (strstr(LpszCmdParam, "-fv") != NULL) // file variables
- mf = new myfiles_v((char *)fname);
- else if (strstr(LpszCmdParam, "-fwapi") != NULL) // windows api
- mf = new myfiles_wapi((char *)fname);
- else// if(strstr(LpszCmdParam, "-fmm") != NULL) // mapping memory file [default]
- mf = new myfiles_mm((char *)fname);
- char *buff;
- int lenght;
- mf->read(buff, lenght);
- /* default settings */
- int width = 320, height = 240, posx = 0, posy = 0;
- hRGB = RGB(0, 0, 0);
- /* Parse settings */
- char * name = NULL, *value = NULL;
- while (splitnext(buff, name, value)) {
- if (strcmp(name, "width") == 0)
- width = atoi(value);
- else if (strcmp(name, "height") == 0)
- height = atoi(value);
- else if (strcmp(name, "posx") == 0)
- posx = atoi(value);
- else if (strcmp(name, "posy") == 0)
- posy = atoi(value);
- else if (strcmp(name, "RGB") == 0)
- hRGB = atoi(value);
- free(name); free(value);
- }
- /* alloc memory */
- pole = new bool*[n];
- for (int i = 0; i < n; i++) {
- pole[i] = new bool[n];
- memset((char*)pole[i], 0, n * sizeof(bool));
- }
- BOOL bMessageOk;
- MSG message; // Here message to the application are saved
- WNDCLASS wincl = { 0 }; // Data structure for the windowclass
- /* The Window structure */
- wincl.hInstance = hInstance;
- wincl.lpszClassName = szWinClass;
- wincl.lpfnWndProc = WindowProcedure; // This function is called by Windows
- /* Create brush for background and pen for grid line */
- hBrush = CreateSolidBrush(hRGB);
- hPen = CreatePen(PS_DASHDOT, 3, RGB(255, 102, 102));
- wincl.hbrBackground = hBrush;
- /* Register the window class, and if it fails quit the program */
- if (!RegisterClass(&wincl))
- return 0;
- /* The class is registered, let's create the program*/
- hwnd = CreateWindow(
- szWinClass, /* Classname */
- szWinName, /* Title Text */
- WS_OVERLAPPEDWINDOW, /* default window */
- posx, /* Windows decides the position */
- posy, /* where the window ends up on the screen */
- width, /* The programs width */
- height, /* and height in pixels */
- HWND_DESKTOP, /* The window is a child-window to desktop */
- NULL, /* No menu */
- hInstance, /* Program Instance handler */
- NULL /* No Window Creation data */
- );
- /* Make the window visible on the screen */
- ShowWindow(hwnd, nCmdShow);
- /* Reg hotkey */
- RegisterHotKey(hwnd, 1, MOD_SHIFT | MOD_NOREPEAT, VK_C); //Shift+C
- RegisterHotKey(hwnd, 2, MOD_CONTROL | MOD_NOREPEAT, VK_Q); //Ctrl+Q
- /* Run the message loop. It will run until GetMessage() returns 0 */
- while ((bMessageOk = GetMessage(&message, NULL, 0, 0)) != 0)
- {
- /* Yep, fuck logic: BOOL mb not only 1 or 0.
- * See msdn at https://msdn.microsoft.com/en-us/library/windows/desktop/ms644936(v=vs.85).aspx
- */
- if (bMessageOk == -1)
- {
- puts("Suddenly, GetMessage failed! You can call GetLastError() to see what happend");
- break;
- }
- /* Translate virtual-key message into character message */
- TranslateMessage(&message);
- /* Send message to WindowProcedure */
- DispatchMessage(&message);
- }
- /* Cleanup stuff */
- DeleteObject(hBrush);
- DeleteObject(hPen);
- DestroyWindow(hwnd);
- UnregisterClass(szWinClass, hInstance);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement