Advertisement
Garey

Win32_Test1

Feb 13th, 2019
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.42 KB | None | 0 0
  1. // Win32Project8.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Win32Project8.h"
  6.  
  7. #define MAX_LOADSTRING 100
  8. #define IDM_USER 130
  9. #define IDM_ADMIN 131
  10. #define IDM_END 132
  11. #define ID_ROLES_SPECIAL 143
  12.  
  13. // Global Variables:
  14. HINSTANCE hInst;                                // current instance
  15. TCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
  16. TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
  17.  
  18. // Forward declarations of functions included in this code module:
  19. ATOM                MyRegisterClass(HINSTANCE hInstance);
  20. BOOL                InitInstance(HINSTANCE, int);
  21. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  22. INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
  23.  
  24. int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
  25.                      _In_opt_ HINSTANCE hPrevInstance,
  26.                      _In_ LPTSTR    lpCmdLine,
  27.                      _In_ int       nCmdShow)
  28. {
  29.     UNREFERENCED_PARAMETER(hPrevInstance);
  30.     UNREFERENCED_PARAMETER(lpCmdLine);
  31.  
  32.     // TODO: Place code here.
  33.     MSG msg;
  34.     HACCEL hAccelTable;
  35.  
  36.     // Initialize global strings
  37.     LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  38.     LoadString(hInstance, IDC_WIN32PROJECT8, szWindowClass, MAX_LOADSTRING);
  39.     MyRegisterClass(hInstance);
  40.  
  41.     // Perform application initialization:
  42.     if (!InitInstance (hInstance, nCmdShow))
  43.     {
  44.         return FALSE;
  45.     }
  46.  
  47.     hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT8));
  48.  
  49.     // Main message loop:
  50.     while (GetMessage(&msg, NULL, 0, 0))
  51.     {
  52.         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  53.         {
  54.             TranslateMessage(&msg);
  55.             DispatchMessage(&msg);
  56.         }
  57.     }
  58.  
  59.     return (int) msg.wParam;
  60. }
  61.  
  62.  
  63.  
  64. //
  65. //  FUNCTION: MyRegisterClass()
  66. //
  67. //  PURPOSE: Registers the window class.
  68. //
  69. ATOM MyRegisterClass(HINSTANCE hInstance)
  70. {
  71.     WNDCLASSEX wcex;
  72.  
  73.     wcex.cbSize = sizeof(WNDCLASSEX);
  74.  
  75.     wcex.style          = CS_HREDRAW | CS_VREDRAW;
  76.     wcex.lpfnWndProc    = WndProc;
  77.     wcex.cbClsExtra     = 0;
  78.     wcex.cbWndExtra     = 0;
  79.     wcex.hInstance      = hInstance;
  80.     wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT8));
  81.     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
  82.     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
  83.     wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_WIN32PROJECT8);
  84.     wcex.lpszClassName  = szWindowClass;
  85.     wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  86.  
  87.     return RegisterClassEx(&wcex);
  88. }
  89.  
  90. //
  91. //   FUNCTION: InitInstance(HINSTANCE, int)
  92. //
  93. //   PURPOSE: Saves instance handle and creates main window
  94. //
  95. //   COMMENTS:
  96. //
  97. //        In this function, we save the instance handle in a global variable and
  98. //        create and display the main program window.
  99. //
  100. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  101. {
  102.    HWND hWnd;
  103.  
  104.    hInst = hInstance; // Store instance handle in our global variable
  105.  
  106.    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  107.       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  108.  
  109.    if (!hWnd)
  110.    {
  111.       return FALSE;
  112.    }
  113.  
  114.    ShowWindow(hWnd, nCmdShow);
  115.    UpdateWindow(hWnd);
  116.  
  117.    return TRUE;
  118. }
  119.  
  120. //
  121. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  122. //
  123. //  PURPOSE:  Processes messages for the main window.
  124. //
  125. //  WM_COMMAND  - process the application menu
  126. //  WM_PAINT    - Paint the main window
  127. //  WM_DESTROY  - post a quit message and return
  128. //
  129. //
  130. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  131. {
  132.     int wmId, wmEvent;
  133.     PAINTSTRUCT ps;
  134.     HDC hdc;
  135.  
  136.     HMENU hMenu;
  137.     POINT point;
  138.  
  139.     switch (message)
  140.     {
  141.     case WM_COMMAND:
  142.         wmId    = LOWORD(wParam);
  143.         wmEvent = HIWORD(wParam);
  144.         // Parse the menu selections:
  145.         switch (wmId)
  146.         {
  147.         case IDM_ABOUT:
  148.             DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  149.             break;
  150.  
  151.         case IDM_EXIT:
  152.             DestroyWindow(hWnd);
  153.             break;
  154.  
  155.             /*! Right-click menu */
  156.         case IDM_USER:
  157.            
  158.             MessageBox(hWnd, L"User", L"User Box", MB_OK | MB_ICONINFORMATION);
  159.             break;
  160.                
  161.         case IDM_ADMIN:
  162.            
  163.             MessageBox(hWnd, L"Admin", L"Admin Box", MB_OK | MB_ICONINFORMATION);
  164.             break;
  165.  
  166.         case IDM_END:
  167.             SendMessage(hWnd, WM_CLOSE, 0, 0);
  168.             break;
  169.  
  170.             /* Main Menu Events */
  171.  
  172.         case ID_ROLES_USER:
  173.         {
  174.             auto hTMenu = GetMenu(hWnd);
  175.             //CheckMenuItem(hTMenu, ID_ROLES_USER, MF_CHECKED) ?  : CheckMenuItem(hTMenu, ID_ROLES_USER, MF_CHECKED);
  176.             if(CheckMenuItem(hTMenu, ID_ROLES_USER, MF_CHECKED)) {
  177.                 CheckMenuItem(hTMenu, ID_ROLES_USER, MF_UNCHECKED);
  178.                 CheckMenuItem(hTMenu, ID_ROLES_ADMIN, MF_CHECKED);
  179.             } else {
  180.                 CheckMenuItem(hTMenu, ID_ROLES_USER, MF_CHECKED);
  181.                 CheckMenuItem(hTMenu, ID_ROLES_ADMIN, MF_UNCHECKED);
  182.             }
  183.            
  184.             break;
  185.         }
  186.         case ID_ROLES_ADMIN:
  187.         {
  188.             auto hTMenu = GetMenu(hWnd);
  189.             MENUITEMINFO mii;
  190.  
  191.             if(CheckMenuItem(hTMenu, ID_ROLES_ADMIN, MF_CHECKED)) {
  192.                 CheckMenuItem(hTMenu, ID_ROLES_ADMIN, MF_UNCHECKED);
  193.                 CheckMenuItem(hTMenu, ID_ROLES_USER, MF_CHECKED);
  194.  
  195.                 mii.fState = MFS_ENABLED;
  196.  
  197.             } else {
  198.                 CheckMenuItem(hTMenu, ID_ROLES_ADMIN, MF_CHECKED);
  199.                 CheckMenuItem(hTMenu, ID_ROLES_USER, MF_UNCHECKED);
  200.  
  201.  
  202.  
  203.             }
  204.             break;
  205.         }
  206.  
  207.         case ID_ACTION_SEARCH:
  208.             break;
  209.         case ID_ROLES_NEW: {
  210.  
  211.             auto hTMenu = GetMenu(hWnd);
  212.  
  213.             if(GetMenuState(hTMenu, ID_ROLES_SPECIAL, MF_BYCOMMAND) == -1) {
  214.                     MENUITEMINFO mii;
  215.                     ZeroMemory(&mii, sizeof(MENUITEMINFO));
  216.  
  217.                     mii.cbSize = sizeof(mii);
  218.                     mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
  219.                     mii.fType = MFT_STRING;
  220.                     mii.fState = MFS_ENABLED;
  221.                     mii.wID = ID_ROLES_SPECIAL;
  222.                     mii.dwTypeData = L"Special Role";
  223.                    
  224.                     auto mHandler = MessageBoxW(hWnd, L"Are you sure?", L"WARNING!", MB_YESNO | MB_ICONWARNING);
  225.  
  226.                     if(mHandler == IDYES)
  227.                         InsertMenuItem(hTMenu, ID_ROLES_USER + 1, FALSE, &mii);
  228.                 } else
  229.                     MessageBoxW(hWnd, L"The item already exists!", L"WARNING", MB_OK | MB_ICONWARNING);
  230.                 break;
  231.             }
  232.         case ID_ROLES_DELETE: {
  233.             HMENU hTMenu = GetMenu(hWnd);
  234.  
  235.             if(!DeleteMenu(hTMenu, ID_ROLES_OLDROLE, MF_BYCOMMAND))
  236.                 MessageBoxW(hWnd, L"There is no old role!", L"WARNING", MB_OK | MB_ICONWARNING);
  237.             break;
  238.         }
  239.                 default:
  240.             return DefWindowProc(hWnd, message, wParam, lParam);
  241.         }
  242.        
  243.         break;
  244.  
  245.         case WM_RBUTTONUP:
  246.      
  247.           point.x = LOWORD(lParam);
  248.           point.y = HIWORD(lParam);
  249.        
  250.           hMenu = CreatePopupMenu();
  251.           ClientToScreen(hWnd, &point);
  252.  
  253.           AppendMenuW(hMenu, MF_STRING, ID_ROLES_USER, L"&User");
  254.           AppendMenuW(hMenu, MF_STRING, ID_ROLES_ADMIN, L"&Admin");
  255.           AppendMenuW(hMenu, MF_SEPARATOR, 0, NULL);
  256.           AppendMenuW(hMenu, MF_STRING, IDM_END, L"&End");
  257.                    
  258.           TrackPopupMenu(hMenu, TPM_RIGHTBUTTON, point.x, point.y, 0, hWnd, NULL);
  259.           DestroyMenu(hMenu);
  260.           break;
  261.  
  262.  
  263.     case WM_PAINT:
  264.         hdc = BeginPaint(hWnd, &ps);
  265.         // TODO: Add any drawing code here...
  266.         EndPaint(hWnd, &ps);
  267.         break;
  268.     case WM_DESTROY:
  269.         PostQuitMessage(0);
  270.         break;
  271.     default:
  272.         return DefWindowProc(hWnd, message, wParam, lParam);
  273.     }
  274.     return 0;
  275. }
  276.  
  277. // Message handler for about box.
  278. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  279. {
  280.     UNREFERENCED_PARAMETER(lParam);
  281.     switch (message)
  282.     {
  283.     case WM_INITDIALOG:
  284.         return (INT_PTR)TRUE;
  285.  
  286.     case WM_COMMAND:
  287.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  288.         {
  289.             EndDialog(hDlg, LOWORD(wParam));
  290.             return (INT_PTR)TRUE;
  291.         }
  292.         break;
  293.     }
  294.     return (INT_PTR)FALSE;
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement