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 / chap02 / enumrect / enumrect.cpp < prev    next >
C/C++ Source or Header  |  1996-05-10  |  8KB  |  336 lines

  1. /*
  2.  * ENUMRECT.CPP
  3.  * C/C++ Enumerator Demonstartion Chapter 2
  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 "enumrect.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.     pApp=new CApp(hInst, hInstPrev, nCmdShow);
  31.  
  32.     if (NULL==pApp)
  33.         return -1;
  34.  
  35.     if (pApp->Init())
  36.         {
  37.         while (GetMessage(&msg, NULL, 0,0 ))
  38.             {
  39.             TranslateMessage(&msg);
  40.             DispatchMessage(&msg);
  41.             }
  42.         }
  43.  
  44.     delete pApp;
  45.     return msg.wParam;
  46.     }
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. /*
  55.  * EnumWndProc
  56.  *
  57.  * Purpose:
  58.  *  Standard window class procedure.
  59.  */
  60.  
  61. LRESULT APIENTRY EnumWndProc(HWND hWnd, UINT iMsg, WPARAM wParam
  62.     , LPARAM lParam)
  63.     {
  64.     PAPP        pApp;
  65.     RECT        rc;
  66.     DWORD       cRect;
  67.     BOOL        fRes;
  68.     PENUMRECT   pClone;
  69.  
  70.     COMMANDPARAMS(wID, wCode, hWndMsg);
  71.  
  72.     pApp=(PAPP)GetWindowLong(hWnd, ENUMWL_STRUCTURE);
  73.  
  74.     switch (iMsg)
  75.         {
  76.         case WM_NCCREATE:
  77.             pApp=(PAPP)(((LPCREATESTRUCT)lParam)->lpCreateParams);
  78.             SetWindowLong(hWnd, ENUMWL_STRUCTURE, (LONG)pApp);
  79.             return (DefWindowProc(hWnd, iMsg, wParam, lParam));
  80.  
  81.  
  82.         case WM_DESTROY:
  83.             PostQuitMessage(0);
  84.             break;
  85.  
  86.  
  87.         case WM_COMMAND:
  88.             switch (wID)
  89.                 {
  90.                 case IDM_ENUMCREATEC:
  91.                     if (NULL!=pApp->m_pIEnumRect)
  92.                         pApp->m_pIEnumRect->Release();
  93.  
  94.                     fRes=CreateRECTEnumeratorC(&pApp->m_pIEnumRect);
  95.  
  96.                     pApp->Message(fRes ? TEXT("C Object created")
  97.                         : TEXT("C Object creation failed"));
  98.  
  99.                     break;
  100.  
  101.                 case IDM_ENUMCREATECPP:
  102.                     if (NULL!=pApp->m_pIEnumRect)
  103.                         pApp->m_pIEnumRect->Release();
  104.  
  105.                     fRes=CreateRECTEnumeratorCPP(&pApp->m_pIEnumRect);
  106.  
  107.                     pApp->Message(fRes ? TEXT("C++ Object created")
  108.                         : TEXT("C++ Object creation failed"));
  109.  
  110.                     break;
  111.  
  112.  
  113.                 case IDM_ENUMRELEASE:
  114.                     if (NULL==pApp->m_pIEnumRect)
  115.                         {
  116.                         pApp->Message(TEXT("There is no object"));
  117.                         break;
  118.                         }
  119.  
  120.                     if (0==pApp->m_pIEnumRect->Release())
  121.                         {
  122.                         pApp->m_pIEnumRect=NULL;
  123.                         pApp->Message(TEXT("Object released"));
  124.                         }
  125.  
  126.                     break;
  127.  
  128.  
  129.                 case IDM_ENUMRUNTHROUGH:
  130.                     if (NULL==pApp->m_pIEnumRect)
  131.                         {
  132.                         pApp->Message(TEXT("There is no object"));
  133.                         break;
  134.                         }
  135.  
  136.                     while (pApp->m_pIEnumRect->Next(1, &rc, &cRect))
  137.                         ;
  138.  
  139.                     pApp->Message(TEXT("Enumeration complete"));
  140.                     break;
  141.  
  142.  
  143.                 case IDM_ENUMEVERYTHIRD:
  144.                     if (NULL==pApp->m_pIEnumRect)
  145.                         {
  146.                         pApp->Message(TEXT("There is no object"));
  147.                         break;
  148.                         }
  149.  
  150.                     while (NOERROR==pApp->m_pIEnumRect->Next(1
  151.                         , &rc, &cRect))
  152.                         {
  153.                         if (NOERROR!=pApp->m_pIEnumRect->Skip(2))
  154.                             break;
  155.                         }
  156.  
  157.                     pApp->Message(TEXT("Enumeration complete"));
  158.                     break;
  159.  
  160.  
  161.                 case IDM_ENUMRESET:
  162.                     if (NULL==pApp->m_pIEnumRect)
  163.                         {
  164.                         pApp->Message(TEXT("There is no object"));
  165.                         break;
  166.                         }
  167.  
  168.                     pApp->m_pIEnumRect->Reset();
  169.                     pApp->Message(TEXT("Reset complete"));
  170.                     break;
  171.  
  172.  
  173.                 case IDM_ENUMCLONE:
  174.                     if (NULL==pApp->m_pIEnumRect)
  175.                         {
  176.                         pApp->Message(TEXT("There is no object"));
  177.                         break;
  178.                         }
  179.  
  180.                     pApp->m_pIEnumRect->Clone((PENUMRECT *)&pClone);
  181.  
  182.                     if (NULL==pClone)
  183.                         pApp->Message(TEXT("Clone failed"));
  184.                     else
  185.                         {
  186.                         pApp->Message(TEXT("Clone succeeded"));
  187.                         pClone->Release();
  188.                         }
  189.                     break;
  190.  
  191.  
  192.                 case IDM_ENUMEXIT:
  193.                     PostMessage(hWnd, WM_CLOSE, 0, 0L);
  194.                     break;
  195.                 }
  196.             break;
  197.  
  198.         default:
  199.             return (DefWindowProc(hWnd, iMsg, wParam, lParam));
  200.         }
  201.  
  202.     return 0L;
  203.     }
  204.  
  205.  
  206.  
  207.  
  208. /*
  209.  * CApp::CApp
  210.  * CApp::~CApp
  211.  *
  212.  * Constructor Parameters:
  213.  *  hInst           HINSTANCE of the Application from WinMain
  214.  *  hInstPrev       HINSTANCE of a previous instance from WinMain
  215.  *  nCmdShow        UINT specifying how to show the app window,
  216.  *                  from WinMain.
  217.  */
  218.  
  219. CApp::CApp(HINSTANCE hInst, HINSTANCE hInstPrev
  220.     , UINT nCmdShow)
  221.     {
  222.     //Initialize WinMain parameter holders.
  223.     m_hInst     =hInst;
  224.     m_hInstPrev =hInstPrev;
  225.     m_nCmdShow  =nCmdShow;
  226.  
  227.     m_hWnd=NULL;
  228.     m_pIEnumRect=NULL;
  229.  
  230.     return;
  231.     }
  232.  
  233.  
  234. CApp::~CApp(void)
  235.     {
  236.     //Free the enumerator object if we have one.
  237.     if (NULL!=m_pIEnumRect)
  238.         m_pIEnumRect->Release();
  239.  
  240.     return;
  241.     }
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248. /*
  249.  * CApp::Init
  250.  *
  251.  * Purpose:
  252.  *  Initializes an CApp object by registering window classes,
  253.  *  creating the main window, and doing anything else prone to
  254.  *  failure.  If this function fails the caller should guarantee
  255.  *  that the destructor is called.
  256.  *
  257.  * Parameters:
  258.  *  None
  259.  *
  260.  * Return Value:
  261.  *  BOOL            TRUE if successful, FALSE otherwise.
  262.  */
  263.  
  264. BOOL CApp::Init(void)
  265.     {
  266.     WNDCLASS    wc;
  267.  
  268.     if (!m_hInstPrev)
  269.         {
  270.         wc.style          = CS_HREDRAW | CS_VREDRAW;
  271.         wc.lpfnWndProc    = EnumWndProc;
  272.         wc.cbClsExtra     = 0;
  273.         wc.cbWndExtra     = CBWNDEXTRA;
  274.         wc.hInstance      = m_hInst;
  275.         wc.hIcon          = LoadIcon(m_hInst, TEXT("Icon"));
  276.         wc.hCursor        = LoadCursor(NULL, IDC_ARROW);
  277.         wc.hbrBackground  = (HBRUSH)(COLOR_WINDOW + 1);
  278.         wc.lpszMenuName   = MAKEINTRESOURCE(IDR_MENU);
  279.         wc.lpszClassName  = TEXT("ENUMRECT");
  280.  
  281.         if (!RegisterClass(&wc))
  282.             return FALSE;
  283.         }
  284.  
  285.     m_hWnd=CreateWindow(TEXT("ENUMRECT"), TEXT("RECT Enumerator")
  286.         , WS_MINIMIZEBOX | WS_OVERLAPPEDWINDOW
  287.         ,35, 35, 350, 250, NULL, NULL, m_hInst, this);
  288.  
  289.     if (NULL==m_hWnd)
  290.         return FALSE;
  291.  
  292.     ShowWindow(m_hWnd, m_nCmdShow);
  293.     UpdateWindow(m_hWnd);
  294.  
  295.     return TRUE;
  296.     }
  297.  
  298.  
  299.  
  300.  
  301. /*
  302.  * CApp::Message
  303.  *
  304.  * Purpose:
  305.  *  Displays a message in the client area of the window.  This is
  306.  *  just to centralize the call to simpify other code.
  307.  *
  308.  * Parameters:
  309.  *  psz             LPTSTR to the string to display.
  310.  *
  311.  * Return Value:
  312.  *  None
  313.  */
  314.  
  315. void CApp::Message(LPTSTR psz)
  316.     {
  317.     HDC     hDC;
  318.     RECT    rc;
  319.  
  320.     hDC=GetDC(m_hWnd);
  321.     GetClientRect(m_hWnd, &rc);
  322.  
  323.     SetBkColor(hDC, GetSysColor(COLOR_WINDOW));
  324.     SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));
  325.  
  326.     /*
  327.      * We'll just be sloppy and clear the whole window as
  328.      * well as write the string with one ExtTextOut call.
  329.      * No word wrapping here...
  330.      */
  331.  
  332.     ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rc, psz, lstrlen(psz), NULL);
  333.     ReleaseDC(m_hWnd, hDC);
  334.     return;
  335.     }
  336.