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 "newmyfiles.h"
- #pragma comment(lib, "Msimg32.lib")
- #define VK_C 0x43
- #define VK_Q 0x51
- #define IMG_X "X2.jpg"
- #define IMG_0 "X3.png"
- /* 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 */
- char *pole;
- myfiles * mf;
- HBITMAP BitmapX, Bitmap0;
- UINT WM_P;
- unsigned char* (_stdcall *my_load_image) (const char*, unsigned int*, unsigned int*);
- 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 DrawBitmap(HDC hDC, int x, int y, int w, int h, HBITMAP hBitmap)
- {
- BITMAP bm;
- POINT ptSize, ptOrg;
- /* Get size pic */
- GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
- ptSize.x = bm.bmWidth;
- ptSize.y = bm.bmHeight;
- /* enter? */
- if (w < bm.bmWidth || h < bm.bmHeight)
- return;
- /* Calc coordinates of the upper right corner */
- int x_ = (w / 2) - (bm.bmWidth / 2),
- y_ = (h / 2) - (bm.bmHeight / 2);
- HDC hMemDC = CreateCompatibleDC(hDC);
- HBITMAP hOldbm = (HBITMAP)SelectObject(hMemDC, hBitmap);
- if (hOldbm)
- {
- SetMapMode(hMemDC, GetMapMode(hDC));
- // Преобразуем координаты устройства в логические для устройства вывода
- DPtoLP(hDC, &ptSize, 1);
- ptOrg.x = 0;
- ptOrg.y = 0;
- // Преобразуем координаты устройства в логические для контекста памяти
- DPtoLP(hMemDC, &ptOrg, 1);
- BitBlt(hDC, x+x_, y+y_, ptSize.x, ptSize.y, hMemDC, ptOrg.x, ptOrg.y, SRCCOPY);
- //TransparentBlt(hDC, x+x_, y+y_, ptSize.x, ptSize.y, hMemDC, ptOrg.x, ptOrg.y, ptSize.x, ptSize.y, RGB(255, 255, 255));
- SelectObject(hMemDC, hOldbm);
- }
- DeleteDC(hMemDC);
- }
- 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 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\nshowCmd=%d", nowPosition.rcNormalPosition.right - nowPosition.rcNormalPosition.left, nowPosition.rcNormalPosition.bottom - nowPosition.rcNormalPosition.top, nowPosition.rcNormalPosition.left, nowPosition.rcNormalPosition.top, hRGB, nowPosition.showCmd);
- 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;
- if (message == WM_P) {
- GetClientRect(hwnd, &c);
- x = GET_X_LPARAM(lParam);
- y = GET_Y_LPARAM(lParam);
- hdc = GetDC(hwnd);
- DrawBitmap(hdc, x*c.right / n, y*c.bottom / n, c.right / n, c.bottom / n, pole[x*n + y] == 1 ? Bitmap0 : BitmapX);
- SelectObject(hdc, hPen);
- ReleaseDC(hwnd, hdc);
- }
- switch (message)
- {
- 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*n + y] == 0) {
- pole[x*n + y] = 1 + rand() % 2;
- SendMessage(HWND_BROADCAST, WM_P, NULL, y << 16 | x);
- }
- break;
- case WM_PAINT:
- GetClientRect(hwnd, &c);
- hdc = BeginPaint(hwnd, &paintstruct);
- SelectObject(hdc, hPen);
- for (int i = 1; i < n; i++)
- {
- MoveToEx(hdc, 0, c.bottom / n * i, NULL);
- LineTo(hdc, c.right, c.bottom / n * i);
- MoveToEx(hdc, c.right / n * i, 0, NULL);
- LineTo(hdc, c.right / n * i, c.bottom);
- }
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- if (pole[i*n + j] != 0)
- DrawBitmap(hdc, i*c.right/n, j*c.bottom/n, c.right/n, c.bottom/n, pole[i*n + j] == 1 ? Bitmap0 : BitmapX);
- EndPaint(hwnd, &paintstruct);
- break;
- case WM_HOTKEY:
- switch (wParam) {
- case 1: //Shift+C
- RunNotepad();
- break;
- case 2: //Ctrl+Q
- DestroyWindow(hwnd);
- break;
- }
- break;
- case WM_KEYDOWN:
- switch (wParam) {
- case VK_RETURN:
- changeBackground(hwnd);
- break;
- case VK_ESCAPE:
- DestroyWindow(hwnd);
- break;
- }
- break;
- case WM_DESTROY:
- Save(hwnd);
- delete mf;
- PostQuitMessage(0);
- return 0;
- }
- 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);
- else if (strcmp(name, "showCmd") == 0)
- nCmdShow = atoi(value);
- free(name); free(value);
- }
- /* load function 'load_image' */
- HINSTANCE my_module = LoadLibrary("my.dll");
- if (my_module == NULL) {
- MessageBox(NULL, "File my.dll not found", "ERROR", MB_OK | MB_ICONERROR);
- return 0;
- }
- if (!((FARPROC &)my_load_image = GetProcAddress(my_module, "_load_image@12"))){
- MessageBox(NULL, "Function load_image not found", "ERROR", MB_OK | MB_ICONERROR);
- return 0;
- }
- /* Create Bitmap X and 0 */
- unsigned int iwidth, iheight;
- unsigned char* image;
- image = my_load_image(IMG_X, &iwidth, &iheight);
- if ((BitmapX = CreateBitmap(iwidth, iheight, 1, 32, image)) == NULL)
- return 0;
- image = my_load_image(IMG_0, &iwidth, &iheight);
- if ((Bitmap0 = CreateBitmap(iwidth, iheight, 1, 32, image)) == NULL)
- return 0;
- /* Register Window Message 'DRAW_PIC' */
- WM_P = RegisterWindowMessage("DRAW_PIC");
- /* Shared memory */
- HANDLE hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE | SEC_COMMIT, 0, n*n, "pole");
- if (hMapping == nullptr) {
- //char Number[10];
- //_itoa(GetLastError(), Number, 10);
- //MessageBox(NULL, "Function load_image not found", Number, MB_OK | MB_ICONERROR);
- return false;
- }
- pole = (char*)MapViewOfFile(hMapping, FILE_MAP_ALL_ACCESS, 0, 0, n*n);
- if (pole == nullptr) {
- CloseHandle(hMapping);
- }
- /**/
- 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 */
- 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 */
- CloseHandle(hMapping);
- FreeLibrary(my_module);
- DeleteObject(BitmapX);
- DeleteObject(Bitmap0);
- DeleteObject(hBrush);
- DeleteObject(hPen);
- DestroyWindow(hwnd);
- UnregisterClass(szWinClass, hInstance);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement