Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.34 KB | None | 0 0
  1. // Podgotovka za TESTA.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Podgotovka za TESTA.h"
  6. #include "strsafe.h"//added by me
  7. #include <string>//added by me
  8. #define MAX_LOADSTRING 100
  9.  
  10. // Global Variables:
  11. HINSTANCE hInst;                                // current instance
  12. TCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
  13. TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
  14.  
  15. // Forward declarations of functions included in this code module:
  16. ATOM                MyRegisterClass(HINSTANCE hInstance);
  17. BOOL                InitInstance(HINSTANCE, int);
  18. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  19. INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
  20. INT_PTR CALLBACK TestPROBA(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
  21.  
  22.  
  23. int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
  24.                      _In_opt_ HINSTANCE hPrevInstance,
  25.                      _In_ LPTSTR    lpCmdLine,
  26.                      _In_ int       nCmdShow)
  27. {
  28.     UNREFERENCED_PARAMETER(hPrevInstance);
  29.     UNREFERENCED_PARAMETER(lpCmdLine);
  30.  
  31.     // TODO: Place code here.
  32.     MSG msg;
  33.     HACCEL hAccelTable;
  34.  
  35.     // Initialize global strings
  36.     LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  37.     LoadString(hInstance, IDC_PODGOTOVKAZATESTA, szWindowClass, MAX_LOADSTRING);
  38.     MyRegisterClass(hInstance);
  39.  
  40.     // Perform application initialization:
  41.     if (!InitInstance (hInstance, nCmdShow))
  42.     {
  43.         return FALSE;
  44.     }
  45.  
  46.     hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_PODGOTOVKAZATESTA));
  47.  
  48.     // Main message loop:
  49.     while (GetMessage(&msg, NULL, 0, 0))
  50.     {
  51.         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  52.         {
  53.             TranslateMessage(&msg);
  54.             DispatchMessage(&msg);
  55.         }
  56.     }
  57.  
  58.     return (int) msg.wParam;
  59. }
  60.  
  61.  
  62.  
  63. //
  64. //  FUNCTION: MyRegisterClass()
  65. //
  66. //  PURPOSE: Registers the window class.
  67. //
  68. ATOM MyRegisterClass(HINSTANCE hInstance)
  69. {
  70.     WNDCLASSEX wcex;
  71.  
  72.     wcex.cbSize = sizeof(WNDCLASSEX);
  73.  
  74.     wcex.style          = CS_HREDRAW | CS_VREDRAW;
  75.     wcex.lpfnWndProc    = WndProc;
  76.     wcex.cbClsExtra     = 0;
  77.     wcex.cbWndExtra     = 0;
  78.     wcex.hInstance      = hInstance;
  79.     wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_PODGOTOVKAZATESTA));
  80.     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
  81.     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
  82.     wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_PODGOTOVKAZATESTA);
  83.     wcex.lpszClassName  = szWindowClass;
  84.     wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  85.  
  86.     return RegisterClassEx(&wcex);
  87. }
  88.  
  89. //
  90. //   FUNCTION: InitInstance(HINSTANCE, int)
  91. //
  92. //   PURPOSE: Saves instance handle and creates main window
  93. //
  94. //   COMMENTS:
  95. //
  96. //        In this function, we save the instance handle in a global variable and
  97. //        create and display the main program window.
  98. //
  99. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  100. {
  101.    HWND hWnd;
  102.  
  103.    hInst = hInstance; // Store instance handle in our global variable
  104.  
  105.    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  106.       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  107.  
  108.    if (!hWnd)
  109.    {
  110.       return FALSE;
  111.    }
  112.  
  113.    ShowWindow(hWnd, nCmdShow);
  114.    UpdateWindow(hWnd);
  115.  
  116.    return TRUE;
  117. }
  118.  
  119. //
  120. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  121. //
  122. //  PURPOSE:  Processes messages for the main window.
  123. //
  124. //  WM_COMMAND  - process the application menu
  125. //  WM_PAINT    - Paint the main window
  126. //  WM_DESTROY  - post a quit message and return
  127. //
  128. //
  129. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  130. {
  131.     int wmId, wmEvent;
  132.     PAINTSTRUCT ps;
  133.     HDC hdc;
  134.  
  135.  
  136.     POINT pt;
  137.     HMENU hMenu, hSubMenu, hPopUpMenu;
  138.     DWORD dwRes;
  139.     BOOL bRes;
  140.     UINT uRes, uResPopUp;
  141.  
  142.     switch (message)
  143.     {
  144.     case WM_COMMAND:
  145.         wmId    = LOWORD(wParam);
  146.         wmEvent = HIWORD(wParam);
  147.         // Parse the menu selections:
  148.         switch (wmId)
  149.         {
  150.         case IDM_MSGBOX:
  151.             MessageBox(hWnd, L"Message", L"MSG", MB_OK);
  152.             break;
  153.  
  154.         case IDM_DIALOGPROBA:
  155.             DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG), hWnd, TestPROBA);
  156.             break;
  157.  
  158.         case IDM_ENABLE_DISABLE:
  159.             hMenu = GetMenu(hWnd);
  160.             uRes = GetMenuState(hMenu, IDM_E_D, MF_BYCOMMAND);
  161.             bRes = EnableMenuItem(hMenu, IDM_E_D, (uRes & MF_DISABLED) ? MF_ENABLED : MF_DISABLED);
  162.             break;
  163.  
  164.         case IDM_CHECK_UNCHECK://we chech C/U in the menu
  165.             hMenu = GetMenu(hWnd);
  166.             dwRes = CheckMenuItem(hMenu, IDM_C_U, MF_BYCOMMAND);
  167.             if (dwRes == CheckMenuItem(hMenu, IDM_C_U, MF_CHECKED)) {
  168.                 CheckMenuItem(hMenu, IDM_C_U, MF_CHECKED);
  169.             }
  170.             else CheckMenuItem(hMenu, IDM_C_U, MF_UNCHECKED);
  171.             break;
  172.  
  173.  
  174.         case IDM_A_D://iztrivam A/D kato go natisna
  175.             hMenu = GetMenu(hWnd);
  176.             bRes = DeleteMenu(hMenu, IDM_A_D, MF_BYCOMMAND);
  177.             break;
  178.  
  179.         case IDM_ADD_DELETE://dobavqm A/D kato natisna Add/Delete,kato se vuzstanovqva purvonachalnoto menu
  180.             hMenu = GetMenu(hWnd);
  181.             hSubMenu = GetSubMenu(hMenu, 3);
  182.             if (GetMenuState(hMenu, IDM_A_D, MF_BYCOMMAND) == -1) {
  183.                 MENUITEMINFO miinf;
  184.                 ZeroMemory(&miinf, sizeof(miinf));
  185.                 miinf.cbSize = sizeof(miinf);
  186.                 miinf.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
  187.                 miinf.wID = IDM_A_D;
  188.                 miinf.fType = MFT_STRING;
  189.                 miinf.dwTypeData = (LPWSTR)_T("A/D");
  190.                 miinf.fState = MFS_ENABLED;
  191.                 bRes = InsertMenuItem(hMenu, IDM_ADD_DELETE, FALSE, &miinf);
  192.             }
  193.             break;
  194.  
  195.         case IDM_ABOUT:
  196.             DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  197.             break;
  198.         case IDM_EXIT:
  199.             DestroyWindow(hWnd);
  200.             break;
  201.         default:
  202.             return DefWindowProc(hWnd, message, wParam, lParam);
  203.         }
  204.         break;
  205.  
  206.     case WM_RBUTTONDOWN://s tozi case se pokazva PopUp menuto
  207.         hPopUpMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_POPUP_MENU));
  208.         hSubMenu = GetSubMenu(hPopUpMenu, 0);
  209.  
  210.         hMenu = GetMenu(hWnd);//taka se Desable-va E/D  osven v glavnoto menu a i v PopUp menuto
  211.         uResPopUp = GetMenuState(hMenu, IDM_E_D, MF_BYCOMMAND);
  212.         bRes = EnableMenuItem(hPopUpMenu, IDM_E_D, (!uResPopUp) ? MF_ENABLED : MF_DISABLED);
  213.  
  214.         pt = { LOWORD(lParam), HIWORD(lParam) };
  215.         ClientToScreen(hWnd, &pt);
  216.         TrackPopupMenu(hSubMenu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hWnd, NULL);
  217.         DestroyMenu(hPopUpMenu);
  218.         break;
  219.  
  220.    
  221.     case WM_PAINT:
  222.         hdc = BeginPaint(hWnd, &ps);
  223.         // TODO: Add any drawing code here...
  224.         EndPaint(hWnd, &ps);
  225.         break;
  226.     case WM_DESTROY:
  227.         PostQuitMessage(0);
  228.         break;
  229.     default:
  230.         return DefWindowProc(hWnd, message, wParam, lParam);
  231.     }
  232.     return 0;
  233. }
  234.  
  235. // Message handler for about box.
  236. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  237. {
  238.     UNREFERENCED_PARAMETER(lParam);
  239.     switch (message)
  240.     {
  241.     case WM_INITDIALOG:
  242.         return (INT_PTR)TRUE;
  243.  
  244.     case WM_COMMAND:
  245.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  246.         {
  247.             //int idMsg = MessageBox(hDlg, L"do you want to ext? Pls confirm", L"About exit", MB_ICONEXCLAMATION | MB_OKCANCEL);  //v About izliza subshtenie i pita dali da ostana
  248.             //if (idMsg == IDOK)
  249.             EndDialog(hDlg, LOWORD(wParam));
  250.             return (INT_PTR)TRUE;
  251.         }
  252.         break;
  253.     }
  254.     return (INT_PTR)FALSE;
  255. }
  256.  
  257.  
  258. INT_PTR CALLBACK TestPROBA(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) {//nova funkciq
  259.     WCHAR szText[MAX_LOADSTRING];
  260.    
  261.  
  262.     int intVal;//Извличане на текст – цяло число :
  263.     BOOL bSigned, bSuccess;
  264.  
  265.     double dVal;//Извличане на текст – реално число
  266.     std::string::size_type sz;
  267.    
  268.    
  269.     UNREFERENCED_PARAMETER(lParam);
  270.     switch (message)
  271.     {
  272.  
  273.     case WM_INITDIALOG:
  274.         return (INT_PTR)TRUE;
  275.  
  276.     case WM_COMMAND:
  277.         UINT status;
  278.         int wmId = LOWORD(wParam);
  279.         switch (wmId)
  280.         {
  281.         case IDC_BTN_PROBA://Извличане на текст-стринг: GetDlgItemText
  282.             if (!GetDlgItemText(hDlg, IDC_EDIT_EGN, szText, MAX_LOADSTRING))
  283.                 *szText = 0;
  284.             MessageBox(hDlg, szText, L"Info", MB_OK);
  285.             return (INT_PTR)TRUE;
  286.             break;  
  287.  
  288.         case IDC_BUTTON2://Извличане на текст – цяло число ,      pokazva go v Edit control
  289.             // requires #include "strsafe.h"
  290.             bSigned = IsDlgButtonChecked(hDlg, IDC_CB_HIDDEN);
  291.             intVal = GetDlgItemInt(hDlg, IDC_EDIT_EGN, &bSuccess, bSigned);
  292.             if (!bSuccess)
  293.             {
  294.                 MessageBox(hDlg, L"Error to extract integer value", L"Error!", MB_OK);
  295.                 SetFocus(GetDlgItem(hDlg, IDC_EDIT_EGN));
  296.             }
  297.             else
  298.             {
  299.                 StringCbPrintf(szText, ARRAYSIZE(szText),
  300.                     L"The value is \"%i\"", intVal);
  301.                 MessageBox(hDlg, szText, L"Done!", MB_OK);
  302.             }
  303.             return (INT_PTR)TRUE;
  304.  
  305.  
  306.         case IDC_BUTTON3://Извличане на текст – реално число, pri natiskane na Buton 3 pravi chisloto realno
  307.             // requires #include <string>
  308.             try {
  309.                 if (!GetDlgItemText(hDlg, IDC_EDIT_EGN, szText, MAX_LOADSTRING))
  310.                     *szText = 0;
  311.                 dVal = std::stod(szText, &sz);
  312.                 StringCbPrintf(szText, ARRAYSIZE(szText), L"The value is \"%f\"",
  313.                     dVal);
  314.                 MessageBox(hDlg, szText, L"Done!", MB_OK);
  315.             }
  316.             catch (std::exception& e){
  317.                 MessageBox(hDlg, L"Error to extract double value", L"Error!",
  318.                     MB_ICONERROR | MB_OK);
  319.                 dVal = 0.0;
  320.             }
  321.             return (INT_PTR)TRUE;
  322.             break;
  323.  
  324.         case IDC_BTN4:
  325.             //MessageBox(hDlg, L"text", L"caption", MB_OK);
  326.             //MessageBox(hDlg, L"text", L"caption", MB_OKCANCEL);
  327.             //MessageBox(hDlg, L"text", L"caption", MB_YESNO);
  328.             //MessageBox(hDlg, L"text", L"caption", MB_ICONWARNING);
  329.             //MessageBox(hDlg, L"text", L"caption", MB_ICONINFORMATION);
  330.             //MessageBox(hDlg, L"text", L"caption", MB_ICONQUESTION);
  331.             //MessageBox(hDlg, L"text", L"caption", MB_ICONSTOP);
  332.             MessageBox(hDlg, L"text", L"caption", MB_ICONHAND);
  333.  
  334.         case IDC_RB_ADULT://Установяване на текст - число : SetDlgItemInt
  335.         case IDC_RB_CHILD://Установяване на текст - число : SetDlgItemInt
  336.             SetDlgItemInt(hDlg, IDC_EDIT_EGN, IsDlgButtonChecked(hDlg, IDC_RB_ADULT), FALSE);
  337.             return (INT_PTR)TRUE;
  338.        
  339.  
  340.         case IDC_CB_ReadOnly:
  341.             status = IsDlgButtonChecked(hDlg, IDC_CB_ReadOnly);
  342.             MessageBox(hDlg, status == BST_CHECKED ? L"\"Checked" : L"\"Unchecked", L"Info", MB_OK);
  343.             return (INT_PTR)TRUE;
  344.             //MessageBox(hDlg, IsDlgButtonChecked(hDlg, IDC_CB_ReadOnly) ? L"Button is pressed" : L" Button is uncheckd", L"da", MB_OK);//kak se pravi MessageBox
  345.             break;
  346.    
  347.         case IDOK:
  348.         case IDCANCEL:
  349.             //int idMsg = MessageBox(hDlg, L"do you want to ext? Pls connnnnfirmmm", L"About exit", MB_ICONEXCLAMATION | MB_OKCANCEL);//pita me dali iskam da zatvorq DIALOGA
  350.             //if (idMsg == IDOK)
  351.             EndDialog(hDlg, LOWORD(wParam));
  352.             return (INT_PTR)TRUE;
  353.             break;
  354.         }
  355.     }
  356.     return (INT_PTR)FALSE;
  357. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement