home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / MVC.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-24  |  4.5 KB  |  182 lines

  1. #pragma once
  2.  
  3. //-----------------------------------------------------------------------------------//
  4. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  5. //                             ISBN  0-13-086985-6                                   //
  6. //                                                                                   //
  7. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  8. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  9. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  10. //                                                                                   //
  11. //  FileName   : mvc.h                                                                 //
  12. //  Description: KView class (drawing), KMDICanvas class (MDI child window)          //
  13. //  Version    : 1.00.000, May 31, 2000                                              //
  14. //-----------------------------------------------------------------------------------//
  15.  
  16. class KView
  17. {
  18. protected:
  19.     int m_nPixelWidth;
  20.     int m_nPixelHeight;
  21.  
  22. public:
  23.  
  24.     typedef enum { View_NoChange = 0, View_Redraw = 1, View_Resize = 2 };
  25.  
  26.     KView(void)
  27.     {
  28.         m_nPixelWidth  = 320;
  29.         m_nPixelHeight = 240;
  30.     }
  31.  
  32.     ~KView(void)
  33.     {
  34.     }
  35.  
  36.     int GetPixelWidth(void) const
  37.     {
  38.         return m_nPixelWidth;
  39.     }
  40.  
  41.     int GetPixelHeight(void) const
  42.     {
  43.         return m_nPixelHeight;
  44.     }
  45.  
  46.     virtual int OnCommand(int cmd, HWND hWnd) = 0;
  47.     virtual int OnDraw(HDC hDC, const RECT * rcPaint) = 0;
  48.     
  49.     virtual int OnKey(int vkey)
  50.     {
  51.         return View_NoChange;
  52.     }
  53. };
  54.  
  55.  
  56. ///////////////////////////////////////////////////
  57. //                                     //
  58. // KMDICanvas: A Generic MDI Child Window Class  //
  59. //                                               //
  60. /////////////////////////////////////////////////// 
  61.  
  62. class KMDICanvas : public KScrollCanvas
  63. {
  64.     HMENU        m_hViewMenu;
  65.     int            m_nMenuID;
  66.     int            m_nIconID;
  67.     KView *        m_pView;
  68.     HINSTANCE    m_hInstance;
  69.     HBRUSH        m_hBackground;
  70.     
  71.     void Response(int rslt)
  72.     {
  73.         if ( rslt & KView::View_Resize )
  74.         {
  75.             if ( m_pView )
  76.                 SetSize(m_pView->GetPixelWidth(), m_pView->GetPixelHeight(), 20, 20, TRUE);
  77.             rslt |= KView::View_Redraw;
  78.         }
  79.  
  80.         if ( rslt & KView::View_Redraw )
  81.             InvalidateRect(m_hWnd, NULL, TRUE);
  82.     }
  83.  
  84.     virtual void  OnDraw(HDC hDC, const RECT * rcPaint)
  85.     {
  86.         if ( m_pView )
  87.             Response(m_pView->OnDraw(hDC, rcPaint));
  88.     }
  89.  
  90.     virtual LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  91.     {
  92.         switch( uMsg )
  93.         {
  94.             case WM_CREATE:
  95.                 m_hWnd        = hWnd;
  96.                 m_hViewMenu = LoadMenu(m_hInstance, MAKEINTRESOURCE(m_nMenuID));
  97.                 
  98.                 if ( m_pView )
  99.                     SetSize(m_pView->GetPixelWidth(), m_pView->GetPixelHeight(), 20, 20, TRUE);
  100.                 else
  101.                     SetSize(320, 240, 20, 20, TRUE);
  102.                 return 0;
  103.  
  104.             case WM_PAINT:
  105.                 return KScrollCanvas::WndProc(hWnd, uMsg, wParam, lParam);
  106.  
  107.             case WM_SIZE:
  108.             case WM_HSCROLL:
  109.             case WM_VSCROLL:
  110.                 return KScrollCanvas::WndProc(hWnd, uMsg, wParam, lParam);
  111.  
  112.             case WM_COMMAND:
  113.                 if ( m_pView )
  114.                     Response( m_pView->OnCommand(LOWORD(wParam), hWnd) );
  115.                 return 0;
  116.                 break;
  117.  
  118.             case WM_KEYDOWN:
  119.                 if ( m_pView )
  120.                     Response( m_pView->OnKey(wParam) );
  121.                 return 0;
  122.  
  123.             default:
  124.                 return CommonMDIChildProc(hWnd, uMsg, wParam, lParam, m_hViewMenu, 3);
  125.         }
  126.     }
  127.  
  128.     void GetWndClassEx(WNDCLASSEX & wc)
  129.     {
  130.         memset(& wc, 0, sizeof(wc));
  131.  
  132.         wc.cbSize         = sizeof(WNDCLASSEX);
  133.         wc.style          = CS_HREDRAW | CS_VREDRAW;
  134.         wc.lpfnWndProc    = WindowProc;
  135.         wc.cbClsExtra     = 0;
  136.         wc.cbWndExtra     = 0;       
  137.         wc.hInstance      = NULL;
  138.         wc.hIcon          = LoadIcon(m_hInstance, MAKEINTRESOURCE(m_nIconID));
  139.         wc.hCursor        = LoadCursor(NULL, IDC_ARROW); 
  140.  
  141.         if ( m_hBackground )
  142.             wc.hbrBackground  = m_hBackground;
  143.         else
  144.             wc.hbrBackground  = (HBRUSH) (COLOR_WINDOW + 1);
  145.         
  146.         wc.lpszMenuName   = NULL;
  147.         wc.lpszClassName  = NULL;
  148.         wc.hIconSm        = NULL;
  149.     }
  150.     
  151. public:
  152.  
  153.     const TCHAR * GetClassName(void) const
  154.     {
  155.         return _T("MDICanvas");
  156.     }
  157.  
  158.     bool Initialize(HINSTANCE hInstance, KStatusWindow * pStatus, KView * pView, int menuid, int iconid)
  159.     {
  160.         m_nMenuID   = menuid;
  161.         m_nIconID   = iconid;
  162.         m_hInstance = hInstance;
  163.         m_pStatus   = pStatus;
  164.         m_pView     = pView;
  165.  
  166.         RegisterClass(GetClassName(), hInstance);
  167.         
  168.         return true;
  169.     }
  170.  
  171.     KMDICanvas(HBRUSH hBackground = NULL)
  172.     {
  173.         m_hBackground = hBackground;
  174.     }
  175.  
  176.     ~KMDICanvas()
  177.     {
  178.         if ( m_pView )
  179.             delete m_pView;
  180.     }
  181. };
  182.