home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / Background.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-11  |  2.9 KB  |  93 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : background.cpp                                                         //
  10. //  Description: Use DDB as window background                                        //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define WIN32_LEAN_AND_MEAN
  16.  
  17. #include <windows.h>
  18. #include <tchar.h>
  19. #include <assert.h>
  20.  
  21. #include "DDB.h"
  22. #include "Background.h"
  23.  
  24. const TCHAR Prop_KBackground[] = _T("KBackground Instance");
  25.  
  26. LRESULT KBackground::EraseBackground(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  27. {
  28.     return DefWindowProc(hWnd, uMsg, wParam, lParam);
  29. }
  30.  
  31. LRESULT KBackground::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  32. {
  33.     if ( uMsg == WM_ERASEBKGND )
  34.         return EraseBackground(hWnd, uMsg, wParam, lParam);
  35.     else
  36.         return CallWindowProc(m_OldProc, hWnd, uMsg, wParam, lParam);
  37. }
  38.  
  39. LRESULT KBackground::BackGroundWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  40. {
  41.     KBackground * pThis = (KBackground *) GetProp(hWnd, Prop_KBackground);
  42.  
  43.     if ( pThis )
  44.         return pThis->WndProc(hWnd, uMsg, wParam, lParam);
  45.     else
  46.         return DefWindowProc(hWnd, uMsg, wParam, lParam);
  47. }
  48.  
  49. BOOL KBackground::Attach(HWND hWnd)
  50. {
  51.     SetProp(hWnd, Prop_KBackground, this);
  52.     m_OldProc = (WNDPROC) SetWindowLong(hWnd, GWL_WNDPROC, (LONG) BackGroundWindowProc);
  53.  
  54.     return m_OldProc!=NULL;
  55. }
  56.  
  57.  
  58. BOOL KBackground::Detatch(HWND hWnd)
  59. {
  60.     RemoveProp(hWnd, Prop_KBackground);
  61.  
  62.     if ( m_OldProc )
  63.         return SetWindowLong(hWnd, GWL_WNDPROC, (LONG) m_OldProc) == (LONG) BackGroundWindowProc;
  64.     else
  65.         return FALSE;
  66. }
  67.  
  68.  
  69. LRESULT KDDBBackground::EraseBackground(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  70. {
  71.     if ( m_DDB.GetBitmap() )
  72.     {
  73.         RECT rect;
  74.         HDC hDC = (HDC) wParam;
  75.  
  76.         GetClientRect(hWnd, & rect);
  77.         HRGN hRgn = CreateRectRgnIndirect(&rect);
  78.  
  79.         SaveDC(hDC);
  80.         SelectClipRgn(hDC, hRgn);
  81.         DeleteObject(hRgn);
  82.  
  83.         m_DDB.Draw(hDC, rect.left, rect.top, rect.right - rect.left, 
  84.             rect.bottom - rect.top, SRCCOPY, m_nStyle);
  85.         RestoreDC(hDC, -1);
  86.  
  87.         return 1;    // processed
  88.     }
  89.     else
  90.         return DefWindowProc(hWnd, uMsg, wParam, lParam);
  91. }
  92.  
  93.