home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / dbmsg / mapi / simple.frm / main.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-11  |  7.7 KB  |  316 lines

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  FILE:           MAIN.CPP
  4. //
  5. //  DESCRIPTION:    This file contains the code for the entry function and
  6. //                      the main window for this form
  7. //
  8. //
  9. //  Copyright (c) 1995, Microsoft Corporation.
  10. //  All rights reserved.
  11. //
  12. //--
  13.  
  14. #include "precomp.h"
  15. #include <wrap3d.h>
  16.  
  17. // Just to make it easier to determine appropriate code paths:
  18. #if defined (_WIN32) // This had better be true, but just in case...
  19.     #define IS_WIN32 TRUE
  20. #else
  21.     #define IS_WIN32 FALSE
  22. #endif
  23. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  24. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  25. #define IS_CHICAGO (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  26.  
  27. ///////////////////////// Global Variables /////////////////////////////////
  28.  
  29. extern CClassFactory   ClassFactory;
  30. HINSTANCE       g_hinst;
  31. char            SzAppName[] = "SimpFrm";  // The name of this application
  32. char            g_szSendWinClass[] = "SampleFormSend";
  33. char            g_szReadWinClass[] = "SampleFormRead";
  34. char            g_szFormName[] = "Simple Sample Form";
  35. char            g_szWindowCaption[] = "Sample Form";
  36. CBaseForm *     g_PfrmFirst = NULL;
  37. LPVOID          g_lpCtl3d = NULL;           //  CTL3D context
  38.  
  39. //// Internal Flags
  40.  
  41. ULONG           UlCoRegForm = 0;
  42. BOOL            FMapiInitialized = FALSE;
  43.  
  44. //we have to create an atom containig string representation of
  45. // the class id;  the atom has to have the same live time as the
  46. //ClassFactory object
  47. static ATOM     atmCFAlive; 
  48.  
  49. //////////////////////////// Extern and forward declarations //////////////
  50.  
  51. LRESULT CALLBACK WndProcMain(HWND, UINT, WPARAM, LPARAM);
  52. LRESULT CALLBACK WndProcForm(HWND, UINT, WPARAM, LPARAM);
  53.  
  54. ////    InitApplication
  55. //
  56.  
  57. BOOL InitApplication(HINSTANCE hInstance)
  58. {
  59.     WNDCLASSEX  wc;
  60.  
  61.     //  Fill in the window class structure with parameters that describe
  62.     //  the main window.  This window will never be displayed and exists
  63.     //  so that OLE can talk to us.
  64.  
  65.     wc.cbSize           = sizeof(wc);
  66.     wc.style            = 0;
  67.     wc.lpfnWndProc      = (WNDPROC) WndProcMain;
  68.     wc.cbClsExtra       = 0;
  69.     wc.cbWndExtra       = 0;
  70.     wc.hInstance        = hInstance;
  71.     wc.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  72.     wc.hCursor          = ::LoadCursor(NULL, IDC_ARROW);
  73.     wc.hbrBackground    = (HBRUSH) (COLOR_WINDOW+1);
  74.     wc.lpszMenuName     = NULL;
  75.     wc.lpszClassName    = SzAppName;
  76.     wc.hIconSm          = LoadIcon(hInstance, "SmallIcon");
  77.     
  78.     //  Register the window class.
  79.     if (!RegisterClassEx(&wc))
  80.     {
  81.         return FALSE;
  82.     }
  83.  
  84.     //  Now setup and register the acutual window class used for the
  85.     //  form.
  86.  
  87.     wc.style            = CS_HREDRAW | CS_VREDRAW;
  88.     wc.lpszClassName    = g_szSendWinClass;
  89.     wc.lpfnWndProc      = (WNDPROC) WndProcForm;
  90.     wc.cbWndExtra       = 4;
  91.     wc.hIcon            = LoadIcon(hInstance, "IconL");
  92. //    if (IS_CHICAGO) {
  93. //        wc.lpszMenuName = "CHICAGO";
  94. //    }
  95. //    else {
  96.         wc.lpszMenuName = MAKEINTRESOURCE(IDR_SEND_FORM);
  97. //    }
  98.  
  99.     if (!RegisterClassEx(&wc))
  100.     {
  101.         return FALSE;
  102.     }
  103.  
  104.     //  Now setup and register the acutual window class used for the
  105.     //  form.
  106.  
  107.     wc.style            = CS_HREDRAW | CS_VREDRAW;
  108.     wc.lpszClassName    = g_szReadWinClass;
  109.     wc.lpfnWndProc      = (WNDPROC) WndProcForm;
  110.     wc.cbWndExtra       = 4;
  111.     wc.hIcon            = LoadIcon(hInstance, "IconL");
  112. //    if (IS_CHICAGO) {
  113. //        wc.lpszMenuName = "CHICAGO";
  114. //    }
  115. //    else {
  116.         wc.lpszMenuName = MAKEINTRESOURCE(IDR_READ_FORM);
  117. //    }
  118.  
  119.     return RegisterClassEx(&wc);
  120. }
  121.  
  122. ////    InitInstance
  123. //
  124.  
  125. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  126. {
  127.     HRESULT             hr;
  128.     ULONG               ulVer;
  129.     HWND                hWnd;
  130.     BOOL                f;
  131.  
  132.     ///////////////////////////////////////////////////////////////////////
  133.     //
  134.     //  Initialize OLE and MAPI
  135.  
  136.     ulVer = CoBuildVersion ();
  137. #if 0          
  138.     if (HIWORD (ulVer) != rmm)
  139.     {
  140.         return FALSE;
  141.     }
  142. #endif // 0
  143.                      
  144.     //
  145.     //  Initialize MAPI -- this implies a successfull initialization of
  146.     //  OLE on success
  147.     //
  148.         
  149.     hr = MAPIInitialize (NULL);
  150.     if (HR_FAILED(hr))
  151.     {
  152.         return FALSE;
  153.     }
  154.     FMapiInitialized = TRUE;
  155.  
  156.     //
  157.     //  Give ourselves the 3D look
  158.     //
  159.     
  160.     if (!g_lpCtl3d)
  161.     {
  162.         g_lpCtl3d = CTL3D_Initialize(hInstance);
  163.         if (g_lpCtl3d)
  164.             CTL3D_AutoSubclass(g_lpCtl3d, hInstance, &f);
  165.     }
  166.         
  167.         
  168.     //Create global atom with string representaton of our class id
  169.     if(0 == FindAtom(szGUID))
  170.         atmCFAlive = GlobalAddAtom(szGUID);
  171.     
  172.     
  173.     //
  174.     //  Register our class factory for the form
  175.     //
  176.     
  177.     hr = CoRegisterClassObject(CLSID_IPM_NOTE_SAMPLE, &ClassFactory,
  178.                                CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE,
  179.                                &UlCoRegForm);
  180.     if (FAILED(hr))
  181.     {
  182.         return FALSE;
  183.     }
  184.         
  185.     //
  186.     //  Create a main window -- This window is used for dealing with
  187.     //          messages coming from the top of the work and consitutes
  188.     //          our main window.  We create this window of size zero and
  189.     //          not visible since it is not the window the user
  190.     //          interacts with
  191.  
  192.     hWnd = CreateWindow(SzAppName, NULL, WS_OVERLAPPEDWINDOW,
  193.                         0, 0, 0, 0, NULL, NULL, hInstance, NULL);
  194.     if (!hWnd)
  195.     {
  196.         return FALSE;
  197.     }
  198.  
  199.     ClassFactory.ms_hWndMain = hWnd;
  200.     g_hinst = hInstance;
  201.  
  202.     return TRUE;
  203. }
  204.  
  205.  
  206. int ExitInstance ()
  207. {
  208.     //
  209.     //  Revoke all classes factories here so we don't get any new
  210.     //  creates
  211.  
  212.     if (UlCoRegForm != 0)
  213.     {
  214.         CoRevokeClassObject (UlCoRegForm);
  215.         UlCoRegForm = 0;
  216.     }
  217.  
  218.     //
  219.     //  Shut down CTL3D
  220.  
  221.     if (g_lpCtl3d)
  222.     {
  223.         CTL3D_Uninitialize(g_lpCtl3d);
  224.         g_lpCtl3d = NULL;
  225.     }
  226.  
  227.     //
  228.     //  Uninitialize MAPI
  229.  
  230.     if (FMapiInitialized)
  231.     {
  232.         MAPIUninitialize();
  233.         FMapiInitialized = FALSE;
  234.     }
  235.  
  236.     GlobalDeleteAtom(atmCFAlive);
  237.  
  238.     return 0;
  239. }
  240.  
  241.  
  242. ////    WinMain
  243. //
  244.  
  245. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  246.                      LPSTR /*lpCmdLine*/, int nCmdShow)
  247. {
  248.     MSG         msg;
  249.  
  250.     if (!hPrevInstance)
  251.     {
  252.         // Perform instance initialization
  253.         if (!InitApplication(hInstance))
  254.         {
  255.             return FALSE;
  256.         }
  257.     }
  258.  
  259.     // Perform application initialization
  260.     if (!InitInstance(hInstance, nCmdShow))
  261.     {
  262.         return FALSE;
  263.     }
  264.  
  265.     
  266.     //  Main message loop
  267.     while (GetMessage(&msg, NULL, 0, 0))
  268.     {
  269.         CBaseForm *     pfrm;
  270.         
  271.         //
  272.         //  Find which top level window this message is for
  273.         //
  274.  
  275.         for (pfrm = g_PfrmFirst; pfrm != NULL; pfrm = pfrm->GetNext())
  276.         {
  277.             if (pfrm->TranslateMessage(msg))
  278.             {
  279.                 break;
  280.             }
  281.         }
  282.  
  283.         if (pfrm == NULL)
  284.         {
  285.             TranslateMessage(&msg);
  286.             DispatchMessage(&msg);
  287.         }
  288.     }
  289.  
  290.     ExitInstance();
  291.  
  292.     return msg.wParam;
  293. }
  294.  
  295.  
  296.  
  297. ////  A specialized version of the window proc which plays how messages
  298. //      are going to be handled.
  299. //   
  300.  
  301. LRESULT CALLBACK WndProcMain(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  302. {
  303.    switch (msg)
  304.    {
  305.    case WM_DESTROY:
  306.        PostQuitMessage(0);
  307.        break;
  308.  
  309.    default:
  310.        return DefWindowProc (hwnd, msg, wParam, lParam);
  311.    }
  312.  
  313.    return 0;
  314. }
  315.  
  316.