
#include <windows.h>  
#include "CreatKey.h" 


HINSTANCE hInst;   // current instance

LPCTSTR lpszAppName  = "MyApp";
LPCTSTR lpszTitle    = "My Application"; 


int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
                      LPTSTR lpCmdLine, int nCmdShow)
{
   MSG        msg;
   HWND       hWnd; 
   WNDCLASSEX wc;

   // Register the main application window class.
   //............................................
   wc.style         = CS_HREDRAW | CS_VREDRAW;
   wc.lpfnWndProc   = (WNDPROC)WndProc;       
   wc.cbClsExtra    = 0;                      
   wc.cbWndExtra    = 0;                      
   wc.hInstance     = hInstance;              
   wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
   wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
   wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
   wc.lpszMenuName  = lpszAppName;              
   wc.lpszClassName = lpszAppName;              
   wc.cbSize        = sizeof(WNDCLASSEX);
   wc.hIconSm       = LoadImage(hInstance, lpszAppName, IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);

   if ( !RegisterClassEx( &wc ) )
      return( FALSE );

   hInst = hInstance; 

   // Create the main application window.
   //....................................
   hWnd = CreateWindow( lpszAppName, 
                        lpszTitle,    
                        WS_OVERLAPPEDWINDOW, 
                        CW_USEDEFAULT, 0, 
                        CW_USEDEFAULT, 0,  
                        NULL,              
                        NULL,              
                        hInstance,         
                        NULL               
                      );

   if ( !hWnd ) 
      return( FALSE );

   ShowWindow( hWnd, nCmdShow ); 
   UpdateWindow( hWnd );         

   while( GetMessage( &msg, NULL, 0, 0) )   
   {
      TranslateMessage( &msg ); 
      DispatchMessage( &msg );  
   }

   return( msg.wParam ); 
}


LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
   switch( uMsg )  
   {   
      case WM_COMMAND :
              switch( LOWORD( wParam ) )
              {
                 case IDM_REGISTER:
                       {
                          LONG  lResult;
                          HKEY  hKeyResult = 0;
                          DWORD dwDisposition = 0;

                          // Create "MyDoc" shell interface.
                          //................................
                          lResult = RegCreateKeyEx( HKEY_CLASSES_ROOT,
                                              "MyDoc\\shell\\Test\\command", 0, 
                                              "", REG_OPTION_VOLATILE, KEY_ALL_ACCESS,
                                              NULL, &hKeyResult, &dwDisposition );

                          // Use Notepad to open the document.
                          //..................................
                          if ( lResult == ERROR_SUCCESS )
                          {
                             lResult = RegSetValueEx( hKeyResult, NULL, 0, REG_SZ,
                                                      "NotePad %1", 11 );
                             RegCloseKey( hKeyResult );   
                          }

                          // Create the DefaultIcon for MyDoc
                          //.................................
                          lResult = RegCreateKeyEx( HKEY_CLASSES_ROOT,
                                              "MyDoc\\DefaultIcon", 0, 
                                              "", REG_OPTION_VOLATILE, KEY_ALL_ACCESS,
                                              NULL, &hKeyResult, &dwDisposition );


                          // Set the default icon.
                          //......................
                          if ( lResult == ERROR_SUCCESS )
                          {
                             lResult = RegSetValueEx( hKeyResult, NULL, 0, REG_SZ,
                                                      "%SystemRoot%\\system32\\shell32.dll,1", 36 );
                             RegCloseKey( hKeyResult );   
                          }

                          // Create .MDC file extension connection.
                          //.......................................
                          lResult = RegCreateKeyEx( HKEY_CLASSES_ROOT, ".MDC",
                                                    0, "", REG_OPTION_VOLATILE,
                                                    KEY_ALL_ACCESS, NULL,
                                                    &hKeyResult, &dwDisposition );

                          // Connect .MDC with MyDoc type.
                          //..............................
                          if ( lResult == ERROR_SUCCESS )
                          {
                             lResult = RegSetValueEx( hKeyResult, NULL, 0, REG_SZ, 
                                                      "MyDoc", 6 );
                             RegCloseKey( hKeyResult );
                          }

                          MessageBox( hWnd, "MyDoc (.MDC) has been registered in the registry", 
                                            "RegCreateKeyEx()", MB_OK | MB_ICONINFORMATION );
                       }
                       break;

                 case IDM_UNREGISTER :
                       {
                          HKEY  hKeyResult = 0;
                          LONG  lResult = RegOpenKeyEx( HKEY_CLASSES_ROOT,
                                                         "MyDoc\\shell\\Test\\command",
                                                         0, KEY_ALL_ACCESS, &hKeyResult );
                          // Delete default value.
                          //......................
                          if ( lResult == ERROR_SUCCESS ) 
                          {
                             lResult = RegDeleteValue( hKeyResult, NULL );
                             RegCloseKey( hKeyResult );
                          }

                          lResult = RegOpenKeyEx( HKEY_CLASSES_ROOT,
                                                         "MyDoc\\DefaultIcon",
                                                         0, KEY_ALL_ACCESS, &hKeyResult );
                          // Delete default value.
                          //......................
                          if ( lResult == ERROR_SUCCESS ) 
                          {
                             lResult = RegDeleteValue( hKeyResult, NULL );
                             RegCloseKey( hKeyResult );
                          }

                          // Remove the keys.. All subkeys must be removed before
                          // the parent keys can be removed. This is a limitation 
                          // of the RegDeleteKey() function.
                          //.....................................................
                          RegDeleteKey( HKEY_CLASSES_ROOT, "MyDoc\\shell\\Test\\command" );
                          RegDeleteKey( HKEY_CLASSES_ROOT, "MyDoc\\shell\\Test" );
                          RegDeleteKey( HKEY_CLASSES_ROOT, "MyDoc\\shell" );
                          RegDeleteKey( HKEY_CLASSES_ROOT, "MyDoc\\DefaultIcon" );
                          RegDeleteKey( HKEY_CLASSES_ROOT, "MyDoc" );
                          RegDeleteKey( HKEY_CLASSES_ROOT, ".MDC" );

                          MessageBox( hWnd, "MyDoc (.MDC) has been removed from the registry", 
                                            "RegCreateKeyEx()", MB_OK | MB_ICONINFORMATION );
                       }
                       break;


                 case IDM_ABOUT :
                        DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
                        break;

                 case IDM_EXIT :
                        DestroyWindow( hWnd );
                        break;
              }
              break;
      
      case WM_DESTROY :
              PostQuitMessage(0);
              break;

      default :
            return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
   }

   return( 0L );
}


LRESULT CALLBACK About( HWND hDlg,           
                        UINT message,        
                        WPARAM wParam,       
                        LPARAM lParam)
{
   switch (message) 
   {
       case WM_INITDIALOG: 
               return (TRUE);

       case WM_COMMAND:                              
               if (   LOWORD(wParam) == IDOK         
                   || LOWORD(wParam) == IDCANCEL)    
               {
                       EndDialog(hDlg, TRUE);        
                       return (TRUE);
               }
               break;
   }

   return (FALSE); 
}
