home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / win.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-20  |  7.1 KB  |  264 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   : win.cpp                                                             //
  10. //  Description: Generic Window Class                                                //
  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 <assert.h>
  19. #include <tchar.h>
  20. #include ".\win.h"
  21.  
  22.  
  23. // Default message handler for main program window, dispatch to OnKeyDown, OnDraw, etc.
  24. LRESULT KWindow::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  25. {
  26.     switch( uMsg )
  27.     {
  28.         case WM_KEYDOWN:
  29.             OnKeyDown(wParam, lParam);
  30.             return 0;
  31.  
  32.         case WM_PAINT:
  33.         {
  34.             PAINTSTRUCT ps; 
  35.                 
  36.             BeginPaint(m_hWnd, &ps);
  37.             OnDraw(ps.hdc);
  38.             EndPaint(m_hWnd, &ps);
  39.         }
  40.         return 0;
  41.  
  42.         case WM_PALETTEISCHANGING: // should not happen
  43.             MessageBox(NULL, _T("Hello"), _T("Hi"), MB_OK);
  44.             return 0;
  45.  
  46.         case WM_PALETTECHANGED:
  47.             return OnPaletteChanged(hWnd, wParam);
  48.  
  49.         case WM_QUERYNEWPALETTE:
  50.             return OnQueryNewPalette();
  51.  
  52.         case WM_DESTROY:
  53.             if ( m_bMainWindow )
  54.                 PostQuitMessage(0); // main window only
  55.             return 0;
  56.     }
  57.  
  58.    return DefWindowProc(hWnd, uMsg, wParam, lParam);
  59. }
  60.  
  61.  
  62. // Generic window procedure passed to WIN32 API, dispatches to KWindow::WndProc
  63. LRESULT CALLBACK KWindow::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  64. {
  65.     KWindow * pWindow;
  66.         
  67.     if ( uMsg==WM_NCCREATE )
  68.     {   
  69.         assert( ! IsBadReadPtr((void *) lParam, sizeof(CREATESTRUCT)) );
  70.         MDICREATESTRUCT * pMDIC = (MDICREATESTRUCT *) ((LPCREATESTRUCT) lParam)->lpCreateParams;
  71.  
  72.         pWindow = (KWindow *) (pMDIC->lParam);
  73.  
  74.         assert( ! IsBadReadPtr(pWindow, sizeof(KWindow)) );
  75.         SetWindowLong(hWnd, GWL_USERDATA, (LONG) pWindow);
  76.     }
  77.     else
  78.         pWindow=(KWindow *)GetWindowLong(hWnd, GWL_USERDATA);
  79.  
  80.     if ( pWindow )
  81.         return pWindow->WndProc(hWnd, uMsg, wParam, lParam);
  82.     else
  83.         return DefWindowProc(hWnd, uMsg, wParam, lParam);
  84. }
  85.  
  86.  
  87. // Register WNDCLASS for the window class. Registering only once
  88. bool KWindow::RegisterClass(LPCTSTR lpszClass, HINSTANCE hInst)
  89. {
  90.     WNDCLASSEX wc;
  91.  
  92.     // Check if class is already registered
  93.     wc.cbSize = sizeof(wc);
  94.  
  95.     if ( ! GetClassInfoEx(hInst, lpszClass, &wc) )
  96.     {
  97.         GetWndClassEx(wc);                // fill class attributes
  98.  
  99.         wc.hInstance     = hInst;
  100.         wc.lpszClassName = lpszClass;
  101.  
  102.         if ( !RegisterClassEx(&wc) )    // register
  103.             return false;
  104.     }
  105.  
  106.     return true;
  107. }
  108.  
  109.  
  110. // Handles window creation
  111. bool KWindow::CreateEx(DWORD dwExStyle, LPCTSTR lpszClass, LPCTSTR lpszName, DWORD dwStyle, 
  112.     int x, int y, int nWidth, int nHeight, HWND hParent, 
  113.     HMENU hMenu, HINSTANCE hInst)
  114. {
  115.     if ( ! RegisterClass(lpszClass, hInst) )
  116.         return false;
  117.  
  118.     // Use MDICREATESTRUCT to support MDI child window
  119.     MDICREATESTRUCT mdic;
  120.     memset(& mdic, 0, sizeof(mdic));
  121.     mdic.lParam = (LPARAM) this;
  122.  
  123.     m_hWnd = CreateWindowEx(dwExStyle, lpszClass, 
  124.         lpszName, dwStyle, x, y, nWidth, nHeight, 
  125.         hParent, hMenu, hInst, & mdic);
  126.     
  127.     return m_hWnd!=NULL;
  128. }
  129.  
  130.  
  131. // Fill WNDCLASSEX, virtual function
  132. void KWindow::GetWndClassEx(WNDCLASSEX & wc)
  133. {
  134.     memset(& wc, 0, sizeof(wc));
  135.  
  136.     wc.cbSize        = sizeof(WNDCLASSEX);
  137.     wc.style         = 0; // CS_HREDRAW | CS_VREDRAW;
  138.     wc.lpfnWndProc   = WindowProc;
  139.     wc.cbClsExtra    = 0;
  140.     wc.cbWndExtra    = 0;       
  141.     wc.hInstance     = NULL;
  142.     wc.hIcon         = NULL;
  143.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  144.     wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
  145.     wc.lpszMenuName  = NULL;
  146.     wc.lpszClassName = NULL;
  147.     wc.hIconSm       = NULL;
  148. }
  149.  
  150.  
  151. WPARAM KWindow::MessageLoop(void)
  152. {
  153.     MSG msg;
  154.  
  155.     m_bMainWindow = true;
  156.  
  157.     while ( GetMessage(&msg, NULL, 0, 0) )
  158.     {
  159. #ifdef _DEBUG
  160.         TCHAR temp[MAX_PATH];
  161.  
  162.         wsprintf(temp, _T("Message(0x%x, 0x%x, 0x%x, 0x%x)\n"), msg.hwnd, msg.message, msg.wParam, msg.lParam);
  163.         OutputDebugString(temp);
  164. #endif
  165.         
  166.         TranslateMessage(&msg);
  167.         DispatchMessage(&msg);
  168.     }
  169.  
  170.     return msg.wParam;
  171. }
  172.  
  173.  
  174. // Common message processing for MDI Child Window
  175. HRESULT KWindow::CommonMDIChildProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, 
  176.                                     HMENU hMenu, int nWindowMenu)
  177. {
  178.     switch ( uMsg )
  179.     {
  180.         case WM_NCDESTROY:                           // should be the last message
  181.             SetWindowLong(hWnd, GWL_USERDATA, 0);  // make sure no more message through WindowProc
  182.             delete this;                           // MDI child are created using new operator, time to delete    
  183.             return 0;
  184.  
  185.         case WM_MDIACTIVATE:
  186.             if ( lParam==(LPARAM) hWnd )           // if current window activate, switch menu
  187.                 SendMessage(GetParent(hWnd), WM_MDISETMENU, (WPARAM) hMenu, (LPARAM) GetSubMenu(hMenu, nWindowMenu));
  188.  
  189.             // send message to parent window to response to child window change
  190.             SendMessage(GetParent(GetParent(hWnd)), WM_USER, lParam!=(LPARAM) hWnd, 0);    
  191.             return 0;
  192.             
  193.         default:
  194.             // generic MDI child window message handling provided by OS
  195.             return DefMDIChildProc(hWnd, uMsg, wParam, lParam);
  196.     }
  197. }
  198.  
  199.  
  200. LRESULT KWindow::OnQueryNewPalette(void)
  201. {
  202.     if ( m_hPalette==NULL )
  203.         return FALSE;
  204.  
  205.     HDC      hDC = GetDC(m_hWnd);
  206.     HPALETTE hOld= SelectPalette(hDC, m_hPalette, FALSE);
  207.  
  208.     BOOL changed = RealizePalette(hDC) != 0;
  209.     SelectPalette(hDC, hOld, FALSE);
  210.     ReleaseDC(m_hWnd, hDC);
  211.  
  212.     if ( changed )
  213.     {
  214.         OutputDebugString(_T("InvalidateRect\n"));
  215.         InvalidateRect(m_hWnd, NULL, TRUE); // repaint
  216.     }
  217.  
  218.     return changed;
  219. }
  220.  
  221.  
  222. LRESULT KWindow::OnPaletteChanged(HWND hWnd, WPARAM wParam)
  223.     if ( ( hWnd != (HWND) wParam ) && m_hPalette )
  224.     {
  225.         HDC hDC = GetDC(hWnd);
  226.         HPALETTE hOld = SelectPalette(hDC, m_hPalette, FALSE);
  227.                 
  228.         if ( RealizePalette(hDC) )
  229.             if ( m_nUpdateCount >=2 )
  230.             {
  231.                 InvalidateRect(hWnd, NULL, TRUE);
  232.                 m_nUpdateCount = 0;
  233.             }
  234.             else
  235.             {
  236.                 UpdateColors(hDC);
  237.                 m_nUpdateCount ++;
  238.             }
  239.  
  240.         SelectPalette(hDC, hOld, FALSE);
  241.         ReleaseDC(hWnd, hDC);
  242.     }
  243.  
  244.     return 0;
  245. }
  246.  
  247.  
  248. int MyMessageBox(HWND hWnd, const TCHAR * text, const TCHAR * caption, DWORD style, int iconid)
  249. {
  250.     MSGBOXPARAMS param;
  251.  
  252.     memset(& param, 0, sizeof(param));
  253.     param.cbSize      = sizeof(param);
  254.     param.hwndOwner   = hWnd;
  255.     param.hInstance   = GetModuleHandle(NULL);
  256.     param.lpszText    = text;
  257.     param.lpszCaption = caption;
  258.     param.dwStyle     = style | MB_USERICON;
  259.     param.lpszIcon    = MAKEINTRESOURCE(iconid);
  260.  
  261.     return MessageBoxIndirect(¶m);
  262. }
  263.