REUSE.CPP
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:9k
源码类别:

Windows编程

开发平台:

Visual C++

  1. /*
  2.  * REUSE.CPP
  3.  * Demonstration of COM Reusability mechanisms.
  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. #define INITGUIDS
  12. #include <windows.h>
  13. #include "reuse.h"
  14. /*
  15.  * WinMain
  16.  *
  17.  * Purpose:
  18.  *  Main entry point of application.
  19.  */
  20. int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hInstPrev
  21.     , LPSTR pszCmdLine, int nCmdShow)
  22.     {
  23.     MSG     msg;
  24.     PAPP    pApp;
  25.     pApp=new CApp(hInst, hInstPrev, nCmdShow);
  26.     if (NULL==pApp)
  27.         return -1;
  28.     if (pApp->Init())
  29.         {
  30.         while (GetMessage(&msg, NULL, 0,0 ))
  31.             {
  32.             TranslateMessage(&msg);
  33.             DispatchMessage(&msg);
  34.             }
  35.         }
  36.     delete pApp;
  37.     return msg.wParam;
  38.     }
  39. /*
  40.  * ReuseWndProc
  41.  *
  42.  * Purpose:
  43.  *  Standard window class procedure.
  44.  */
  45. LRESULT APIENTRY ReuseWndProc(HWND hWnd, UINT iMsg, WPARAM wParam
  46.     , LPARAM lParam)
  47.     {
  48.     PAPP        pApp;
  49.     BOOL        fRes;
  50.     IAnimal    *pIAnimal;
  51.     IKoala     *pIKoala;
  52.     COMMANDPARAMS(wID, wCode, hWndMsg);
  53.     pApp=(PAPP)GetWindowLong(hWnd, REUSEWL_STRUCTURE);
  54.     switch (iMsg)
  55.         {
  56.         case WM_NCCREATE:
  57.             pApp=(PAPP)(((LPCREATESTRUCT)lParam)->lpCreateParams);
  58.             SetWindowLong(hWnd, REUSEWL_STRUCTURE, (LONG)pApp);
  59.             return (DefWindowProc(hWnd, iMsg, wParam, lParam));
  60.         case WM_DESTROY:
  61.             PostQuitMessage(0);
  62.             break;
  63.         case WM_COMMAND:
  64.             switch (wID)
  65.                 {
  66.                 case IDM_CREATECONTAINMENT:
  67.                     if (NULL!=pApp->m_pIUnknown)
  68.                         pApp->m_pIUnknown->Release();
  69.                     fRes=CreateKoalaContainment(&pApp->m_pIUnknown);
  70.                     pApp->Message(fRes ? TEXT("KoalaC created")
  71.                         : TEXT("KoalaC creation failed"));
  72.                     break;
  73.                 case IDM_CREATEAGGREGATION:
  74.                     if (NULL!=pApp->m_pIUnknown)
  75.                         pApp->m_pIUnknown->Release();
  76.                     fRes=CreateKoalaAggregation(&pApp->m_pIUnknown);
  77.                     pApp->Message(fRes ? TEXT("KoalaA created")
  78.                         : TEXT("KoalaA creation failed"));
  79.                     break;
  80.                 case IDM_ANIMALEAT:
  81.                     if (!pApp->GetInterface(IID_IAnimal, (void **)&pIAnimal))
  82.                         break;
  83.                     pIAnimal->Eat();
  84.                     pApp->Message(TEXT("IAnimal::Eat called"));
  85.                     pIAnimal->Release();
  86.                     break;
  87.                 case IDM_ANIMALSLEEP:
  88.                     if (!pApp->GetInterface(IID_IAnimal, (void **)&pIAnimal))
  89.                         break;
  90.                     pIAnimal->Sleep();
  91.                     pApp->Message(TEXT("IAnimal::Sleep called"));
  92.                     pIAnimal->Release();
  93.                     break;
  94.                 case IDM_ANIMALPROCREATE:
  95.                     if (!pApp->GetInterface(IID_IAnimal, (void **)&pIAnimal))
  96.                         break;
  97.                     pIAnimal->Procreate();
  98.                     pApp->Message(TEXT("IAnimal::Procreate called"));
  99.                     pIAnimal->Release();
  100.                     break;
  101.                 case IDM_KOALACLIMBEUCALYPTUSTREES:
  102.                     if (!pApp->GetInterface(IID_IKoala, (void **)&pIKoala))
  103.                         break;
  104.                     pIKoala->ClimbEucalyptusTrees();
  105.                     pApp->Message(TEXT("IKoala::ClimbEucalyptusTrees called"));
  106.                     pIKoala->Release();
  107.                     break;
  108.                 case IDM_KOALAPOUCHOPENSDOWN:
  109.                     if (!pApp->GetInterface(IID_IKoala, (void **)&pIKoala))
  110.                         break;
  111.                     pIKoala->PouchOpensDown();
  112.                     pApp->Message(TEXT("IKoala::PouchOpensDown called"));
  113.                     pIKoala->Release();
  114.                     break;
  115.                 case IDM_KOALASLEEPFORHOURSAFTEREATING:
  116.                     if (!pApp->GetInterface(IID_IKoala, (void **)&pIKoala))
  117.                         break;
  118.                     pIKoala->SleepForHoursAfterEating();
  119.                     pApp->Message(TEXT("IKoala::SleepForHoursAfterEating called"));
  120.                     pIKoala->Release();
  121.                     break;
  122.                 case IDM_RELEASE:
  123.                     if (NULL==pApp->m_pIUnknown)
  124.                         {
  125.                         pApp->Message(TEXT("There is no object"));
  126.                         break;
  127.                         }
  128.                     if (0==pApp->m_pIUnknown->Release())
  129.                         {
  130.                         pApp->m_pIUnknown=NULL;
  131.                         pApp->Message(TEXT("Object released"));
  132.                         }
  133.                     break;
  134.                 case IDM_EXIT:
  135.                     PostMessage(hWnd, WM_CLOSE, 0, 0L);
  136.                     break;
  137.                 }
  138.             break;
  139.         default:
  140.             return (DefWindowProc(hWnd, iMsg, wParam, lParam));
  141.         }
  142.     return 0L;
  143.     }
  144. /*
  145.  * CApp::CApp
  146.  * CApp::~CApp
  147.  *
  148.  * Constructor Parameters:
  149.  *  hInst           HINSTANCE of the Application from WinMain
  150.  *  hInstPrev       HINSTANCE of a previous instance from WinMain
  151.  *  nCmdShow        UINT specifying how to show the app window,
  152.  *                  from WinMain.
  153.  */
  154. CApp::CApp(HINSTANCE hInst, HINSTANCE hInstPrev
  155.     , UINT nCmdShow)
  156.     {
  157.     //Initialize WinMain parameter holders.
  158.     m_hInst     =hInst;
  159.     m_hInstPrev =hInstPrev;
  160.     m_nCmdShow  =nCmdShow;
  161.     m_hWnd=NULL;
  162.     m_pIUnknown=NULL;
  163.     return;
  164.     }
  165. CApp::~CApp(void)
  166.     {
  167.     //Release the object if we still have it.
  168.     ReleaseInterface(m_pIUnknown);
  169.     return;
  170.     }
  171. /*
  172.  * CApp::Init
  173.  *
  174.  * Purpose:
  175.  *  Initializes an CApp object by registering window classes,
  176.  *  creating the main window, and doing anything else prone to
  177.  *  failure.  If this function fails the caller should guarantee
  178.  *  that the destructor is called.
  179.  *
  180.  * Parameters:
  181.  *  None
  182.  *
  183.  * Return Value:
  184.  *  BOOL            TRUE if successful, FALSE otherwise.
  185.  */
  186. BOOL CApp::Init(void)
  187.     {
  188.     WNDCLASS    wc;
  189.     if (!m_hInstPrev)
  190.         {
  191.         wc.style          = CS_HREDRAW | CS_VREDRAW;
  192.         wc.lpfnWndProc    = ReuseWndProc;
  193.         wc.cbClsExtra     = 0;
  194.         wc.cbWndExtra     = CBWNDEXTRA;
  195.         wc.hInstance      = m_hInst;
  196.         wc.hIcon          = LoadIcon(m_hInst, TEXT("Icon"));
  197.         wc.hCursor        = LoadCursor(NULL, IDC_ARROW);
  198.         wc.hbrBackground  = (HBRUSH)(COLOR_WINDOW + 1);
  199.         wc.lpszMenuName   = MAKEINTRESOURCE(IDR_MENU);
  200.         wc.lpszClassName  = TEXT("REUSE");
  201.         if (!RegisterClass(&wc))
  202.             return FALSE;
  203.         }
  204.     m_hWnd=CreateWindow(TEXT("REUSE"), TEXT("COM Reusability Demo")
  205.         , WS_MINIMIZEBOX | WS_OVERLAPPEDWINDOW
  206.         ,35, 35, 350, 250, NULL, NULL, m_hInst, this);
  207.     if (NULL==m_hWnd)
  208.         return FALSE;
  209.     ShowWindow(m_hWnd, m_nCmdShow);
  210.     UpdateWindow(m_hWnd);
  211.     return TRUE;
  212.     }
  213. /*
  214.  * GetInterface
  215.  *
  216.  * Purpose:
  217.  *  Centralized function to query for an interface and
  218.  *  return the pointer.
  219.  *
  220.  * Parameters:
  221.  *  riid            REFIID of the interface to query for.
  222.  *  ppv             void ** in which to return the pointer.
  223.  *
  224.  * Return Value:
  225.  *  BOOL            TRUE if the interface was retrieved, FALSE
  226.  *                  otherwise.
  227.  */
  228. BOOL CApp::GetInterface(REFIID riid, void **ppv)
  229.     {
  230.     HRESULT     hr;
  231.     *ppv=NULL;
  232.     if (NULL==m_pIUnknown)
  233.         {
  234.         Message(TEXT("There is no object"));
  235.         return FALSE;
  236.         }
  237.     hr=m_pIUnknown->QueryInterface(riid, ppv);
  238.     if (FAILED(hr))
  239.         {
  240.         Message(TEXT("Failed to get the interface"));
  241.         return FALSE;
  242.         }
  243.     return TRUE;
  244.     }
  245. /*
  246.  * CApp::Message
  247.  *
  248.  * Purpose:
  249.  *  Displays a message in the client area of the window.  This is
  250.  *  just to centralize the call to simpify other code.
  251.  *
  252.  * Parameters:
  253.  *  psz             LPTSTR to the string to display.
  254.  *
  255.  * Return Value:
  256.  *  None
  257.  */
  258. void inline CApp::Message(LPTSTR psz)
  259.     {
  260.     HDC     hDC;
  261.     RECT    rc;
  262.     hDC=GetDC(m_hWnd);
  263.     GetClientRect(m_hWnd, &rc);
  264.     SetBkColor(hDC, GetSysColor(COLOR_WINDOW));
  265.     SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));
  266.     /*
  267.      * We'll just be sloppy and clear the whole window as
  268.      * well as write the string with one ExtTextOut call.
  269.      * No word wrapping here...
  270.      */
  271.     ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rc, psz, lstrlen(psz), NULL);
  272.     ReleaseDC(m_hWnd, hDC);
  273.     return;
  274.     }