home *** CD-ROM | disk | FTP | other *** search
/ PC Administrator / spravce.iso / RunModule / src / SubclassWnd.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-08-31  |  3.2 KB  |  94 lines

  1. // SubclassWnd.cpp: implementation of the CSubclassWnd class.
  2. //
  3. /*
  4. Copyright 2001 Anish Mistry. All rights reserved.
  5.  
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8.  
  9.    1. Redistributions of source code must retain the above copyright notice, 
  10.    this list of conditions and the following disclaimer.
  11.    2. Redistributions in binary form must reproduce the above copyright notice,
  12.    this list of conditions and the following disclaimer in the documentation 
  13.    and/or other materials provided with the distribution.
  14.  
  15. THIS SOFTWARE IS PROVIDED BY ANISH MISTRY ``AS IS'' AND ANY EXPRESS OR IMPLIED 
  16. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
  17. AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS 
  18. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  19. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  20. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  22. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24.  
  25. The views and conclusions contained in the software and documentation are those
  26. of the authors and should not be interpreted as representing official policies,
  27. either expressed or implied, of Anish Mistry or AM Productions.
  28.  
  29. * Variation of the FreeBSD License. http://www.freebsd.org/copyright/freebsd-license.html
  30. */
  31. //////////////////////////////////////////////////////////////////////
  32.  
  33. #include "stdafx.h"
  34. #include "SubclassWnd.h"
  35.  
  36. //////////////////////////////////////////////////////////////////////
  37. // Construction/Destruction
  38. //////////////////////////////////////////////////////////////////////
  39.  
  40. CSubclassWnd::CSubclassWnd()
  41. {
  42.     m_hInstance = GetModuleHandle(NULL);
  43.     m_hWnd = NULL;
  44.     m_wpOldWndProc = NULL;
  45.     // init the ThunkData with the this pointer so that this can be subclassed
  46.     ThunkInit(m_Thunk, this);
  47. }
  48.  
  49. CSubclassWnd::~CSubclassWnd()
  50. {
  51.     if(m_wpOldWndProc || m_hWnd)
  52.         OnDestroy();
  53. }
  54.  
  55. bool CSubclassWnd::Init(HWND hSubclassWnd)
  56. {// begin Init
  57.     // save HWND
  58.     if(!hSubclassWnd)
  59.         return false;
  60.     m_hWnd = hSubclassWnd;
  61.     // subclass the window
  62.     m_wpOldWndProc = (WNDPROC)SetWindowLong(m_hWnd,GWL_WNDPROC,(LONG)(void *)m_Thunk);
  63.     if(!m_wpOldWndProc)
  64.         return false;
  65.     return true;
  66. }// end Init
  67.  
  68. LRESULT CSubclassWnd::WndProc(HWND hWnd,UINT nMessage,WPARAM wParam,LPARAM lParam)
  69. {// begin WndProc
  70.     switch(nMessage)
  71.     {// begin nMessage switch
  72.     case WM_DESTROY:
  73.         OnDestroy();
  74.         break;
  75.     }// end nMessage swtich
  76.     return CallWindowProc(m_wpOldWndProc,hWnd, nMessage, wParam, lParam);
  77. }// end WndProc
  78.  
  79. void CSubclassWnd::OnDestroy()
  80. {// begin OnDestroy
  81.     // remove subclassing
  82.     if(m_wpOldWndProc)
  83.     {
  84.         SetWindowLong(m_hWnd,GWL_WNDPROC,(LONG)m_wpOldWndProc);
  85.         m_wpOldWndProc = NULL;
  86.     }
  87.     m_hWnd = NULL;
  88. }// end OnDestroy
  89.  
  90. HWND CSubclassWnd::GetSafeHwnd()
  91. {// begin GetSafeHwnd
  92.     return m_hWnd;
  93. }// end GetSafeHwnd
  94.