home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_05 / WinPaint / WinPaint.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-20  |  7.1 KB  |  277 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   : winpaint.cpp                                                         //
  10. //  Description: Visualize WM_PAINT message                                          //
  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.  
  21. #include "..\..\include\win.h"
  22. #include "..\..\include\Canvas.h"
  23. #include "..\..\include\Status.h"
  24. #include "..\..\include\FrameWnd.h"
  25. #include "..\..\include\LogWindow.h"
  26.  
  27. #include "Resource.h"
  28.  
  29.  
  30. class KMyCanvas : public KCanvas
  31. {
  32.     virtual void    OnDraw(HDC hDC, const RECT * rcPaint);
  33.     virtual LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  34.  
  35.     int                m_nRepaint;    
  36.     
  37.     int                m_Red, m_Green, m_Blue;
  38.     HRGN            m_hRegion;
  39.     KLogWindow        m_Log;
  40.     DWORD            m_Redraw;
  41.  
  42. public:
  43.  
  44.     BOOL OnCommand(WPARAM wParam, LPARAM lParam);
  45.  
  46.     KMyCanvas()
  47.     {
  48.         m_nRepaint = 0;
  49.         m_hRegion  = CreateRectRgn(0, 0, 1, 1);
  50.         
  51.         m_Red       = 0x4F;
  52.         m_Green    = 0x8F;
  53.         m_Blue     = 0xCF;
  54.         m_Redraw   = 0;
  55.     }
  56. };
  57.  
  58.  
  59. BOOL KMyCanvas::OnCommand(WPARAM wParam, LPARAM lParam)
  60. {
  61.     switch ( LOWORD(wParam) )
  62.     {
  63.         case IDM_VIEW_HREDRAW:
  64.         case IDM_VIEW_VREDRAW:
  65.             {
  66.                 HMENU hMenu = GetMenu(GetParent(m_hWnd));
  67.  
  68.                 MENUITEMINFO mii;
  69.                 
  70.                 memset(& mii, 0, sizeof(mii));
  71.                 mii.cbSize = sizeof(mii);
  72.                 mii.fMask  = MIIM_STATE;
  73.                 
  74.                 if ( GetMenuState(hMenu, LOWORD(wParam), MF_BYCOMMAND) & MF_CHECKED )
  75.                     mii.fState = MF_UNCHECKED;
  76.                 else
  77.                     mii.fState = MF_CHECKED;
  78.                 SetMenuItemInfo(hMenu, LOWORD(wParam), FALSE, & mii);
  79.                 
  80.                 if ( LOWORD(wParam)==IDM_VIEW_HREDRAW )
  81.                     m_Redraw ^= WVR_HREDRAW;
  82.                 else
  83.                     m_Redraw ^= WVR_VREDRAW;
  84.             }
  85.             return TRUE;
  86.  
  87.         case IDM_FILE_EXIT:
  88.             DestroyWindow(GetParent(m_hWnd));
  89.             return TRUE;
  90.     }
  91.  
  92.     return FALSE;    // not processed
  93. }
  94.  
  95.  
  96. LRESULT KMyCanvas::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  97. {
  98.     LRESULT lr;
  99.  
  100.     switch( uMsg )
  101.     {
  102.         case WM_CREATE:
  103.             m_hWnd = hWnd;
  104.             m_Log.Create(m_hInst, _T("WinPaint"), LoadIcon(m_hInst, MAKEINTRESOURCE(IDI_GRAPH)));
  105.             m_Log.Log(_T("WM_CREATE\r\n"));
  106.             return 0;
  107.  
  108.         case WM_NCCALCSIZE:
  109.             m_Log.Log(_T("WM_NCCALCSIZE\r\n"));
  110.             lr = DefWindowProc(hWnd, uMsg, wParam, lParam);
  111.             m_Log.Log("WM_NCCALCSIZE returns %x\r\n", lr);
  112.  
  113.             if ( wParam )
  114.             {
  115.                 lr &= ~ (WVR_HREDRAW | WVR_VREDRAW);
  116.                 lr |= m_Redraw;
  117.             }
  118.             break;
  119.  
  120.         case WM_NCPAINT:
  121.             m_Log.Log("WM_NCPAINT HRGN %0x\r\n", (HRGN) wParam);
  122.             lr = DefWindowProc(hWnd, uMsg, wParam, lParam);
  123.             m_Log.Log("WN_NCPAINT returns\r\n");
  124.             break;
  125.  
  126.         case WM_ERASEBKGND:
  127.             m_Log.Log("WM_ERASEBKGND HDC %0x\r\n", (HDC) wParam);
  128.             lr = DefWindowProc(hWnd, uMsg, wParam, lParam);
  129.             m_Log.Log("WM_ERASEBKGND returns\r\n");
  130.             break;
  131.  
  132.         case WM_SIZE:
  133.             m_Log.Log("WM_SIZE type %d, width %d, height %d\r\n", wParam, LOWORD(lParam), HIWORD(lParam));
  134.             lr = DefWindowProc(hWnd, uMsg, wParam, lParam);
  135.             m_Log.Log("WM_SIZE returns\r\n");
  136.             break;
  137.  
  138.         case WM_PAINT:
  139.             {
  140.                 PAINTSTRUCT ps; 
  141.  
  142.                 m_Log.Log("WM_PAINT\r\n");
  143.                 m_Log.Log("BeginPaint\r\n");
  144.                 HDC hDC = BeginPaint(m_hWnd, &ps);
  145.                 m_Log.Log("BeginPaint returns HDC %8x\r\n", hDC);
  146.  
  147.                 OnDraw(hDC, &ps.rcPaint);
  148.  
  149.                 m_Log.Log("EndPaint\r\n");
  150.                 EndPaint(m_hWnd, &ps);
  151.                 m_Log.Log("EndPaint returns GetObjectType(%08x)=%d\r\n", hDC, GetObjectType(hDC));
  152.                 m_Log.Log("WM_PAINT returns\r\n");
  153.             }
  154.             return 0;
  155.  
  156.         default:
  157.             lr = DefWindowProc(hWnd, uMsg, wParam, lParam);
  158.     }
  159.  
  160.     return lr;
  161. }
  162.  
  163.  
  164. void KMyCanvas::OnDraw(HDC hDC, const RECT * rcPaint)
  165. {
  166.     RECT rect;
  167.  
  168.     GetClientRect(m_hWnd, & rect);
  169.     
  170.     GetRandomRgn(hDC, m_hRegion, SYSRGN);
  171.     
  172.     POINT Origin;
  173.     GetDCOrgEx(hDC, & Origin);
  174.  
  175.     if ( ((unsigned) hDC) & 0xFFFF0000 )
  176.         OffsetRgn(m_hRegion, - Origin.x, - Origin.y);
  177.  
  178.     m_nRepaint ++;
  179.  
  180.     TCHAR mess[64];
  181.  
  182.     wsprintf(mess, _T("HDC 0x%X, Org(%d, %d)"), hDC, Origin.x, Origin.y); 
  183.     if ( m_pStatus )
  184.         m_pStatus->SetText(pane_1, mess);
  185.  
  186.     switch ( m_nRepaint % 3 )
  187.     {
  188.         case 0: m_Red  = (m_Red   + 0x31) & 0xFF; break;
  189.         case 1: m_Green= (m_Green + 0x31) & 0xFF; break;
  190.         case 2: m_Blue = (m_Blue  + 0x31) & 0xFF; break;
  191.     }
  192.  
  193.     SetTextAlign(hDC, TA_TOP | TA_CENTER);
  194.  
  195.     int size = GetRegionData(m_hRegion, 0, NULL);
  196.     int rectcount = 0;
  197.  
  198.     if ( size )
  199.     {
  200.         RGNDATA * pRegion = (RGNDATA *) new char[size];
  201.         GetRegionData(m_hRegion, size, pRegion);
  202.  
  203.         const RECT * pRect = (const RECT *) & pRegion->Buffer;
  204.         rectcount = pRegion->rdh.nCount;
  205.  
  206.         TEXTMETRIC tm;
  207.         GetTextMetrics(hDC, & tm);
  208.         int lineheight = tm.tmHeight + tm.tmExternalLeading; 
  209.  
  210.         for (unsigned i=0; i<pRegion->rdh.nCount; i++)
  211.         {
  212.             int x = (pRect[i].left + pRect[i].right)/2;
  213.             int y = (pRect[i].top + pRect[i].bottom)/2;
  214.  
  215.             wsprintf(mess, "WM_PAINT %d, rect %d", m_nRepaint, i+1);
  216.             ::TextOut(hDC, x, y - lineheight, mess, _tcslen(mess));
  217.  
  218.             wsprintf(mess, "(%d, %d, %d, %d)", pRect[i].left, pRect[i].top, pRect[i].right, pRect[i].bottom);
  219.             ::TextOut(hDC, x, y, mess, _tcslen(mess));
  220.  
  221.         }
  222.  
  223.         delete [] (char *) pRegion;
  224.     }
  225.  
  226.     wsprintf(mess, _T("WM_PAINT message %d, %d rects in sysrgn"), m_nRepaint, rectcount);
  227.     if ( m_pStatus )
  228.         m_pStatus->SetText(pane_2, mess);
  229.  
  230.  
  231.     HBRUSH hBrush = CreateSolidBrush(RGB(m_Red, m_Green, m_Blue));
  232.  
  233.     FrameRgn(hDC, m_hRegion, hBrush, 4, 4);
  234.     FrameRgn(hDC, m_hRegion, (HBRUSH) GetStockObject(WHITE_BRUSH), 1, 1);
  235.  
  236.     DeleteObject(hBrush);
  237. }
  238.  
  239.  
  240. class KMyFrame : public KFrame
  241. {
  242.     void GetWndClassEx(WNDCLASSEX & wc)
  243.     {
  244.         KFrame::GetWndClassEx(wc);
  245.         wc.hIcon  = LoadIcon(m_hInst, MAKEINTRESOURCE(IDI_GRAPH));
  246.     }
  247.  
  248. public:
  249.     KMyFrame(HINSTANCE hInstance, const TBBUTTON * pButtons, int nCount,
  250.             KToolbar * pToolbar, KCanvas * pCanvas, KStatusWindow * pStatus) :
  251.         KFrame(hInstance, pButtons, nCount, pToolbar, pCanvas, pStatus)
  252.     {
  253.     }
  254.  
  255. };
  256.  
  257.  
  258. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int nShow)
  259. {
  260.     KMyCanvas     canvas;
  261.     KStatusWindow status;
  262.  
  263.     KMyFrame frame(hInst, NULL, 0, NULL, & canvas, & status);
  264.  
  265.     frame.CreateEx(0, _T("ClassName"), _T("WinPaint"),
  266.         WS_OVERLAPPEDWINDOW,
  267.         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
  268.         NULL, LoadMenu(hInst, MAKEINTRESOURCE(IDR_MAIN)), hInst);
  269.  
  270.     frame.ShowWindow(nShow);
  271.     frame.UpdateWindow();
  272.  
  273.     frame.MessageLoop();
  274.  
  275.     return 0;
  276. }
  277.