vakho

Win API 2

Mar 19th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.19 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.     static HWND btn;
  132.     static HWND txt;
  133.     static HWND chk;
  134.     static HWND chk2;
  135.     static HWND listBox;
  136.  
  137.     switch (message)
  138.     {
  139.     case WM_CREATE:
  140.         MessageBox(hWnd, L"Hello!", L"Hi", 0);
  141.         btn = CreateWindow(L"BUTTON", L"OK", WS_CHILD | WS_VISIBLE, 10, 35, 200, 20, hWnd, NULL, hInst, NULL); // BS_AUTOCHECKBOX
  142.         txt = CreateWindow(L"EDIT", L"", WS_CHILD | WS_VISIBLE | WS_BORDER, 10, 10, 200, 20, hWnd, 0, hInst, 0);
  143.         chk = CreateWindow(L"BUTTON", L"CheckBox", WS_CHILD | WS_VISIBLE | BS_CHECKBOX, 10, 60, 200, 20, hWnd, NULL, hInst, NULL);
  144.         chk2 = CreateWindow(L"BUTTON", L"CheckBox2", WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX, 10, 85, 200, 20, hWnd, NULL, hInst, NULL);
  145.         listBox = CreateWindow(L"LISTBOX", L"", WS_CHILD | WS_VISIBLE | WS_BORDER | LBS_NOTIFY, 10, 110, 200, 200, hWnd, NULL, hInst, NULL);
  146.         SendMessage(listBox, LB_ADDSTRING, 0, (LPARAM)L"პრიმა");
  147.         SendMessage(listBox, LB_ADDSTRING, 0, (LPARAM)L"სეკუნდა");
  148.         SendMessage(listBox, LB_ADDSTRING, 0, (LPARAM)L"ტერცია");
  149.         SendMessage(listBox, LB_ADDSTRING, 0, (LPARAM)L"კვარტა");
  150.         SendMessage(listBox, LB_ADDSTRING, 0, (LPARAM)L"კვინტა");
  151.         SendMessage(listBox, LB_ADDSTRING, 0, (LPARAM)L"სექსტა");
  152.         SendMessage(listBox, LB_ADDSTRING, 0, (LPARAM)L"სეპტიმა");
  153.         SendMessage(listBox, LB_ADDSTRING, 0, (LPARAM)L"ოქტავა");
  154.         break;
  155.     case WM_COMMAND:
  156.         wmEvent = HIWORD(wParam);
  157.         if (btn == (HWND)lParam) {
  158.             if (wmEvent == BN_CLICKED) {
  159.                 TCHAR* buff;
  160.                 int n = GetWindowTextLength(txt);
  161.                 buff = (TCHAR*)malloc((n + 1) * sizeof(TCHAR));
  162.                 GetWindowText(txt, buff, n + 1);
  163.                 int k = SendMessage(listBox, LB_GETCURSEL, 0, 0);
  164.                 if (k >= 0) {
  165.                     SendMessage(listBox, LB_INSERTSTRING, k, (LPARAM)buff); // დაამატებს მონიშნულის თავზე
  166.                 }
  167.                 else {
  168.                     SendMessage(listBox, LB_ADDSTRING, 0, (LPARAM)buff); // ბოლოში დაამატებს
  169.                 }
  170.                 free(buff);
  171.                 buff = NULL;
  172.                 delete buff;
  173.             }
  174.         }
  175.         else if
  176.             (txt == (HWND)lParam) {
  177.  
  178.             if (wmEvent == EN_CHANGE) {
  179.                 TCHAR* buff;
  180.                 int n = SendMessage(txt, WM_GETTEXTLENGTH, 0, 0);
  181.                 buff = (TCHAR*)malloc((n + 1) * sizeof(TCHAR));
  182.                 SendMessage(txt, WM_GETTEXT, n + 1, (LPARAM)buff);
  183.                 SendMessage(btn, WM_SETTEXT, 0, (LPARAM)buff);
  184.                 free(buff);
  185.                 buff = NULL;
  186.                 delete buff;
  187.             }
  188.         }
  189.         else if (chk == (HWND)lParam) {
  190.             if (wmEvent == BN_CLICKED) {
  191.                 int result = SendMessage(chk, BM_GETCHECK, 0, 0);
  192.                 if (result == BST_CHECKED) {
  193.                     SendMessage(chk, BM_SETCHECK, BST_UNCHECKED, 0);
  194.                 }
  195.                 else {
  196.                     SendMessage(chk, BM_SETCHECK, BST_CHECKED, 0);
  197.                 }
  198.             }
  199.         }
  200.         else if (listBox == (HWND)lParam) {
  201.             int n;
  202.             int len;
  203.             switch (wmEvent) {
  204.             case LBN_DBLCLK:
  205.                 n = SendMessage(listBox, LB_GETCURSEL, 0, 0); // მერამდენეა მონიშნული
  206.                 TCHAR* buff;
  207.                 len = SendMessage(listBox, LB_GETTEXTLEN, n, 0);
  208.                 buff = (TCHAR*)malloc((len + 1) * sizeof(TCHAR));
  209.                 SendMessage(listBox, LB_GETTEXT, n, (LPARAM)buff);
  210.                 SendMessage(btn, WM_SETTEXT, NULL, (LPARAM)buff);
  211.                 free(buff);
  212.                 break;
  213.             case LBN_SELCHANGE:
  214.                 n = SendMessage(listBox, LB_GETCURSEL, 0, 0); // მერამდენეა მონიშნული
  215.                 TCHAR* buff2;
  216.                 len = SendMessage(listBox, LB_GETTEXTLEN, n, 0);
  217.                 buff2 = (TCHAR*)malloc((len + 1) * sizeof(TCHAR));
  218.                 SendMessage(listBox, LB_GETTEXT, n, (LPARAM)buff2);
  219.                 SendMessage(hWnd, WM_SETTEXT, NULL, (LPARAM)buff2);
  220.                 free(buff2);
  221.                 break;
  222.             case LBN_SELCANCEL:
  223.                 n = SendMessage(listBox, LB_GETCURSEL, 0, 0); // მერამდენეა მონიშნული
  224.                 TCHAR* buff3;
  225.                 len = SendMessage(listBox, LB_GETTEXTLEN, n, 0);
  226.                 buff3 = (TCHAR*)malloc((len + 1) * sizeof(TCHAR));
  227.                 SendMessage(listBox, LB_GETTEXT, n, (LPARAM)buff3);
  228.                 MessageBox(hWnd, buff3, L"მოეხსნა", 0);
  229.                 free(buff3);
  230.                 break;
  231.             }
  232.         }
  233.         wmId = LOWORD(wParam);
  234.         // Parse the menu selections:
  235.         switch (wmId)
  236.         {
  237.         case IDM_ABOUT:
  238.             DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  239.             break;
  240.         case IDM_EXIT:
  241.             DestroyWindow(hWnd);
  242.             break;
  243.         default:
  244.             return DefWindowProc(hWnd, message, wParam, lParam);
  245.         }
  246.         break;
  247.     case WM_PAINT:
  248.         hdc = BeginPaint(hWnd, &ps);
  249.         // TODO: Add any drawing code here...
  250.         EndPaint(hWnd, &ps);
  251.         break;
  252.     case WM_DESTROY:
  253.         PostQuitMessage(0);
  254.         break;
  255.     default:
  256.         return DefWindowProc(hWnd, message, wParam, lParam);
  257.     }
  258.     return 0;
  259. }
  260.  
  261. // Message handler for about box.
  262. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  263. {
  264.     UNREFERENCED_PARAMETER(lParam);
  265.     switch (message)
  266.     {
  267.     case WM_INITDIALOG:
  268.         return (INT_PTR)TRUE;
  269.  
  270.     case WM_COMMAND:
  271.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  272.         {
  273.             EndDialog(hDlg, LOWORD(wParam));
  274.             return (INT_PTR)TRUE;
  275.         }
  276.         break;
  277.     }
  278.     return (INT_PTR)FALSE;
  279. }
Add Comment
Please, Sign In to add comment