Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.55 KB | None | 0 0
  1. #include <windows.h>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. LRESULT CALLBACK windowprocessforwindow1(HWND handleforwindow1, UINT message, WPARAM wParam, LPARAM lParam);
  7. LRESULT CALLBACK windowprocessforwindow2(HWND handleforwindow2, UINT message, WPARAM wParam, LPARAM lParam);
  8.  
  9. HINSTANCE hInst, hPrevInst;
  10. COLORREF color = RGB(68, 68, 68);
  11. HDC hdc;
  12. PAINTSTRUCT ps;
  13. DWORD dColors[3] = {255, 222, 222};
  14.  
  15. HWND hEdit1, hEdit2, hEdit3, hEdit4;
  16.  
  17. HWND handleforwindow1;
  18. HWND handleforwindow2;
  19.  
  20. WNDCLASSEX windowclassforwindow2;
  21. int nShowCmdGlob;
  22.  
  23. vector<vector<int> > rectaingles;
  24. vector<COLORREF> colors;
  25.  
  26. const int DRAW_MENU_BUTTON = 101;
  27. const int REFRESH_MENU_BUTTON = 102;
  28. const int EXIT_MENU_BUTTON = 103;
  29.  
  30. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd) {
  31.     nShowCmdGlob = nShowCmd;
  32.     WNDCLASSEX windowclassforwindow1;
  33.     ZeroMemory(&windowclassforwindow1, sizeof(WNDCLASSEX));
  34.     windowclassforwindow1.cbClsExtra = 0;
  35.     windowclassforwindow1.cbSize = sizeof(WNDCLASSEX);
  36.     windowclassforwindow1.cbWndExtra = 0;
  37.     windowclassforwindow1.hbrBackground = (HBRUSH) COLOR_WINDOW;
  38.     windowclassforwindow1.hCursor = LoadCursor(NULL, IDC_ARROW);
  39.     windowclassforwindow1.hIcon = NULL;
  40.     windowclassforwindow1.hIconSm = NULL;
  41.     windowclassforwindow1.hInstance = hInst;
  42.     windowclassforwindow1.lpfnWndProc = (WNDPROC) windowprocessforwindow1;
  43.     windowclassforwindow1.lpszClassName = "windowclass 1";
  44.     windowclassforwindow1.lpszMenuName = NULL;
  45.     windowclassforwindow1.style = CS_HREDRAW | CS_VREDRAW;
  46.  
  47.     if (!RegisterClassEx(&windowclassforwindow1)) {
  48.         int nResult = GetLastError();
  49.         MessageBox(NULL,
  50.                    "Window class creation failed",
  51.                    "Window Class Failed",
  52.                    MB_ICONERROR);
  53.     }
  54.  
  55.     HMENU hMenuPopup = CreateMenu();
  56.     AppendMenu(hMenuPopup, MF_STRING, DRAW_MENU_BUTTON, "Draw");
  57.     AppendMenu(hMenuPopup, MF_STRING, REFRESH_MENU_BUTTON, "Refresh");
  58.     AppendMenu(hMenuPopup, MF_STRING, EXIT_MENU_BUTTON, "Exit");
  59.  
  60.     RECT screen_rect;
  61.     GetWindowRect(GetDesktopWindow(), &screen_rect);
  62.     int x = screen_rect.right / 2 - 450;
  63.     int y = screen_rect.bottom / 2 - 250;
  64.  
  65.     handleforwindow1 = CreateWindow(windowclassforwindow1.lpszClassName, "Rectaingles", WS_OVERLAPPEDWINDOW | WS_POPUP | WS_VISIBLE, x, y,
  66.                                          900, 500, (HWND) NULL, hMenuPopup, hInst, (LPSTR) NULL);
  67.     ShowWindow(handleforwindow1, nShowCmd);
  68.     hdc = GetDC(handleforwindow1);
  69.  
  70.     ZeroMemory(&windowclassforwindow2, sizeof(WNDCLASSEX));
  71.     windowclassforwindow2.cbClsExtra = 0;
  72.     windowclassforwindow2.cbSize = sizeof(WNDCLASSEX);
  73.     windowclassforwindow2.cbWndExtra = 0;
  74.     windowclassforwindow2.hbrBackground = (HBRUSH) COLOR_WINDOW;
  75.     windowclassforwindow2.hCursor = LoadCursor(NULL, IDC_ARROW);
  76.     windowclassforwindow2.hIcon = NULL;
  77.     windowclassforwindow2.hIconSm = NULL;
  78.     windowclassforwindow2.hInstance = hInst;
  79.     windowclassforwindow2.lpfnWndProc = (WNDPROC) windowprocessforwindow2;
  80.     windowclassforwindow2.lpszClassName = "window class2";
  81.     windowclassforwindow2.lpszMenuName = NULL;
  82.     windowclassforwindow2.style = CS_HREDRAW | CS_VREDRAW;
  83.  
  84.     if (!RegisterClassEx(&windowclassforwindow2)) {
  85.         int nResult = GetLastError();
  86.         MessageBox(NULL,
  87.                    "Window class creation failed for window 2",
  88.                    "Window Class Failed",
  89.                    MB_ICONERROR);
  90.     }
  91.  
  92.     MSG lpMsg;
  93.     while (GetMessage(&lpMsg, NULL, 0, 0)) {
  94.         TranslateMessage(&lpMsg);
  95.         DispatchMessage(&lpMsg);
  96.     }
  97.     return (lpMsg.wParam);
  98. }
  99.  
  100.  
  101. LRESULT CALLBACK windowprocessforwindow1(HWND handleforwindow, UINT msg, WPARAM wParam, LPARAM lParam) {
  102.     HWND hTest, hAdd, hDel, hFlu, hWin, hCombobox;
  103.     PAINTSTRUCT ps;
  104.     CHOOSECOLOR cc;
  105.     cc.Flags = CC_RGBINIT | CC_FULLOPEN;
  106.     cc.hInstance = NULL;
  107.     cc.hwndOwner = handleforwindow;
  108.     cc.lCustData = 0L;
  109.     cc.lpCustColors = dColors;
  110.     cc.lpfnHook = NULL;
  111.     cc.lpTemplateName = (LPSTR) NULL;
  112.     cc.lStructSize = sizeof(cc);
  113.     cc.rgbResult = RGB(255, 0, 0);
  114.  
  115.     HBRUSH hBrush;
  116.  
  117.     switch (msg) {
  118.         case WM_INITDIALOG:
  119.             break;
  120.  
  121.         case WM_COMMAND:
  122.             if (HIWORD(wParam) == 0) {
  123.                 switch (LOWORD(wParam)) {
  124.                     case DRAW_MENU_BUTTON:
  125.                         handleforwindow2 = CreateWindow(windowclassforwindow2.lpszClassName, "Draw rectaingle", WS_OVERLAPPEDWINDOW, 200, 150, 300, 380, NULL, NULL, hInst, (LPSTR) NULL);
  126.                         ShowWindow(handleforwindow2, nShowCmdGlob);
  127.                         break;
  128.                     case REFRESH_MENU_BUTTON:
  129.                         rectaingles.clear();
  130.                         colors.clear();
  131.                         InvalidateRect(handleforwindow1, NULL, 1);
  132.                         break;
  133.                     case EXIT_MENU_BUTTON:
  134.                         PostQuitMessage(0);
  135.                 }
  136.             }
  137.             return 0;
  138.         case WM_PAINT : {
  139.             HDC hdc = BeginPaint(handleforwindow, &ps);
  140.  
  141.             for (int i = 0; i < rectaingles.size(); i++) {
  142.                 RECT r;
  143.                 r.left=rectaingles[i][0];
  144.                 r.top=rectaingles[i][1];
  145.                 r.right=rectaingles[i][2];
  146.                 r.bottom=rectaingles[i][3];
  147.                 FillRect(hdc, &r, HBRUSH(CreateSolidBrush(colors[i])));
  148.             }
  149.  
  150.             EndPaint(handleforwindow, &ps);
  151.             break;
  152.         }
  153.  
  154.         case WM_DESTROY:
  155.             PostQuitMessage(0);
  156.             break;
  157.         default:
  158.             return (DefWindowProc(handleforwindow, msg, wParam, lParam));
  159.     }
  160.     return 0;
  161. }
  162.  
  163. LRESULT CALLBACK windowprocessforwindow2(HWND handleforwindow, UINT msg, WPARAM wParam, LPARAM lParam) {
  164.     CHOOSECOLOR cc;
  165.     cc.Flags = CC_RGBINIT | CC_FULLOPEN;
  166.     cc.hInstance = NULL;
  167.     cc.hwndOwner = handleforwindow;
  168.     cc.lCustData = 0L;
  169.     cc.lpCustColors = dColors;
  170.     cc.lpfnHook = NULL;
  171.     cc.lpTemplateName = (LPSTR) NULL;
  172.     cc.lStructSize = sizeof(cc);
  173.     cc.rgbResult = RGB(255, 0, 0);
  174.  
  175.     const int SET_COLOR_BUTTON = 1;
  176.     const int DRAW_RECT_BUTTON = 2;
  177.  
  178.     switch (msg) {
  179.         case WM_CREATE : {
  180.             CreateWindow("STATIC", "Draw rectaingle", WS_VISIBLE | WS_CHILD | SS_LEFT, 10, 10, 200, 100,
  181.                          handleforwindow, NULL, hInst, NULL);
  182.  
  183.             CreateWindow("STATIC", "Left:", WS_VISIBLE | WS_CHILD | SS_LEFT, 10, 50, 200, 100,
  184.                          handleforwindow, NULL, hInst, NULL);
  185.             CreateWindow("STATIC", "Top:", WS_VISIBLE | WS_CHILD | SS_LEFT, 10, 100, 200, 100,
  186.                          handleforwindow, NULL, hInst, NULL);
  187.             CreateWindow("STATIC", "Right:", WS_VISIBLE | WS_CHILD | SS_LEFT, 10, 150, 200, 100,
  188.                          handleforwindow, NULL, hInst, NULL);
  189.             CreateWindow("STATIC", "Bottom:", WS_VISIBLE | WS_CHILD | SS_LEFT, 10, 200, 200, 100,
  190.                          handleforwindow, NULL, hInst, NULL);
  191.             hEdit1 = CreateWindowEx(WS_EX_CLIENTEDGE, "edit", 0, WS_CHILD | WS_VISIBLE, 70, 45, 120, 30,
  192.                                     handleforwindow, (HMENU) 910, NULL, NULL);
  193.             hEdit2 = CreateWindowEx(WS_EX_CLIENTEDGE, "edit", 0, WS_CHILD | WS_VISIBLE, 70, 95, 120, 30,
  194.                                     handleforwindow, (HMENU) 911, NULL, NULL);
  195.             hEdit3 = CreateWindowEx(WS_EX_CLIENTEDGE, "edit", 0, WS_CHILD | WS_VISIBLE, 70, 145, 120, 30,
  196.                                     handleforwindow, (HMENU) 912, NULL, NULL);
  197.             hEdit4 = CreateWindowEx(WS_EX_CLIENTEDGE, "edit", 0, WS_CHILD | WS_VISIBLE, 70, 195, 120, 30,
  198.                                     handleforwindow, (HMENU) 913, NULL, NULL);
  199.             CreateWindow("button", "Set color", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 70, 245, 120, 20,
  200.                          handleforwindow, (HMENU) SET_COLOR_BUTTON, NULL, NULL);
  201.             CreateWindow("button", "Draw", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 70, 295, 120, 20,
  202.                          handleforwindow, (HMENU) DRAW_RECT_BUTTON, NULL, NULL);
  203.             break;
  204.         }
  205.         case WM_COMMAND:
  206.             if (HIWORD(wParam) == 0) {
  207.                 switch (LOWORD(wParam)) {
  208.                     case SET_COLOR_BUTTON:
  209.                         if (ChooseColor(&cc)) { color = (COLORREF) cc.rgbResult; }
  210.                         return 0;
  211.                     case DRAW_RECT_BUTTON:
  212.                         char left[9];
  213.                         char top[9];
  214.                         char right[9];
  215.                         char bottom[9];
  216.                         GetWindowText(hEdit1,left,9);
  217.                         GetWindowText(hEdit2,top,9);
  218.                         GetWindowText(hEdit3,right,9);
  219.                         GetWindowText(hEdit4,bottom,9);
  220.  
  221.                         vector<int> rectaingle;
  222.                         rectaingle.push_back(atoi(left));
  223.                         rectaingle.push_back(atoi(top));
  224.                         rectaingle.push_back(atoi(right));
  225.                         rectaingle.push_back(atoi(bottom));
  226.  
  227.                         rectaingles.push_back(rectaingle);
  228.  
  229.                         colors.push_back(color);
  230.  
  231.                         InvalidateRect(handleforwindow1,NULL,TRUE);
  232.  
  233.                         break;
  234.                 }
  235.             }
  236.             return 0;
  237.         case WM_DESTROY: {
  238.             return 0;
  239.         }
  240.     }
  241.  
  242.     return DefWindowProc(handleforwindow, msg, wParam, lParam);
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement