Advertisement
Guest User

Untitled

a guest
Jan 27th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <windows.h>
  2. #include <string>
  3. #include <map>
  4. using namespace std;
  5.  
  6. #define ID_DISPLAY 1
  7.  
  8. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  9. CHAR szClassName[ ] = "CodeBlocksWindowsApp";
  10.  
  11. HWND hColor, hDisplay;
  12. map<string, COLORREF> ColBase;
  13. COLORREF BackroundCol = RGB(0,0,0);
  14. RECT ObszarRysowany;
  15.  
  16. void FillBase(){
  17.     ColBase["czerwony"] = RGB(255, 0, 0);
  18.     ColBase["zielony"] = RGB(0, 255, 0);
  19.     ColBase["niebieski"] = RGB(0, 0, 255);
  20.     ColBase["bialy"] = RGB(255, 255, 255);
  21.     ColBase["czarny"] = RGB(0, 0, 0);
  22. }
  23. COLORREF fGetCol(string tekst){
  24.     map<string, COLORREF>::iterator cur;
  25.     cur = ColBase.find(tekst);
  26.     return cur->second;
  27. }
  28.  
  29. int WINAPI WinMain (HINSTANCE hThisInstance,
  30.                      HINSTANCE hPrevInstance,
  31.                      LPSTR lpszArgument,
  32.                      int nCmdShow)
  33. {
  34.     HWND hwnd;
  35.     MSG messages;
  36.     WNDCLASSEX wincl;
  37.  
  38.     wincl.hInstance = hThisInstance;
  39.     wincl.lpszClassName = szClassName;
  40.     wincl.lpfnWndProc = WindowProcedure;
  41.     wincl.style = CS_DBLCLKS;
  42.     wincl.cbSize = sizeof (WNDCLASSEX);
  43.     wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  44.     wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  45.     wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  46.     wincl.lpszMenuName = NULL;
  47.     wincl.cbClsExtra = 0;
  48.     wincl.cbWndExtra = 0;
  49.     wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  50.  
  51.     if (!RegisterClassEx (&wincl))
  52.         return 0;
  53.  
  54.     FillBase();
  55.  
  56.     hwnd = CreateWindowEx (
  57.            0,                   /* Extended possibilites for variation */
  58.            szClassName,         /* Classname */
  59.            "Pobieranie Koloru",       /* Title Text */
  60.            WS_OVERLAPPEDWINDOW, /* default window */
  61.            CW_USEDEFAULT,       /* Windows decides the position */
  62.            CW_USEDEFAULT,       /* where the window ends up on the screen */
  63.            200,                 /* The programs width */
  64.            100,                 /* and height in pixels */
  65.            NULL,        /* The window is a child-window to desktop */
  66.            NULL,                /* No menu */
  67.            hThisInstance,       /* Program Instance handler */
  68.            NULL                 /* No Window Creation data */
  69.            );
  70.  
  71.     hColor = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "Podaj kolor...", WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL, 0, 0, 200, 23, hwnd, NULL, hThisInstance, NULL);
  72.     hDisplay = CreateWindowEx(0, "BUTTON", "Wyswietl podany kolor", WS_CHILD | WS_VISIBLE, 0, 23, 200, 23, hwnd, (HMENU)ID_DISPLAY, hThisInstance, NULL);
  73.  
  74.     ObszarRysowany.left = 0;
  75.     ObszarRysowany.right = 200;
  76.     ObszarRysowany.top = 46;
  77.     ObszarRysowany.bottom = 64;
  78.  
  79.     ShowWindow (hwnd, nCmdShow);
  80.  
  81.     while (GetMessage (&messages, NULL, 0, 0))
  82.     {
  83.         TranslateMessage(&messages);
  84.         DispatchMessage(&messages);
  85.     }
  86.     return messages.wParam;
  87. }
  88.  
  89. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  90. {
  91.     switch (message)
  92.     {
  93.         case WM_PAINT:{
  94.             PAINTSTRUCT ps;
  95.             HDC hdc = BeginPaint(hwnd, &ps);
  96.  
  97.             HBRUSH pedzel = CreateSolidBrush(BackroundCol);
  98.             FillRect(hdc, &ObszarRysowany, pedzel);
  99.             DeleteObject(pedzel);
  100.  
  101.             EndPaint(hwnd, &ps);
  102.             break;
  103.         }
  104.         case WM_COMMAND:{
  105.             if(wParam == ID_DISPLAY)
  106.             {
  107.                 int lenght = GetWindowTextLength(hColor);
  108.                 char *tekst = new char[lenght+1];
  109.                 GetWindowText(hColor, tekst, lenght+1);
  110.                 BackroundCol = fGetCol(tekst);
  111.                 delete tekst;
  112.                 InvalidateRect(hwnd, NULL, TRUE);
  113.             }
  114.             break;
  115.         }
  116.         case WM_DESTROY:
  117.             PostQuitMessage (0);
  118.             break;
  119.         default:
  120.             return DefWindowProc (hwnd, message, wParam, lParam);
  121.     }
  122.  
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement