home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / tutsamp / perclien / listwin.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-05  |  9.9 KB  |  345 lines

  1. /*+==========================================================================
  2.   File:      LISTWIN.CPP
  3.  
  4.   Summary:   Implementation of the C++ object that encapsulates the
  5.              list box control window that displays the page list.
  6.  
  7.   Classes:   CListWin.
  8.  
  9.   Functions: none.
  10.  
  11.   Origin:    5-24-97: atrent - Editor Inheritance from CMsgLog in
  12.              APPUTIL.CPP.
  13.  
  14. ----------------------------------------------------------------------------
  15.   This file is part of the Microsoft COM Tutorial Code Samples.
  16.  
  17.   Copyright (C) Microsoft Corporation, 1997.  All rights reserved.
  18.  
  19.   This source code is intended only as a supplement to Microsoft
  20.   Development Tools and/or on-line documentation.  See these other
  21.   materials for detailed information regarding Microsoft code samples.
  22.  
  23.   THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  24.   KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  25.   IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  26.   PARTICULAR PURPOSE.
  27. ==========================================================================+*/
  28.  
  29. /*---------------------------------------------------------------------------
  30.   We include WINDOWS.H for all Win32 applications.
  31.   We include TCHAR.H for general Unicode/Ansi prototype of utility
  32.     functions like wvsprintf.
  33.   We include APPUTIL.H because we will be building this application using
  34.     the convenient Virtual Window and Dialog classes and other
  35.     utility functions in the APPUTIL Library (ie, APPUTIL.LIB).
  36.   We include LISTWIN.H for the defines and the CListWin class declaration
  37.     needed in LISTWIN.
  38. ---------------------------------------------------------------------------*/
  39. #include <windows.h>
  40. #include <tchar.h>
  41. #include <apputil.h>
  42. #include "listwin.h"
  43.  
  44.  
  45. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  46.   Method:   CListWin::Create
  47.  
  48.   Summary:  Creates the ListBox as a child window to fill the client area
  49.             of the specified parent window or to exist as a separate
  50.             window owned by the parent window.
  51.  
  52.   Args:     HINSTANCE hInst,
  53.               Instance handle of the application.
  54.             HWND hWndparent,
  55.               Window handle for the parent window of the listbox.
  56.             BOOL bSeparate)
  57.               Flag to create the listbox as a separate window.  FALSE
  58.               means fit the child window to fill the client area of the
  59.               parent window. TRUE means the window is a separate (but
  60.               owned) window.
  61.  
  62.   Returns:  BOOL
  63.               TRUE if successful; FALSE if not.
  64. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  65. BOOL CListWin::Create(
  66.        HINSTANCE hInst,
  67.        HWND hWndParent,
  68.        BOOL bSeparate)
  69. {
  70.   BOOL bResult = FALSE;
  71.   HWND hWnd;
  72.   RECT rect;
  73.   DWORD dwStyle = WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | LBS_NOTIFY |
  74.                     LBS_NOINTEGRALHEIGHT;
  75.   TCHAR* pszTitle = bSeparate ? TEXT("Page List") : NULL;
  76.  
  77.   dwStyle |= bSeparate ? WS_OVERLAPPEDWINDOW : WS_CHILD;
  78.  
  79.   if (IsWindow(hWndParent))
  80.   {
  81.     GetClientRect(hWndParent, &rect);
  82.  
  83.     // Create the ListBox window.
  84.     hWnd = ::CreateWindowEx(
  85.                0,                // Extended Window Style
  86.                TEXT("LISTBOX"),  // Class Name
  87.                pszTitle,         // Window Title
  88.                dwStyle,          // The window style
  89.                0,                // (x,y)=Upper left of Parent window
  90.                0,
  91.                rect.right,       // Width; Fill Client Window
  92.                rect.bottom,      // Height
  93.                hWndParent,       // Parent Window Handle
  94.                (HMENU) IDC_LISTWIN, // No menu but rig ID for notifications.
  95.                hInst,            // App Instance Handle
  96.                NULL);            // Window Creation Data
  97.  
  98.     if (NULL != hWnd)
  99.     {
  100.       // Remember the window handle of this listbox window.
  101.       m_hWnd = hWnd;
  102.       // Remember the instance of this application.
  103.       m_hInst = hInst;
  104.       // Remember if this is a separate window (bSeparate==TRUE).
  105.       m_bSeparate = bSeparate;
  106.       // Return success.
  107.       bResult = TRUE;
  108.     }
  109.   }
  110.  
  111.   return (bResult);
  112. }
  113.  
  114.  
  115. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  116.   Method:   CListWin::SetCurSel
  117.  
  118.   Summary:  Sets the current selection to the specified list item. Also
  119.             sets keyboard focus to the ListWin listbox.
  120.  
  121.   Args:     INT iIndex
  122.               Zero-based Listbox Index of the selection to set.
  123.  
  124.   Returns:  BOOL
  125.               TRUE.
  126. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  127. BOOL CListWin::SetCurSel(
  128.        INT iIndex)
  129. {
  130.   BOOL bOk = FALSE;
  131.  
  132.   if (IsWindow(m_hWnd))
  133.   {
  134.     iIndex = ::SendMessage(
  135.                  m_hWnd,
  136.                  LB_SETCURSEL,
  137.                  iIndex,
  138.                  0);
  139.     if (LB_ERR != iIndex && iIndex >= 0)
  140.     {
  141.       ::SetFocus(m_hWnd);
  142.       bOk = TRUE;
  143.     }
  144.   }
  145.  
  146.   return bOk;
  147. }
  148.  
  149.  
  150. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  151.   Method:   CListWin::GetCurSel
  152.  
  153.   Summary:  Gets the index (page number) of the current listbox selection.
  154.  
  155.   Args:     INT* piPage
  156.               Address of output INT Page number variable.
  157.  
  158.   Returns:  BOOL
  159.               TRUE.
  160. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  161. BOOL CListWin::GetCurSel(
  162.        INT* piPage)
  163. {
  164.   BOOL bOk = FALSE;
  165.   INT iIndex;
  166.  
  167.   if (IsWindow(m_hWnd))
  168.   {
  169.     iIndex = ::SendMessage(
  170.                  m_hWnd,
  171.                  LB_GETCURSEL,
  172.                  0,
  173.                  0);
  174.     if (LB_ERR != iIndex && iIndex >= 0)
  175.     {
  176.       *piPage = iIndex;
  177.       bOk = TRUE;
  178.     }
  179.   }
  180.  
  181.   return bOk;
  182. }
  183.  
  184.  
  185. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  186.   Method:   CListWin::AddFmt
  187.  
  188.   Summary:  Add a printf-style formatted message as a separate line in the
  189.             ListWin listbox.
  190.  
  191.   Args:     LPTSTR szFmt
  192.               The format/message string to control the display line.
  193.             [...]
  194.               Arguments to match those specified in the format string.
  195.  
  196.   Returns:  BOOL
  197.               TRUE if successful; FALSE if not.
  198. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  199. BOOL CListWin::AddFmt(
  200.        LPTSTR szFmt,
  201.        ...)
  202. {
  203.   BOOL bResult = FALSE;
  204.   va_list arglist;
  205.   va_start(arglist, szFmt);
  206.   TCHAR szLine[MAX_STRING_LENGTH];
  207.   INT iIndex;
  208.  
  209.   // Use the format string and arguments to format the line text.
  210.   wvsprintf(szLine, szFmt, arglist);
  211.  
  212.   // Send the newly formated message string to the ListWin listbox.
  213.   if (IsWindow(m_hWnd))
  214.   {
  215.     ::SendMessage(
  216.         m_hWnd,
  217.         LB_ADDSTRING,
  218.         0,
  219.         (LPARAM)szLine);
  220.     iIndex = ::SendMessage(
  221.                  m_hWnd,
  222.                  LB_GETCOUNT,
  223.                  0,
  224.                  0);
  225.     if (LB_ERR != iIndex && iIndex > 0)
  226.     {
  227.       --iIndex;
  228.       ::SendMessage(
  229.           m_hWnd,
  230.           LB_SETCURSEL,
  231.           (WPARAM)iIndex,
  232.           0);
  233.     }
  234.     bResult = TRUE;
  235.   }
  236.  
  237.   return (bResult);
  238. }
  239.  
  240.  
  241. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  242.   Method:   CListWin::InsertFmt
  243.  
  244.   Summary:  Insert a printf-style formatted entry as a separate line in
  245.             the ListWin listbox at the given index.
  246.  
  247.   Args:     SHORT nIndex,
  248.               Index in the Listbox at which to perform the insert.
  249.             LPTSTR szFmt
  250.               The format/message string to control the display line.
  251.             [...]
  252.               Arguments to match those specified in the format string.
  253.  
  254.   Returns:  BOOL
  255.               TRUE if successful; FALSE if not.
  256. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  257. BOOL CListWin::InsertFmt(
  258.        INT iIndex,
  259.        LPTSTR szFmt,
  260.        ...)
  261. {
  262.   BOOL bResult = FALSE;
  263.   va_list arglist;
  264.   va_start(arglist, szFmt);
  265.   TCHAR szLine[MAX_STRING_LENGTH];
  266.   INT iNewItem;
  267.  
  268.   // Use the format string and arguments to format the line text.
  269.   wvsprintf(szLine, szFmt, arglist);
  270.  
  271.   // Send the newly formated message string to the ListWin Listbox.
  272.   if (IsWindow(m_hWnd))
  273.   {
  274.     iNewItem = ::SendMessage(
  275.                    m_hWnd,
  276.                    LB_INSERTSTRING,
  277.                    iIndex,
  278.                    (LPARAM)szLine);
  279.     if (LB_ERR != iNewItem && iNewItem > 0)
  280.     {
  281.       ::SendMessage(
  282.           m_hWnd,
  283.           LB_SETCURSEL,
  284.           (WPARAM)iNewItem,
  285.           0);
  286.     }
  287.     bResult = TRUE;
  288.   }
  289.  
  290.   return (bResult);
  291. }
  292.  
  293.  
  294. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  295.   Method:   CListWin::Resize
  296.  
  297.   Summary:  Resizes the listbox to a new width and height.  Called during
  298.             the parent window's WM_SIZE to fit the listbox to the client
  299.             area of the parent window.  It only honors this request if it
  300.             is an integral child window.
  301.  
  302.   Args:     INT nWidth
  303.               New width in pixels of the listbox.
  304.             INT nHeight
  305.               New height in pixels of the listbox.
  306.  
  307.   Returns:  BOOL
  308.               TRUE if successful; FALSE if not.
  309. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  310. BOOL CListWin::Resize(
  311.        INT nWidth,
  312.        INT nHeight)
  313. {
  314.   BOOL bResult = FALSE;
  315.  
  316.   if (!m_bSeparate)
  317.     bResult = ::MoveWindow(m_hWnd, 0, 0, nWidth, nHeight, TRUE);
  318.  
  319.   return (bResult);
  320. }
  321.  
  322.  
  323. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  324.   Method:   CListWin::Clear
  325.  
  326.   Summary:  Clears all displayh lines from the ListWin listbox.
  327.  
  328.   Args:     void
  329.  
  330.   Returns:  BOOL
  331.               TRUE.
  332. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  333. BOOL CListWin::Clear(
  334.        void)
  335. {
  336.   if (IsWindow(m_hWnd))
  337.     ::SendMessage(
  338.         m_hWnd,
  339.         LB_RESETCONTENT,
  340.         0,
  341.         0);
  342.  
  343.   return (TRUE);
  344. }
  345.