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

Windows编程

开发平台:

Visual C++

  1. /*
  2.  * QUERY.CPP
  3.  * Demonstration of QueryInterface and different techniques to
  4.  * implement multiple interfaces.
  5.  *
  6.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  7.  *
  8.  * Kraig Brockschmidt, Microsoft
  9.  * Internet  :  kraigb@microsoft.com
  10.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  11.  */
  12. #define INITGUIDS
  13. #include <windows.h>
  14. #include "query.h"
  15. /*
  16.  * WinMain
  17.  *
  18.  * Purpose:
  19.  *  Main entry point of application.
  20.  */
  21. int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hInstPrev
  22.     , LPSTR pszCmdLine, int nCmdShow)
  23.     {
  24.     MSG     msg;
  25.     PAPP    pApp;
  26.     pApp=new CApp(hInst, hInstPrev, nCmdShow);
  27.     if (NULL==pApp)
  28.         return -1;
  29.     if (pApp->Init())
  30.         {
  31.         while (GetMessage(&msg, NULL, 0,0 ))
  32.             {
  33.             TranslateMessage(&msg);
  34.             DispatchMessage(&msg);
  35.             }
  36.         }
  37.     delete pApp;
  38.     return msg.wParam;
  39.     }
  40. /*
  41.  * QueryWndProc
  42.  *
  43.  * Purpose:
  44.  *  Standard window class procedure.
  45.  */
  46. LRESULT APIENTRY QueryWndProc(HWND hWnd, UINT iMsg, WPARAM wParam
  47.     , LPARAM lParam)
  48.     {
  49.     PAPP        pApp;
  50.     HRESULT     hr;
  51.     BOOL        fRes;
  52.     ISampleOne *pISampleOne;
  53.     ISampleTwo *pISampleTwo;
  54.     const UINT  CCHTEMP=80;
  55.     TCHAR       szTemp[CCHTEMP];
  56.     COMMANDPARAMS(wID, wCode, hWndMsg);
  57.     pApp=(PAPP)GetWindowLong(hWnd, QUERYWL_STRUCTURE);
  58.     switch (iMsg)
  59.         {
  60.         case WM_NCCREATE:
  61.             pApp=(PAPP)(((LPCREATESTRUCT)lParam)->lpCreateParams);
  62.             SetWindowLong(hWnd, QUERYWL_STRUCTURE, (LONG)pApp);
  63.             return (DefWindowProc(hWnd, iMsg, wParam, lParam));
  64.         case WM_DESTROY:
  65.             PostQuitMessage(0);
  66.             break;
  67.         case WM_COMMAND:
  68.             switch (wID)
  69.                 {
  70.                 case IDM_OBJECTCREATE1:
  71.                     if (NULL!=pApp->m_pIUnknown)
  72.                         pApp->m_pIUnknown->Release();
  73.                     fRes=CreateObject1(&pApp->m_pIUnknown);
  74.                     pApp->Message(fRes ? TEXT("Object 1 created")
  75.                         : TEXT("Object 1 creation failed"));
  76.                     break;
  77.                 case IDM_OBJECTCREATE2:
  78.                     if (NULL!=pApp->m_pIUnknown)
  79.                         pApp->m_pIUnknown->Release();
  80.                     fRes=CreateObject2(&pApp->m_pIUnknown);
  81.                     pApp->Message(fRes ? TEXT("Object 2 created")
  82.                         : TEXT("Object 2 creation failed"));
  83.                     break;
  84.                 case IDM_OBJECTCREATE3:
  85.                     if (NULL!=pApp->m_pIUnknown)
  86.                         pApp->m_pIUnknown->Release();
  87.                     fRes=CreateObject3(&pApp->m_pIUnknown);
  88.                     pApp->Message(fRes ? TEXT("Object 3 created")
  89.                         : TEXT("Object 3 creation failed"));
  90.                     break;
  91.                 case IDM_OBJECTRELEASE:
  92.                     if (NULL==pApp->m_pIUnknown)
  93.                         {
  94.                         pApp->Message(TEXT("There is no object"));
  95.                         break;
  96.                         }
  97.                     if (0==pApp->m_pIUnknown->Release())
  98.                         {
  99.                         pApp->m_pIUnknown=NULL;
  100.                         pApp->Message(TEXT("Object released"));
  101.                         }
  102.                     break;
  103.                 case IDM_OBJECTGETMESSAGE:
  104.                     if (NULL==pApp->m_pIUnknown)
  105.                         {
  106.                         pApp->Message(TEXT("There is no object"));
  107.                         break;
  108.                         }
  109.                     hr=pApp->m_pIUnknown->QueryInterface
  110.                         (IID_ISampleOne, (PPVOID)&pISampleOne);
  111.                     if (FAILED(hr))
  112.                         {
  113.                         pApp->Message(TEXT("Failed to get ISampleOne"));
  114.                         break;
  115.                         }
  116.                     hr=pISampleOne->GetMessage(szTemp, CCHTEMP);
  117.                     pApp->Message(SUCCEEDED(hr) ? szTemp
  118.                         : TEXT("ISampleOne::GetMessage failed"));
  119.                     //m_pIUnknown is still valid after this Release
  120.                     pISampleOne->Release();
  121.                     break;
  122.                 case IDM_OBJECTGETSTRING:
  123.                     if (NULL==pApp->m_pIUnknown)
  124.                         {
  125.                         pApp->Message(TEXT("There is no object"));
  126.                         break;
  127.                         }
  128.                     hr=pApp->m_pIUnknown->QueryInterface
  129.                         (IID_ISampleTwo, (PPVOID)&pISampleTwo);
  130.                     if (FAILED(hr))
  131.                         {
  132.                         pApp->Message(TEXT("Failed to get ISampleTwo"));
  133.                         break;
  134.                         }
  135.                     hr=pISampleTwo->GetString(szTemp, CCHTEMP);
  136.                     pApp->Message(SUCCEEDED(hr) ? szTemp
  137.                         : TEXT("ISampleTwp::GetString failed"));
  138.                     //m_pIUnknown is still valid after this Release
  139.                     pISampleTwo->Release();
  140.                     break;
  141.                 case IDM_OBJECTEXIT:
  142.                     PostMessage(hWnd, WM_CLOSE, 0, 0L);
  143.                     break;
  144.                 }
  145.             break;
  146.         default:
  147.             return (DefWindowProc(hWnd, iMsg, wParam, lParam));
  148.         }
  149.     return 0L;
  150.     }
  151. /*
  152.  * CApp::CApp
  153.  * CApp::~CApp
  154.  *
  155.  * Constructor Parameters:
  156.  *  hInst           HINSTANCE of the Application from WinMain
  157.  *  hInstPrev       HINSTANCE of a previous instance from WinMain
  158.  *  nCmdShow        UINT specifying how to show the app window,
  159.  *                  from WinMain.
  160.  */
  161. CApp::CApp(HINSTANCE hInst, HINSTANCE hInstPrev
  162.     , UINT nCmdShow)
  163.     {
  164.     //Initialize WinMain parameter holders.
  165.     m_hInst     =hInst;
  166.     m_hInstPrev =hInstPrev;
  167.     m_nCmdShow  =nCmdShow;
  168.     m_hWnd=NULL;
  169.     m_pIUnknown=NULL;
  170.     return;
  171.     }
  172. CApp::~CApp(void)
  173.     {
  174.     //Release the object if we still have it.
  175.     ReleaseInterface(m_pIUnknown);
  176.     return;
  177.     }
  178. /*
  179.  * CApp::Init
  180.  *
  181.  * Purpose:
  182.  *  Initializes an CApp object by registering window classes,
  183.  *  creating the main window, and doing anything else prone to
  184.  *  failure.  If this function fails the caller should guarantee
  185.  *  that the destructor is called.
  186.  *
  187.  * Parameters:
  188.  *  None
  189.  *
  190.  * Return Value:
  191.  *  BOOL            TRUE if successful, FALSE otherwise.
  192.  */
  193. BOOL CApp::Init(void)
  194.     {
  195.     WNDCLASS    wc;
  196.     if (!m_hInstPrev)
  197.         {
  198.         wc.style          = CS_HREDRAW | CS_VREDRAW;
  199.         wc.lpfnWndProc    = QueryWndProc;
  200.         wc.cbClsExtra     = 0;
  201.         wc.cbWndExtra     = CBWNDEXTRA;
  202.         wc.hInstance      = m_hInst;
  203.         wc.hIcon          = LoadIcon(m_hInst, TEXT("Icon"));
  204.         wc.hCursor        = LoadCursor(NULL, IDC_ARROW);
  205.         wc.hbrBackground  = (HBRUSH)(COLOR_WINDOW + 1);
  206.         wc.lpszMenuName   = MAKEINTRESOURCE(IDR_MENU);
  207.         wc.lpszClassName  = TEXT("QUERY");
  208.         if (!RegisterClass(&wc))
  209.             return FALSE;
  210.         }
  211.     m_hWnd=CreateWindow(TEXT("QUERY"), TEXT("QueryInterface Demo")
  212.         , WS_MINIMIZEBOX | WS_OVERLAPPEDWINDOW
  213.         ,35, 35, 350, 250, NULL, NULL, m_hInst, this);
  214.     if (NULL==m_hWnd)
  215.         return FALSE;
  216.     ShowWindow(m_hWnd, m_nCmdShow);
  217.     UpdateWindow(m_hWnd);
  218.     return TRUE;
  219.     }
  220. /*
  221.  * CApp::Message
  222.  *
  223.  * Purpose:
  224.  *  Displays a message in the client area of the window.  This is
  225.  *  just to centralize the call to simpify other code.
  226.  *
  227.  * Parameters:
  228.  *  psz             LPTSTR to the string to display.
  229.  *
  230.  * Return Value:
  231.  *  None
  232.  */
  233. void inline CApp::Message(LPTSTR psz)
  234.     {
  235.     HDC     hDC;
  236.     RECT    rc;
  237.     hDC=GetDC(m_hWnd);
  238.     GetClientRect(m_hWnd, &rc);
  239.     SetBkColor(hDC, GetSysColor(COLOR_WINDOW));
  240.     SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));
  241.     /*
  242.      * We'll just be sloppy and clear the whole window as
  243.      * well as write the string with one ExtTextOut call.
  244.      * No word wrapping here...
  245.      */
  246.     ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rc, psz, lstrlen(psz), NULL);
  247.     ReleaseDC(m_hWnd, hDC);
  248.     return;
  249.     }