vakho

Win API 3

Mar 26th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.87 KB | None | 0 0
  1. // Win32Project2.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Win32Project2.h"
  6.  
  7. #define MAX_LOADSTRING 100
  8.  
  9. // Global Variables:
  10. HINSTANCE hInst;                                // current instance
  11. TCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
  12. TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
  13.  
  14. // Forward declarations of functions included in this code module:
  15. ATOM                MyRegisterClass(HINSTANCE hInstance);
  16. BOOL                InitInstance(HINSTANCE, int);
  17. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  18. INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
  19.  
  20. int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
  21.     _In_opt_ HINSTANCE hPrevInstance,
  22.     _In_ LPTSTR    lpCmdLine,
  23.     _In_ int       nCmdShow)
  24. {
  25.     UNREFERENCED_PARAMETER(hPrevInstance);
  26.     UNREFERENCED_PARAMETER(lpCmdLine);
  27.  
  28.     // TODO: Place code here.
  29.     MSG msg;
  30.     HACCEL hAccelTable;
  31.  
  32.     // Initialize global strings
  33.     LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  34.     LoadString(hInstance, IDC_WIN32PROJECT2, szWindowClass, MAX_LOADSTRING);
  35.     MyRegisterClass(hInstance);
  36.  
  37.     // Perform application initialization:
  38.     if (!InitInstance(hInstance, nCmdShow))
  39.     {
  40.         return FALSE;
  41.     }
  42.  
  43.     hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT2));
  44.  
  45.     // Main message loop:
  46.     while (GetMessage(&msg, NULL, 0, 0))
  47.     {
  48.         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  49.         {
  50.             TranslateMessage(&msg);
  51.             DispatchMessage(&msg);
  52.         }
  53.     }
  54.  
  55.     return (int)msg.wParam;
  56. }
  57.  
  58.  
  59.  
  60. //
  61. //  FUNCTION: MyRegisterClass()
  62. //
  63. //  PURPOSE: Registers the window class.
  64. //
  65. ATOM MyRegisterClass(HINSTANCE hInstance)
  66. {
  67.     WNDCLASSEX wcex;
  68.  
  69.     wcex.cbSize = sizeof(WNDCLASSEX);
  70.  
  71.     wcex.style = CS_HREDRAW | CS_VREDRAW;
  72.     wcex.lpfnWndProc = WndProc;
  73.     wcex.cbClsExtra = 0;
  74.     wcex.cbWndExtra = 0;
  75.     wcex.hInstance = hInstance;
  76.     wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT2));
  77.     wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  78.     wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  79.     wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WIN32PROJECT2);
  80.     wcex.lpszClassName = szWindowClass;
  81.     wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  82.  
  83.     return RegisterClassEx(&wcex);
  84. }
  85.  
  86. //
  87. //   FUNCTION: InitInstance(HINSTANCE, int)
  88. //
  89. //   PURPOSE: Saves instance handle and creates main window
  90. //
  91. //   COMMENTS:
  92. //
  93. //        In this function, we save the instance handle in a global variable and
  94. //        create and display the main program window.
  95. //
  96. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  97. {
  98.     HWND hWnd;
  99.  
  100.     hInst = hInstance; // Store instance handle in our global variable
  101.  
  102.     hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  103.         CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  104.  
  105.     if (!hWnd)
  106.     {
  107.         return FALSE;
  108.     }
  109.  
  110.     ShowWindow(hWnd, nCmdShow);
  111.     UpdateWindow(hWnd);
  112.  
  113.     return TRUE;
  114. }
  115.  
  116. //
  117. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  118. //
  119. //  PURPOSE:  Processes messages for the main window.
  120. //
  121. //  WM_COMMAND  - process the application menu
  122. //  WM_PAINT    - Paint the main window
  123. //  WM_DESTROY  - post a quit message and return
  124. //
  125. //
  126. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  127. {
  128.     int wmId, wmEvent;
  129.     PAINTSTRUCT ps;
  130.     HDC hdc;
  131.     HDC hotherdc;
  132.     static HWND btn;
  133.     static HWND txt;
  134.     static HWND chk;
  135.     static HWND chk2;
  136.     static HWND listBox;
  137.     static HPEN hpen;
  138.     static HBRUSH hbrush;
  139.  
  140.     static HBRUSH hotherbrush;
  141.     static HBITMAP hbmp;
  142.     static HBITMAP hbigbmp;
  143.  
  144.     switch (message)
  145.     {
  146.     case WM_CREATE:
  147.         MessageBox(hWnd, L"Hello!", L"Hi", 0);
  148.         btn = CreateWindow(L"BUTTON", L"Draw Stuff...", WS_CHILD | WS_VISIBLE, 500, 35, 200, 20, hWnd, NULL, hInst, NULL); // BS_AUTOCHECKBOX
  149.         txt = CreateWindow(L"EDIT", L"", WS_CHILD | WS_VISIBLE | WS_BORDER, 10, 10, 200, 20, hWnd, 0, hInst, 0);
  150.         chk = CreateWindow(L"BUTTON", L"CheckBox", WS_CHILD | WS_VISIBLE | BS_CHECKBOX, 10, 60, 200, 20, hWnd, NULL, hInst, NULL);
  151.         chk2 = CreateWindow(L"BUTTON", L"CheckBox2", WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX, 10, 85, 200, 20, hWnd, NULL, hInst, NULL);
  152.         listBox = CreateWindow(L"LISTBOX", L"", WS_CHILD | WS_VISIBLE | WS_BORDER | LBS_NOTIFY, 10, 110, 200, 200, hWnd, NULL, hInst, NULL);
  153.         SendMessage(listBox, LB_ADDSTRING, 0, (LPARAM)L"პრიმა");
  154.         SendMessage(listBox, LB_ADDSTRING, 0, (LPARAM)L"სეკუნდა");
  155.         SendMessage(listBox, LB_ADDSTRING, 0, (LPARAM)L"ტერცია");
  156.         SendMessage(listBox, LB_ADDSTRING, 0, (LPARAM)L"კვარტა");
  157.         SendMessage(listBox, LB_ADDSTRING, 0, (LPARAM)L"კვინტა");
  158.         SendMessage(listBox, LB_ADDSTRING, 0, (LPARAM)L"სექსტა");
  159.         SendMessage(listBox, LB_ADDSTRING, 0, (LPARAM)L"სეპტიმა");
  160.         SendMessage(listBox, LB_ADDSTRING, 0, (LPARAM)L"ოქტავა");
  161.         break;
  162.     case WM_COMMAND:
  163.         wmEvent = HIWORD(wParam);
  164.         if (btn == (HWND)lParam) {
  165.             if (wmEvent == BN_CLICKED) {
  166.                 HDC hdc2 = GetDC(hWnd);
  167.                 LineTo(hdc2, 300, 480);
  168.                 RECT rec;
  169.                 GetClientRect(hWnd, &rec);
  170.                 DrawText(hdc2, L"Hello, world!", 17, &rec, DT_CENTER | DT_VCENTER);
  171.                 ReleaseDC(hWnd, hdc2);
  172.                 //TCHAR* buff;
  173.                 //int n = GetWindowTextLength(txt);
  174.                 //buff = (TCHAR*)malloc((n + 1) * sizeof(TCHAR));
  175.                 //GetWindowText(txt, buff, n + 1);
  176.                 //int k = SendMessage(listBox, LB_GETCURSEL, 0, 0);
  177.                 //if (k >= 0) {
  178.                 //  SendMessage(listBox, LB_INSERTSTRING, k, (LPARAM)buff); // დაამატებს მონიშნულის თავზე
  179.                 //}
  180.                 //else {
  181.                 //  SendMessage(listBox, LB_ADDSTRING, 0, (LPARAM)buff); // ბოლოში დაამატებს
  182.                 //}
  183.                 //free(buff);
  184.                 //buff = NULL;
  185.                 //delete buff;
  186.             }
  187.         }
  188.         else if
  189.             (txt == (HWND)lParam) {
  190.  
  191.             if (wmEvent == EN_CHANGE) {
  192.                 TCHAR* buff;
  193.                 int n = SendMessage(txt, WM_GETTEXTLENGTH, 0, 0);
  194.                 buff = (TCHAR*)malloc((n + 1) * sizeof(TCHAR));
  195.                 SendMessage(txt, WM_GETTEXT, n + 1, (LPARAM)buff);
  196.                 SendMessage(btn, WM_SETTEXT, 0, (LPARAM)buff);
  197.                 free(buff);
  198.                 buff = NULL;
  199.                 delete buff;
  200.             }
  201.         }
  202.         else if (chk == (HWND)lParam) {
  203.             if (wmEvent == BN_CLICKED) {
  204.                 int result = SendMessage(chk, BM_GETCHECK, 0, 0);
  205.                 if (result == BST_CHECKED) {
  206.                     SendMessage(chk, BM_SETCHECK, BST_UNCHECKED, 0);
  207.                 }
  208.                 else {
  209.                     SendMessage(chk, BM_SETCHECK, BST_CHECKED, 0);
  210.                 }
  211.             }
  212.         }
  213.         else if (listBox == (HWND)lParam) {
  214.             int n;
  215.             int len;
  216.             switch (wmEvent) {
  217.             case LBN_DBLCLK:
  218.                 n = SendMessage(listBox, LB_GETCURSEL, 0, 0); // მერამდენეა მონიშნული
  219.                 TCHAR* buff;
  220.                 len = SendMessage(listBox, LB_GETTEXTLEN, n, 0);
  221.                 buff = (TCHAR*)malloc((len + 1) * sizeof(TCHAR));
  222.                 SendMessage(listBox, LB_GETTEXT, n, (LPARAM)buff);
  223.                 SendMessage(btn, WM_SETTEXT, NULL, (LPARAM)buff);
  224.                 free(buff);
  225.                 break;
  226.             case LBN_SELCHANGE:
  227.                 n = SendMessage(listBox, LB_GETCURSEL, 0, 0); // მერამდენეა მონიშნული
  228.                 TCHAR* buff2;
  229.                 len = SendMessage(listBox, LB_GETTEXTLEN, n, 0);
  230.                 buff2 = (TCHAR*)malloc((len + 1) * sizeof(TCHAR));
  231.                 SendMessage(listBox, LB_GETTEXT, n, (LPARAM)buff2);
  232.                 SendMessage(hWnd, WM_SETTEXT, NULL, (LPARAM)buff2);
  233.                 free(buff2);
  234.                 break;
  235.             case LBN_SELCANCEL:
  236.                 n = SendMessage(listBox, LB_GETCURSEL, 0, 0); // მერამდენეა მონიშნული
  237.                 TCHAR* buff3;
  238.                 len = SendMessage(listBox, LB_GETTEXTLEN, n, 0);
  239.                 buff3 = (TCHAR*)malloc((len + 1) * sizeof(TCHAR));
  240.                 SendMessage(listBox, LB_GETTEXT, n, (LPARAM)buff3);
  241.                 MessageBox(hWnd, buff3, L"მოეხსნა", 0);
  242.                 free(buff3);
  243.                 break;
  244.             }
  245.         }
  246.         wmId = LOWORD(wParam);
  247.         // Parse the menu selections:
  248.         switch (wmId)
  249.         {
  250.         case IDM_ABOUT:
  251.             DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  252.             break;
  253.         case IDM_EXIT:
  254.             DestroyWindow(hWnd);
  255.             break;
  256.         default:
  257.             return DefWindowProc(hWnd, message, wParam, lParam);
  258.         }
  259.         break;
  260.     case WM_PAINT:
  261.         hbrush = CreateSolidBrush(RGB(255, 0, 0));
  262.         //hotherbrush = CreateHatchBrush(HS_DIAGCROSS, RGB(0, 255, 0));
  263.         hbmp = (HBITMAP)LoadImage(0, L"D:\\texture.bmp", IMAGE_BITMAP, 32, 32, LR_LOADFROMFILE);
  264.         hbigbmp = (HBITMAP)LoadImage(0, L"D:\\big.bmp", IMAGE_BITMAP, 200, 150, LR_LOADFROMFILE);
  265.         hotherbrush = CreatePatternBrush(hbmp);
  266.  
  267.         hpen = CreatePen(PS_DASH, 3, RGB(210, 225, 225));
  268.         hdc = BeginPaint(hWnd, &ps);
  269.         // TODO: Add any drawing code here...
  270.  
  271.         MoveToEx(hdc, 400, 100, NULL);
  272.         hpen = (HPEN)SelectObject(hdc, hpen);
  273.         Rectangle(hdc, 400, 100, 800, 300);
  274.         Ellipse(hdc, 400, 100, 800, 300);
  275.         LineTo(hdc, 800, 300);
  276.  
  277.         hbrush = (HBRUSH)SelectObject(hdc, hbrush); // save brush
  278.         Rectangle(hdc, 500, 200, 800, 300);
  279.         Ellipse(hdc, 400, 100, 700, 200);
  280.         hotherbrush = (HBRUSH)SelectObject(hdc, hotherbrush);
  281.         Pie(hdc, 400, 100, 700, 200, 400, 100, 550, 150);
  282.         FloodFill(hdc, 0, 0, RGB(210, 225, 225));
  283.         hotherbrush = (HBRUSH)SelectObject(hdc, hotherbrush);
  284.         hbrush = (HBRUSH)SelectObject(hdc, hbrush);
  285.  
  286.         hpen = (HPEN)SelectObject(hdc, hpen);
  287.        
  288.         hotherdc = CreateCompatibleDC(hdc);
  289.  
  290.         SelectObject(hotherdc, (HGDIOBJ)hbigbmp);
  291.         BitBlt(hdc, 200, 200, 400, 400, hotherdc, 1, 1, SRCINVERT);
  292.  
  293.         DeleteDC(hotherdc);
  294.  
  295.         EndPaint(hWnd, &ps);
  296.         DeleteObject((HGDIOBJ)hpen);
  297.         DeleteObject((HGDIOBJ)hbrush);
  298.         DeleteObject((HGDIOBJ)hotherbrush);
  299.         DeleteObject((HGDIOBJ)hbmp);
  300.         break;
  301.     case WM_DESTROY:
  302.         PostQuitMessage(0);
  303.         break;
  304.     default:
  305.         return DefWindowProc(hWnd, message, wParam, lParam);
  306.     }
  307.     return 0;
  308. }
  309.  
  310. // Message handler for about box.
  311. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  312. {
  313.     UNREFERENCED_PARAMETER(lParam);
  314.     switch (message)
  315.     {
  316.     case WM_INITDIALOG:
  317.         return (INT_PTR)TRUE;
  318.  
  319.     case WM_COMMAND:
  320.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  321.         {
  322.             EndDialog(hDlg, LOWORD(wParam));
  323.             return (INT_PTR)TRUE;
  324.         }
  325.         break;
  326.     }
  327.     return (INT_PTR)FALSE;
  328. }
Advertisement
Add Comment
Please, Sign In to add comment