subclass.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 3k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. //////////////////////////////////////////////////////////////////////////////
  2. /*  File: subclass.cpp
  3.     Description: Helps with subclassing a window.  See header comments in 
  4.         subclass.h for details.
  5.     Revision History:
  6.     Date        Description                                          Programmer
  7.     --------    ---------------------------------------------------  ----------
  8.     12/16/97    Initial creation.                                    BrianAu
  9. */
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "pch.h"
  12. #pragma hdrstop
  13. #include "subclass.h"
  14. #include "msgbox.h"
  15. //
  16. // Window property name for storing the "this" ptr per window instance.
  17. //
  18. const TCHAR WindowSubclass::m_szPropThis[] = TEXT("WSCPROP_THISPTR");
  19. WindowSubclass::WindowSubclass(
  20.     void
  21.     ) : m_hwnd(NULL),
  22.         m_lpfnWndProc(NULL)
  23. {
  24. }
  25. WindowSubclass::~WindowSubclass(
  26.     void
  27.     )
  28. {
  29.     Cancel();
  30. }
  31. bool
  32. WindowSubclass::Initialize(
  33.     HWND hwnd
  34.     )
  35. {
  36.     bool bResult = false;
  37.     m_hwnd = hwnd;
  38.     if (m_hwnd)
  39.         bResult = Resume();
  40.     return bResult;
  41. }
  42. bool
  43. WindowSubclass::Resume(
  44.     void
  45.     )
  46. {
  47.     bool bResult = false;
  48.     if (NULL != m_hwnd)
  49.     {
  50.         if (SetProp(m_hwnd, m_szPropThis, (HANDLE)this))
  51.         {
  52.             SetLastError(0);
  53.             m_lpfnWndProc = (WNDPROC)SetWindowLongPtr(m_hwnd, GWLP_WNDPROC, (INT_PTR)WndProc);
  54.             bResult = (ERROR_SUCCESS == GetLastError());
  55.         }
  56.     }
  57.     return bResult;
  58. }
  59. void
  60. WindowSubclass::Cancel(
  61.     void
  62.     )
  63. {
  64.     if (NULL != m_hwnd)
  65.     {
  66.         RemoveProp(m_hwnd, m_szPropThis);
  67.         if (NULL != m_lpfnWndProc)
  68.         {
  69.             SetWindowLongPtr(m_hwnd, GWLP_WNDPROC, (INT_PTR)m_lpfnWndProc);
  70.             m_lpfnWndProc = NULL;
  71.         }
  72.         m_hwnd = NULL;
  73.     }
  74. }        
  75. LRESULT CALLBACK
  76. WindowSubclass::WndProc(
  77.     HWND hwnd, 
  78.     UINT message,
  79.     WPARAM wParam,
  80.     LPARAM lParam
  81.     )
  82. {
  83.     WindowSubclass *pThis = (WindowSubclass *)GetProp(hwnd, m_szPropThis);
  84.    
  85.     try
  86.     {
  87.         pThis->HandleMessages(hwnd, message, wParam, lParam);
  88.     }
  89.     catch(CException& e)
  90.     {
  91.         DBGERROR((TEXT("C++ exception %d caught in WindowSubclass::WndProc"), e.dwError));
  92.         CscWin32Message(NULL, e.dwError, CSCUI::SEV_ERROR);
  93.     }
  94.     catch(...)
  95.     {
  96.         DBGERROR((TEXT("Unknown exception caught in WindowSubclass::WndProc")));
  97.         CscWin32Message(NULL, DISP_E_EXCEPTION, CSCUI::SEV_ERROR);
  98.     }
  99.     return CallWindowProc(pThis->m_lpfnWndProc, hwnd, message, wParam, lParam);
  100. }