home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SRC / WINCTRL.CP_ / WINCTRL.CP
Encoding:
Text File  |  1993-02-08  |  7.5 KB  |  303 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library. 
  2. // Copyright (C) 1992 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 Microsoft 
  7. // QuickHelp and/or WinHelp documentation provided with the library. 
  8. // See these sources for detailed information regarding the 
  9. // Microsoft Foundation Classes product. 
  10.  
  11.  
  12. #include "stdafx.h"
  13.  
  14. #ifdef AFX_CORE1_SEG
  15. #pragma code_seg(AFX_CORE2_SEG)
  16. #endif
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char BASED_CODE THIS_FILE[] = __FILE__;
  21. #define new DEBUG_NEW
  22. #endif
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CStatic
  26.  
  27. IMPLEMENT_DYNAMIC(CStatic, CWnd)
  28.  
  29. WNDPROC* CStatic::GetSuperWndProcAddr()
  30. {
  31.     static WNDPROC NEAR pfnSuper;
  32.     return &pfnSuper;
  33. }
  34.  
  35. BOOL CStatic::Create(LPCSTR lpszText, DWORD dwStyle,
  36.         const RECT& rect, CWnd* pParentWnd, UINT nID)
  37. {
  38.     return CWnd::Create("STATIC", lpszText, dwStyle, rect, pParentWnd, nID);
  39. }
  40.  
  41. CStatic::~CStatic()
  42. {
  43.     DestroyWindow();
  44. }
  45.  
  46. /////////////////////////////////////////////////////////////////////////////
  47. // CButton
  48.  
  49. IMPLEMENT_DYNAMIC(CButton, CWnd)
  50.  
  51. WNDPROC* CButton::GetSuperWndProcAddr()
  52. {
  53.     static WNDPROC NEAR pfnSuper;
  54.     return &pfnSuper;
  55. }
  56.  
  57. BOOL CButton::Create(LPCSTR lpszCaption, DWORD dwStyle,
  58.         const RECT& rect, CWnd* pParentWnd, UINT nID)
  59. {
  60.     return CWnd::Create("BUTTON", lpszCaption, dwStyle, rect, pParentWnd, nID);
  61. }
  62.  
  63. CButton::~CButton()
  64. {
  65.     DestroyWindow();
  66. }
  67.  
  68. // Helper for radio buttons
  69. int CWnd::GetCheckedRadioButton(int nIDFirstButton, int nIDLastButton)
  70. {
  71.     for (int nID = nIDFirstButton; nID <= nIDLastButton; nID++)
  72.     {
  73.         if (IsDlgButtonChecked(nID))
  74.             return nID; // id that matched
  75.     }
  76.     return 0; // invalid ID
  77. }
  78.  
  79. // Derived class is responsible for implementing all of these handlers
  80. //   for owner/self draw controls
  81. void CButton::DrawItem(LPDRAWITEMSTRUCT)
  82.     ASSERT(FALSE); 
  83. }
  84.  
  85. BOOL CButton::OnChildNotify(UINT message, WPARAM, LPARAM lParam,
  86.     LRESULT* pLResult)
  87. {
  88.     if (message != WM_DRAWITEM)
  89.         return FALSE;
  90.     ASSERT(pLResult == NULL);       // no return value expected
  91.     DrawItem((LPDRAWITEMSTRUCT)lParam);
  92.     return TRUE;
  93. }
  94.  
  95. /////////////////////////////////////////////////////////////////////////////
  96. // CListBox
  97.  
  98. IMPLEMENT_DYNAMIC(CListBox, CWnd)
  99.  
  100. WNDPROC* CListBox::GetSuperWndProcAddr()
  101. {
  102.     static WNDPROC NEAR pfnSuper;
  103.     return &pfnSuper;
  104. }
  105.  
  106. BOOL CListBox::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd,
  107.         UINT nID)
  108. {
  109.     return CWnd::Create("LISTBOX", NULL, dwStyle, rect, pParentWnd, nID);
  110. }
  111.  
  112. CListBox::~CListBox()
  113. {
  114.     DestroyWindow();
  115. }
  116.  
  117. // Derived class is responsible for implementing these handlers
  118. //   for owner/self draw controls (except for the optional DeleteItem)
  119. void CListBox::DrawItem(LPDRAWITEMSTRUCT)
  120.     { ASSERT(FALSE); }
  121. void CListBox::MeasureItem(LPMEASUREITEMSTRUCT)
  122.     { ASSERT(FALSE); }
  123. int CListBox::CompareItem(LPCOMPAREITEMSTRUCT)
  124.     { ASSERT(FALSE); return 0; }
  125. void CListBox::DeleteItem(LPDELETEITEMSTRUCT)
  126.     { /* default to nothing */ }
  127.  
  128. BOOL CListBox::OnChildNotify(UINT message, WPARAM, LPARAM lParam,
  129.     LRESULT* pLResult)
  130. {
  131.     switch (message)
  132.     {
  133.     case WM_DRAWITEM:
  134.         ASSERT(pLResult == NULL);       // no return value expected
  135.         DrawItem((LPDRAWITEMSTRUCT)lParam);
  136.         break;
  137.     case WM_MEASUREITEM:
  138.         ASSERT(pLResult == NULL);       // no return value expected
  139.         MeasureItem((LPMEASUREITEMSTRUCT)lParam);
  140.         break;
  141.     case WM_COMPAREITEM:
  142.         ASSERT(pLResult != NULL);       // return value expected
  143.         *pLResult = CompareItem((LPCOMPAREITEMSTRUCT)lParam);
  144.         break;
  145.     case WM_DELETEITEM:
  146.         ASSERT(pLResult == NULL);       // no return value expected
  147.         DeleteItem((LPDELETEITEMSTRUCT)lParam);
  148.         break;
  149.     default:
  150.         return FALSE;   // not for us
  151.     }
  152.     return TRUE;
  153. }
  154.  
  155.  
  156. /////////////////////////////////////////////////////////////////////////////
  157. // CComboBox
  158.  
  159. IMPLEMENT_DYNAMIC(CComboBox, CWnd)
  160.  
  161. WNDPROC* CComboBox::GetSuperWndProcAddr()
  162. {
  163.     static WNDPROC NEAR pfnSuper;
  164.     return &pfnSuper;
  165. }
  166.  
  167. BOOL CComboBox::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd,
  168.         UINT nID)
  169. {
  170.     return CWnd::Create("COMBOBOX", NULL, dwStyle, rect, pParentWnd, nID);
  171. }
  172.  
  173. CComboBox::~CComboBox()
  174. {
  175.     DestroyWindow();
  176. }
  177.  
  178. // Derived class is responsible for implementing these handlers
  179. //   for owner/self draw controls (except for the optional DeleteItem)
  180. void CComboBox::DrawItem(LPDRAWITEMSTRUCT)
  181.     { ASSERT(FALSE); }
  182. void CComboBox::MeasureItem(LPMEASUREITEMSTRUCT)
  183.     { ASSERT(FALSE); }
  184. int CComboBox::CompareItem(LPCOMPAREITEMSTRUCT)
  185.     { ASSERT(FALSE); return 0; }
  186. void CComboBox::DeleteItem(LPDELETEITEMSTRUCT)
  187.     { /* default to nothing */ }
  188.  
  189. BOOL CComboBox::OnChildNotify(UINT message, WPARAM, LPARAM lParam,
  190.     LRESULT* pLResult)
  191. {
  192.     switch (message)
  193.     {
  194.     case WM_DRAWITEM:
  195.         ASSERT(pLResult == NULL);       // no return value expected
  196.         DrawItem((LPDRAWITEMSTRUCT)lParam);
  197.         break;
  198.     case WM_MEASUREITEM:
  199.         ASSERT(pLResult == NULL);       // no return value expected
  200.         MeasureItem((LPMEASUREITEMSTRUCT)lParam);
  201.         break;
  202.     case WM_COMPAREITEM:
  203.         ASSERT(pLResult != NULL);       // return value expected
  204.         *pLResult = CompareItem((LPCOMPAREITEMSTRUCT)lParam);
  205.         break;
  206.     case WM_DELETEITEM:
  207.         ASSERT(pLResult == NULL);       // no return value expected
  208.         DeleteItem((LPDELETEITEMSTRUCT)lParam);
  209.         break;
  210.     default:
  211.         return FALSE;   // not for us
  212.     }
  213.     return TRUE;
  214. }
  215.  
  216. /////////////////////////////////////////////////////////////////////////////
  217. // CEdit
  218.  
  219. IMPLEMENT_DYNAMIC(CEdit, CWnd)
  220.  
  221. WNDPROC* CEdit::GetSuperWndProcAddr()
  222. {
  223.     static WNDPROC NEAR pfnSuper;
  224.     return &pfnSuper;
  225. }
  226.  
  227. BOOL CEdit::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)
  228. {
  229.     return CWnd::Create("EDIT", NULL, dwStyle, rect, pParentWnd, nID);
  230. }
  231.  
  232. CEdit::~CEdit()
  233. {
  234.     DestroyWindow();
  235. }
  236.  
  237. /////////////////////////////////////////////////////////////////////////////
  238. // CScrollBar
  239.  
  240. IMPLEMENT_DYNAMIC(CScrollBar, CWnd)
  241.  
  242. WNDPROC* CScrollBar::GetSuperWndProcAddr()
  243. {
  244.     static WNDPROC NEAR pfnSuper;
  245.     return &pfnSuper;
  246. }
  247.  
  248. BOOL CScrollBar::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd,
  249.         UINT nID)
  250. {
  251.     return CWnd::Create("SCROLLBAR", NULL, dwStyle, rect, pParentWnd, nID);
  252. }
  253.  
  254. CScrollBar::~CScrollBar()
  255. {
  256.     DestroyWindow();
  257. }
  258.  
  259. /////////////////////////////////////////////////////////////////////////////
  260. // Extra CWnd support for dynamic subclassing of controls
  261.  
  262. BOOL CWnd::SubclassWindow(HWND hWnd)
  263. {
  264.     if (!Attach(hWnd))
  265.         return NULL;
  266.  
  267.     // now hook into the AFX WndProc
  268.     WNDPROC* lplpfn = GetSuperWndProcAddr();
  269.     WNDPROC oldWndProc = (WNDPROC)::SetWindowLong(hWnd, GWL_WNDPROC,
  270.         (DWORD)(WNDPROC)AfxWndProc);
  271.     ASSERT(oldWndProc != (WNDPROC)AfxWndProc);
  272.     
  273.     if (*lplpfn == NULL)
  274.         *lplpfn = oldWndProc;   // the first control of that type created
  275. #ifdef _DEBUG
  276.     else if (*lplpfn != oldWndProc)
  277.     {
  278.         TRACE0("Error: Trying to use SubclassWindow with incorrect CWnd "
  279.             "derived class\n");
  280.         TRACE3("\thWnd = $%04X (nIDC=$%04X) is not a %Fs\n",
  281.             (UINT)hWnd, _AfxGetDlgCtrlID(hWnd),
  282.             GetRuntimeClass()->m_lpszClassName);
  283.         ASSERT(FALSE);
  284.         // undo the subclassing if continuing after assert
  285.         ::SetWindowLong(hWnd, GWL_WNDPROC, (DWORD)oldWndProc);
  286.     }
  287. #endif
  288.  
  289.     return TRUE;
  290. }
  291.  
  292. BOOL CWnd::SubclassDlgItem(UINT nID, CWnd* pParent)
  293. {
  294.     ASSERT(pParent != NULL);
  295.     ASSERT(pParent->m_hWnd != NULL);
  296.  
  297.     HWND hWndControl = ::GetDlgItem(pParent->m_hWnd, nID);
  298.     if (hWndControl == NULL)
  299.         return FALSE;
  300.     return SubclassWindow(hWndControl);
  301. }
  302.