Create_Accel.cpp
Upload User: dq031136
Upload Date: 2022-08-08
Package Size: 802k
Code Size: 8k
Development Platform:

C++ Builder

  1. #include <windows.h>  
  2. #include "Create_Accel.h" 
  3. #if defined (WIN32)
  4. #define IS_WIN32 TRUE
  5. #else
  6. #define IS_WIN32 FALSE
  7. #endif
  8. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  9. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  10. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  11. HINSTANCE hInst;   // current instance
  12. LPCTSTR lpszAppName  = "MyApp";
  13. LPCTSTR lpszTitle    = "My Application"; 
  14. HACCEL hAccel = NULL;
  15. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  16. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  17.                       LPTSTR lpCmdLine, int nCmdShow)
  18. {
  19.    MSG      msg;
  20.    HWND     hWnd; 
  21.    WNDCLASS wc;
  22.    // Register the main application window class.
  23.    //............................................
  24.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  25.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  26.    wc.cbClsExtra    = 0;                      
  27.    wc.cbWndExtra    = 0;                      
  28.    wc.hInstance     = hInstance;              
  29.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  30.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  31.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  32.    wc.lpszMenuName  = lpszAppName;              
  33.    wc.lpszClassName = lpszAppName;              
  34.    if ( IS_WIN95 )
  35.    {
  36.       if ( !RegisterWin95( &wc ) )
  37.          return( FALSE );
  38.    }
  39.    else if ( !RegisterClass( &wc ) )
  40.       return( FALSE );
  41.    hInst = hInstance; 
  42.    // Create the main application window.
  43.    //....................................
  44.    hWnd = CreateWindow( lpszAppName, 
  45.                         lpszTitle,    
  46.                         WS_OVERLAPPEDWINDOW, 
  47.                         CW_USEDEFAULT, 0, 
  48.                         CW_USEDEFAULT, 0,  
  49.                         NULL,              
  50.                         NULL,              
  51.                         hInstance,         
  52.                         NULL               
  53.                       );
  54.    if ( !hWnd ) 
  55.       return( FALSE );
  56.    ShowWindow( hWnd, nCmdShow ); 
  57.    UpdateWindow( hWnd );         
  58.    while( GetMessage( &msg, NULL, 0, 0) )   
  59.    {
  60.       if ( !hAccel || !TranslateAccelerator( hWnd, hAccel, &msg ) )
  61.       {
  62.          TranslateMessage( &msg ); 
  63.          DispatchMessage( &msg );  
  64.       }
  65.    }
  66.    return( msg.wParam ); 
  67. }
  68. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  69. {
  70.    WNDCLASSEX wcex;
  71.    wcex.style         = lpwc->style;
  72.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  73.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  74.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  75.    wcex.hInstance     = lpwc->hInstance;
  76.    wcex.hIcon         = lpwc->hIcon;
  77.    wcex.hCursor       = lpwc->hCursor;
  78.    wcex.hbrBackground = lpwc->hbrBackground;
  79.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  80.    wcex.lpszClassName = lpwc->lpszClassName;
  81.    // Added elements for Windows 95.
  82.    //...............................
  83.    wcex.cbSize = sizeof(WNDCLASSEX);
  84.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  85.                             IMAGE_ICON, 16, 16,
  86.                             LR_DEFAULTCOLOR );
  87.    return RegisterClassEx( &wcex );
  88. }
  89. #define IDM_NEWBASE 300
  90. VOID AddNewTestItem( HWND hWnd )
  91. {
  92. static int nNum = 0;
  93.    char   szMenuItem[20];
  94.    HMENU  hMenu      = GetMenu( hWnd );
  95.    ACCEL* pAccelData = NULL;
  96.    ACCEL* pCurAccel  = NULL;
  97.    HANDLE hAccelData = NULL;
  98.    int    nNumAccel  = 1;
  99.    // Maximum of 4 new items allowed.
  100.    //................................
  101.    if ( nNum == 4 )
  102.       return;
  103.    // If accelerator table exists, get the number of items.
  104.    //......................................................   
  105.    if ( hAccel )
  106.    {
  107.       nNumAccel = CopyAcceleratorTable( hAccel, NULL, 0 ) + 1;
  108.    }
  109.    // Allocate an array of ACCEL structures.
  110.    //.......................................
  111.    hAccelData = GlobalAlloc( GHND, sizeof(ACCEL) * nNumAccel );
  112.    if ( hAccelData )
  113.       pAccelData = (ACCEL*)GlobalLock( hAccelData );
  114.    // If an accelerator table exists, copy the items into
  115.    // the newly allocated array.
  116.    //....................................................
  117.    if ( hAccel && pAccelData )
  118.    {
  119.       CopyAcceleratorTable( hAccel, pAccelData, nNumAccel-1 );
  120.       DestroyAcceleratorTable( hAccel );
  121.       hAccel = NULL;
  122.    }
  123.    // Add the new menu option and accelerator key
  124.    //............................................
  125.    if ( pAccelData )
  126.    {
  127.       // Get a pointer to the new accelerator key in
  128.       // the array.
  129.       //............................................
  130.       pCurAccel = (ACCEL*)(pAccelData+nNumAccel-1);
  131.       // Create a new menu option on the menu.
  132.       //......................................
  133.       nNum++;
  134.       wsprintf( szMenuItem, "New Item&%d", nNum );
  135.       AppendMenu( hMenu, MFT_STRING, IDM_NEWBASE+nNum, szMenuItem );
  136.       DrawMenuBar( hWnd );
  137.       // Set up a new accelerator of F1,F2,F3,or F4 for the
  138.       // the new menu option.
  139.       //...................................................
  140.       pCurAccel->fVirt = FNOINVERT | FVIRTKEY;
  141.       pCurAccel->cmd   = IDM_NEWBASE+nNum;
  142.       pCurAccel->key   = ( nNum == 1 ? VK_F1 :
  143.                            nNum == 2 ? VK_F2 :
  144.                            nNum == 3 ? VK_F3 :
  145.                            /*default*/ VK_F4 );
  146.       // Create the new accelerator table.
  147.       //..................................
  148.       hAccel = CreateAcceleratorTable( pAccelData, nNumAccel );
  149.       GlobalUnlock( hAccelData );
  150.    }
  151.    if ( hAccelData )
  152.       GlobalFree( hAccelData );
  153. }
  154. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  155. {
  156.    switch( uMsg )
  157.    {
  158.       case WM_COMMAND :
  159.               switch( LOWORD( wParam ) )
  160.               {
  161.                  case IDM_TEST :
  162.                          AddNewTestItem( hWnd );
  163.                          break;
  164.                  case IDM_NEWBASE+1 :
  165.                          MessageBox( hWnd, "New Item 1 Selected", "Item Selected", 
  166.                                      MB_OK | MB_ICONINFORMATION );
  167.                          break;
  168.                  case IDM_NEWBASE+2 :
  169.                          MessageBox( hWnd, "New Item 2 Selected", "Item Selected", 
  170.                                      MB_OK | MB_ICONINFORMATION );
  171.                          break;
  172.                  case IDM_NEWBASE+3 :
  173.                          MessageBox( hWnd, "New Item 3 Selected", "Item Selected", 
  174.                                      MB_OK | MB_ICONINFORMATION );
  175.                          break;
  176.                  case IDM_NEWBASE+4 :
  177.                          MessageBox( hWnd, "New Item 4 Selected", "Item Selected", 
  178.                                      MB_OK | MB_ICONINFORMATION );
  179.                          break;
  180.                  case IDM_ABOUT :
  181.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  182.                         break;
  183.                  case IDM_EXIT :
  184.                         DestroyWindow( hWnd );
  185.                         break;
  186.               }
  187.               break;
  188.       
  189.       case WM_DESTROY :
  190.               PostQuitMessage(0);
  191.               break;
  192.       default :
  193.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  194.    }
  195.    return( 0L );
  196. }
  197. LRESULT CALLBACK About( HWND hDlg,           
  198.                         UINT message,        
  199.                         WPARAM wParam,       
  200.                         LPARAM lParam)
  201. {
  202.    switch (message) 
  203.    {
  204.        case WM_INITDIALOG: 
  205.                return (TRUE);
  206.        case WM_COMMAND:                              
  207.                if (   LOWORD(wParam) == IDOK         
  208.                    || LOWORD(wParam) == IDCANCEL)    
  209.                {
  210.                        EndDialog(hDlg, TRUE);        
  211.                        return (TRUE);
  212.                }
  213.                break;
  214.    }
  215.    return (FALSE); 
  216. }