home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap06 / objuser2 / objuser2.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  7KB  |  320 lines

  1. /*
  2.  * OBJUSER2.CPP
  3.  * Koala Client #2 Chapter 6
  4.  *
  5.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  6.  *
  7.  * Kraig Brockschmidt, Microsoft
  8.  * Internet  :  kraigb@microsoft.com
  9.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  10.  */
  11.  
  12.  
  13. #define INITGUIDS
  14. #include "objuser2.h"
  15.  
  16.  
  17. /*
  18.  * WinMain
  19.  *
  20.  * Purpose:
  21.  *  Main entry point of application.
  22.  */
  23.  
  24. int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hInstPrev
  25.     , LPSTR pszCmdLine, int nCmdShow)
  26.     {
  27.     MSG         msg;
  28.     PAPP        pApp;
  29.  
  30.     SETMESSAGEQUEUE;
  31.  
  32.     pApp=new CApp(hInst, hInstPrev, nCmdShow);
  33.  
  34.     if (NULL==pApp)
  35.         return -1;
  36.  
  37.     if (pApp->Init())
  38.         {
  39.         while (GetMessage(&msg, NULL, 0,0 ))
  40.             {
  41.             TranslateMessage(&msg);
  42.             DispatchMessage(&msg);
  43.             }
  44.         }
  45.  
  46.     delete pApp;
  47.     return msg.wParam;
  48.     }
  49.  
  50.  
  51.  
  52.  
  53.  
  54. /*
  55.  * ObjectUserWndProc
  56.  *
  57.  * Purpose:
  58.  *  Window class procedure.  Standard callback.
  59.  */
  60.  
  61. LRESULT APIENTRY ObjectUserWndProc(HWND hWnd, UINT iMsg
  62.     , WPARAM wParam, LPARAM lParam)
  63.     {
  64.     HRESULT         hr;
  65.     PAPP            pApp;
  66.     IUnknown       *pUnk;
  67.     IPersist       *pIPersist;
  68.  
  69.     pApp=(PAPP)GetWindowLong(hWnd, OBJUSERWL_STRUCTURE);
  70.  
  71.     switch (iMsg)
  72.         {
  73.         case WM_NCCREATE:
  74.             pApp=(PAPP)(((LPCREATESTRUCT)lParam)->lpCreateParams);
  75.             SetWindowLong(hWnd, OBJUSERWL_STRUCTURE, (LONG)pApp);
  76.             return (DefWindowProc(hWnd, iMsg, wParam, lParam));
  77.  
  78.         case WM_DESTROY:
  79.             PostQuitMessage(0);
  80.             break;
  81.  
  82.         case WM_COMMAND:
  83.             if (NULL!=pApp)
  84.                 pUnk=pApp->m_pIUnknown;
  85.  
  86.             switch (LOWORD(wParam))
  87.                 {
  88.                 case IDM_OBJECTCREATE:
  89.                     if (NULL!=pUnk)
  90.                         ReleaseInterface(pApp->m_pIUnknown);
  91.  
  92.                     hr=CoCreateInstance(CLSID_Koala, NULL
  93.                         , CLSCTX_LOCAL_SERVER, IID_IUnknown
  94.                         , (PPVOID)&pApp->m_pIUnknown);
  95.  
  96.                     if (SUCCEEDED(hr))
  97.                         pApp->Message(TEXT("Creation succeeded"));
  98.                     else
  99.                         pApp->Message(TEXT("Creation failed"));
  100.  
  101.                     break;
  102.  
  103.  
  104.                 case IDM_OBJECTGETCLASSID:
  105.                     if (NULL==pUnk)
  106.                         {
  107.                         pApp->Message(TEXT("There is no object"));
  108.                         break;
  109.                         }
  110.  
  111.                     if (SUCCEEDED(pUnk->QueryInterface(IID_IPersist
  112.                         , (void **)&pIPersist)))
  113.                         {
  114.                         CLSID       clsID;
  115.                         HRESULT     hr;
  116.                         TCHAR       szMsg[256];
  117.  
  118.                         hr=pIPersist->GetClassID(&clsID);
  119.                         pIPersist->Release();
  120.  
  121.                         wsprintf(szMsg
  122.                             , TEXT("IPersist::GetClassID returned 0x%lX")
  123.                             , (DWORD)hr);
  124.                         pApp->Message(szMsg);
  125.                         }
  126.                     else
  127.                         pApp->Message(TEXT("IPersist not available"));
  128.  
  129.                     break;
  130.  
  131.  
  132.                 case IDM_OBJECTRELEASE:
  133.                     if (NULL==pUnk)
  134.                         {
  135.                         pApp->Message(TEXT("There is no object"));
  136.                         break;
  137.                         }
  138.  
  139.                     ReleaseInterface(pApp->m_pIUnknown);
  140.                     pApp->Message(TEXT("Object released"));
  141.                     break;
  142.  
  143.  
  144.                 case IDM_OBJECTEXIT:
  145.                     PostMessage(hWnd, WM_CLOSE, 0, 0L);
  146.                     break;
  147.                 }
  148.             break;
  149.  
  150.         default:
  151.             return (DefWindowProc(hWnd, iMsg, wParam, lParam));
  152.         }
  153.  
  154.     return 0L;
  155.     }
  156.  
  157.  
  158.  
  159.  
  160.  
  161. /*
  162.  * CApp::CApp
  163.  * CApp::~CApp
  164.  *
  165.  * Constructor Parameters: (from WinMain)
  166.  *  hInst           HINSTANCE of the application.
  167.  *  hInstPrev       HINSTANCE of a previous instance.
  168.  *  nCmdShow        UINT specifying how to show the app window.
  169.  *
  170.  */
  171.  
  172. CApp::CApp(HINSTANCE hInst, HINSTANCE hInstPrev
  173.     , UINT nCmdShow)
  174.     {
  175.     m_hInst       =hInst;
  176.     m_hInstPrev   =hInstPrev;
  177.     m_nCmdShow    =nCmdShow;
  178.  
  179.     m_hWnd        =NULL;
  180.  
  181.     m_pIUnknown   =NULL;
  182.     m_fInitialized=FALSE;
  183.  
  184.     m_pMsgFilter  =NULL;
  185.     return;
  186.     }
  187.  
  188.  
  189. CApp::~CApp(void)
  190.     {
  191.     if (NULL!=m_pMsgFilter)
  192.         {
  193.         CoRegisterMessageFilter(NULL, NULL);
  194.         ReleaseInterface(m_pMsgFilter);
  195.         }
  196.  
  197.     ReleaseInterface(m_pIUnknown);
  198.  
  199.     if (IsWindow(m_hWnd))
  200.         DestroyWindow(m_hWnd);
  201.  
  202.     if (m_fInitialized)
  203.         CoUninitialize();
  204.  
  205.     return;
  206.     }
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214. /*
  215.  * CApp::Init
  216.  *
  217.  * Purpose:
  218.  *  Initializes an CApp object by registering window classes,
  219.  *  creating the main window, and doing anything else prone to
  220.  *  failure such as calling CoInitialize.  If this function fails
  221.  *  the caller should insure that the destructor is called.
  222.  *
  223.  * Parameters:
  224.  *  None
  225.  *
  226.  * Return Value:
  227.  *  BOOL            TRUE if successful, FALSE otherwise.
  228.  */
  229.  
  230. BOOL CApp::Init(void)
  231.     {
  232.     WNDCLASS    wc;
  233.  
  234.     CHECKVER_COM;
  235.  
  236.     if (FAILED(CoInitialize(NULL)))
  237.         return FALSE;
  238.  
  239.     m_fInitialized=TRUE;
  240.  
  241.     if (!m_hInstPrev)
  242.         {
  243.         wc.style          = CS_HREDRAW | CS_VREDRAW;
  244.         wc.lpfnWndProc    = ObjectUserWndProc;
  245.         wc.cbClsExtra     = 0;
  246.         wc.cbWndExtra     = CBWNDEXTRA;
  247.         wc.hInstance      = m_hInst;
  248.         wc.hIcon          = LoadIcon(m_hInst, TEXT("Icon"));
  249.         wc.hCursor        = LoadCursor(NULL, IDC_ARROW);
  250.         wc.hbrBackground  = (HBRUSH)(COLOR_WINDOW + 1);
  251.         wc.lpszMenuName   = MAKEINTRESOURCE(IDR_MENU);
  252.         wc.lpszClassName  = TEXT("OBJUSER2");
  253.  
  254.         if (!RegisterClass(&wc))
  255.             return FALSE;
  256.         }
  257.  
  258.     m_hWnd=CreateWindow(TEXT("OBJUSER2")
  259.         , TEXT("Koala Client #2"), WS_OVERLAPPEDWINDOW
  260.         , 35, 35, 350, 250, NULL, NULL, m_hInst, this);
  261.  
  262.     if (NULL==m_hWnd)
  263.         return FALSE;
  264.  
  265.  
  266.     //Create and register a message filter.
  267.     m_pMsgFilter=new CMessageFilter(this);
  268.  
  269.     if (NULL!=m_pMsgFilter)
  270.         {
  271.         m_pMsgFilter->AddRef();
  272.  
  273.         if (FAILED(CoRegisterMessageFilter(m_pMsgFilter, NULL)))
  274.             ReleaseInterface(m_pMsgFilter);
  275.         }
  276.  
  277.     ShowWindow(m_hWnd, m_nCmdShow);
  278.     UpdateWindow(m_hWnd);
  279.  
  280.     return TRUE;
  281.     }
  282.  
  283.  
  284.  
  285. /*
  286.  * CApp::Message
  287.  *
  288.  * Purpose:
  289.  *  Displays a message in the client area of the window.  This is
  290.  *  just to centralize the call to simpify other code.
  291.  *
  292.  * Parameters:
  293.  *  psz             LPTSTR to the string to display.
  294.  *
  295.  * Return Value:
  296.  *  None
  297.  */
  298.  
  299. void CApp::Message(LPTSTR psz)
  300.     {
  301.     HDC     hDC;
  302.     RECT    rc;
  303.  
  304.     hDC=GetDC(m_hWnd);
  305.     GetClientRect(m_hWnd, &rc);
  306.  
  307.     SetBkColor(hDC, GetSysColor(COLOR_WINDOW));
  308.     SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));
  309.  
  310.     /*
  311.      * We'll just be sloppy and clear the whole window as
  312.      * well as write the string with one ExtTextOut call.
  313.      * No word wrapping here...
  314.      */
  315.  
  316.     ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rc, psz, lstrlen(psz), NULL);
  317.     ReleaseDC(m_hWnd, hDC);
  318.     return;
  319.     }
  320.