home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / winctrl1.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  7KB  |  267 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12. #ifndef _AFX_NO_OCC_SUPPORT
  13. #include "occimpl.h"
  14. #endif
  15.  
  16. #ifdef AFX_CORE4_SEG
  17. #pragma code_seg(AFX_CORE4_SEG)
  18. #endif
  19.  
  20. #ifdef _DEBUG
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. #define new DEBUG_NEW
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CStatic
  29.  
  30. BOOL CStatic::Create(LPCTSTR lpszText, DWORD dwStyle,
  31.         const RECT& rect, CWnd* pParentWnd, UINT nID)
  32. {
  33.     CWnd* pWnd = this;
  34.     return pWnd->Create(_T("STATIC"), lpszText, dwStyle, rect, pParentWnd, nID);
  35. }
  36.  
  37. CStatic::~CStatic()
  38. {
  39.     DestroyWindow();
  40. }
  41.  
  42. /////////////////////////////////////////////////////////////////////////////
  43. // CButton
  44.  
  45. BOOL CButton::Create(LPCTSTR lpszCaption, DWORD dwStyle,
  46.         const RECT& rect, CWnd* pParentWnd, UINT nID)
  47. {
  48.     CWnd* pWnd = this;
  49.     return pWnd->Create(_T("BUTTON"), lpszCaption, dwStyle, rect, pParentWnd, nID);
  50. }
  51.  
  52. CButton::~CButton()
  53. {
  54.     DestroyWindow();
  55. }
  56.  
  57. // Helper for radio buttons
  58. int CWnd::GetCheckedRadioButton(int nIDFirstButton, int nIDLastButton)
  59. {
  60.     for (int nID = nIDFirstButton; nID <= nIDLastButton; nID++)
  61.     {
  62.         if (IsDlgButtonChecked(nID))
  63.             return nID; // id that matched
  64.     }
  65.     return 0; // invalid ID
  66. }
  67.  
  68. // Derived class is responsible for implementing all of these handlers
  69. //   for owner/self draw controls
  70. void CButton::DrawItem(LPDRAWITEMSTRUCT)
  71. {
  72.     ASSERT(FALSE);
  73. }
  74.  
  75. BOOL CButton::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam,
  76.     LRESULT* pResult)
  77. {
  78.     if (message != WM_DRAWITEM)
  79.         return CWnd::OnChildNotify(message, wParam, lParam, pResult);
  80.  
  81.     ASSERT(pResult == NULL);       // no return value expected
  82.     UNUSED(pResult); // unused in release builds
  83.     DrawItem((LPDRAWITEMSTRUCT)lParam);
  84.     return TRUE;
  85. }
  86.  
  87. /////////////////////////////////////////////////////////////////////////////
  88. // CListBox
  89.  
  90. BOOL CListBox::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd,
  91.         UINT nID)
  92. {
  93.     CWnd* pWnd = this;
  94.     return pWnd->Create(_T("LISTBOX"), NULL, dwStyle, rect, pParentWnd, nID);
  95. }
  96.  
  97. CListBox::~CListBox()
  98. {
  99.     DestroyWindow();
  100. }
  101.  
  102. // Derived class is responsible for implementing these handlers
  103. //   for owner/self draw controls (except for the optional DeleteItem)
  104. void CListBox::DrawItem(LPDRAWITEMSTRUCT)
  105.     { ASSERT(FALSE); }
  106. void CListBox::MeasureItem(LPMEASUREITEMSTRUCT)
  107.     { ASSERT(FALSE); }
  108. int CListBox::CompareItem(LPCOMPAREITEMSTRUCT)
  109.     { ASSERT(FALSE); return 0; }
  110. void CListBox::DeleteItem(LPDELETEITEMSTRUCT)
  111.     { /* default to nothing */ }
  112. int CListBox::VKeyToItem(UINT, UINT)
  113.     { return Default(); }
  114. int CListBox::CharToItem(UINT, UINT)
  115.     { return Default(); }
  116.  
  117. BOOL CListBox::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam,
  118.     LRESULT* pResult)
  119. {
  120.     switch (message)
  121.     {
  122.     case WM_DRAWITEM:
  123.         ASSERT(pResult == NULL);       // no return value expected
  124.         DrawItem((LPDRAWITEMSTRUCT)lParam);
  125.         break;
  126.     case WM_MEASUREITEM:
  127.         ASSERT(pResult == NULL);       // no return value expected
  128.         MeasureItem((LPMEASUREITEMSTRUCT)lParam);
  129.         break;
  130.     case WM_COMPAREITEM:
  131.         ASSERT(pResult != NULL);       // return value expected
  132.         *pResult = CompareItem((LPCOMPAREITEMSTRUCT)lParam);
  133.         break;
  134.     case WM_DELETEITEM:
  135.         ASSERT(pResult == NULL);       // no return value expected
  136.         DeleteItem((LPDELETEITEMSTRUCT)lParam);
  137.         break;
  138.     case WM_VKEYTOITEM:
  139.         *pResult = VKeyToItem(LOWORD(wParam), HIWORD(wParam));
  140.         break;
  141.     case WM_CHARTOITEM:
  142.         *pResult = CharToItem(LOWORD(wParam), HIWORD(wParam));
  143.         break;
  144.     default:
  145.         return CWnd::OnChildNotify(message, wParam, lParam, pResult);
  146.     }
  147.     return TRUE;
  148. }
  149.  
  150. void CListBox::GetText(int nIndex, CString& rString) const
  151. {
  152.     ASSERT(::IsWindow(m_hWnd));
  153.     GetText(nIndex, rString.GetBufferSetLength(GetTextLen(nIndex)));
  154.     rString.ReleaseBuffer();
  155. }
  156.  
  157. #if (WINVER >= 0x400)
  158. UINT CListBox::ItemFromPoint(CPoint pt, BOOL& bOutside) const
  159. {
  160.     ASSERT(::IsWindow(m_hWnd));
  161.     DWORD dw = (DWORD)::SendMessage(m_hWnd, LB_ITEMFROMPOINT, 0, MAKELPARAM(pt.x, pt.y));
  162.     bOutside = !!HIWORD(dw);
  163.     return LOWORD(dw);
  164. }
  165. #endif
  166. /////////////////////////////////////////////////////////////////////////////
  167. // CComboBox
  168.  
  169. BOOL CComboBox::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd,
  170.         UINT nID)
  171. {
  172.     CWnd* pWnd = this;
  173.     return pWnd->Create(_T("COMBOBOX"), NULL, dwStyle, rect, pParentWnd, nID);
  174. }
  175.  
  176. CComboBox::~CComboBox()
  177. {
  178.     DestroyWindow();
  179. }
  180.  
  181. // Derived class is responsible for implementing these handlers
  182. //   for owner/self draw controls (except for the optional DeleteItem)
  183. void CComboBox::DrawItem(LPDRAWITEMSTRUCT)
  184.     { ASSERT(FALSE); }
  185. void CComboBox::MeasureItem(LPMEASUREITEMSTRUCT)
  186.     { ASSERT(FALSE); }
  187. int CComboBox::CompareItem(LPCOMPAREITEMSTRUCT)
  188.     { ASSERT(FALSE); return 0; }
  189. void CComboBox::DeleteItem(LPDELETEITEMSTRUCT)
  190.     { /* default to nothing */ }
  191.  
  192. BOOL CComboBox::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam,
  193.     LRESULT* pResult)
  194. {
  195.     switch (message)
  196.     {
  197.     case WM_DRAWITEM:
  198.         ASSERT(pResult == NULL);       // no return value expected
  199.         DrawItem((LPDRAWITEMSTRUCT)lParam);
  200.         break;
  201.     case WM_MEASUREITEM:
  202.         ASSERT(pResult == NULL);       // no return value expected
  203.         MeasureItem((LPMEASUREITEMSTRUCT)lParam);
  204.         break;
  205.     case WM_COMPAREITEM:
  206.         ASSERT(pResult != NULL);       // return value expected
  207.         *pResult = CompareItem((LPCOMPAREITEMSTRUCT)lParam);
  208.         break;
  209.     case WM_DELETEITEM:
  210.         ASSERT(pResult == NULL);       // no return value expected
  211.         DeleteItem((LPDELETEITEMSTRUCT)lParam);
  212.         break;
  213.     default:
  214.         return CWnd::OnChildNotify(message, wParam, lParam, pResult);
  215.     }
  216.     return TRUE;
  217. }
  218.  
  219. void CComboBox::GetLBText(int nIndex, CString& rString) const
  220. {
  221.     ASSERT(::IsWindow(m_hWnd));
  222.     GetLBText(nIndex, rString.GetBufferSetLength(GetLBTextLen(nIndex)));
  223.     rString.ReleaseBuffer();
  224. }
  225.  
  226. /////////////////////////////////////////////////////////////////////////////
  227. // CEdit
  228.  
  229. BOOL CEdit::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)
  230. {
  231.     CWnd* pWnd = this;
  232.     return pWnd->Create(_T("EDIT"), NULL, dwStyle, rect, pParentWnd, nID);
  233. }
  234.  
  235. CEdit::~CEdit()
  236. {
  237.     DestroyWindow();
  238. }
  239.  
  240. /////////////////////////////////////////////////////////////////////////////
  241. // CScrollBar
  242.  
  243. BOOL CScrollBar::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd,
  244.         UINT nID)
  245. {
  246.     CWnd* pWnd = this;
  247.     return pWnd->Create(_T("SCROLLBAR"), NULL, dwStyle, rect, pParentWnd, nID);
  248. }
  249.  
  250. CScrollBar::~CScrollBar()
  251. {
  252.     DestroyWindow();
  253. }
  254.  
  255. #ifdef AFX_INIT_SEG
  256. #pragma code_seg(AFX_INIT_SEG)
  257. #endif
  258.  
  259. IMPLEMENT_DYNAMIC(CStatic, CWnd)
  260. IMPLEMENT_DYNAMIC(CButton, CWnd)
  261. IMPLEMENT_DYNAMIC(CListBox, CWnd)
  262. IMPLEMENT_DYNAMIC(CComboBox, CWnd)
  263. IMPLEMENT_DYNAMIC(CEdit, CWnd)
  264. IMPLEMENT_DYNAMIC(CScrollBar, CWnd)
  265.  
  266. /////////////////////////////////////////////////////////////////////////////
  267.