home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_16 / EMFScope / WINPP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-12  |  6.1 KB  |  240 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   : winpp.cpp                                                             //
  10. //  Description: Another C++ window class                                            //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #include <windows.h>
  16. #include <assert.h>
  17.  
  18. #include "Winpp.h"
  19.  
  20.  
  21. int mapx(int x)
  22. {
  23.     return LOWORD(GetDialogBaseUnits()) * x / 4;
  24. }
  25.  
  26.  
  27. int mapy(int y)
  28. {
  29.     return HIWORD(GetDialogBaseUnits()) * y / 8;
  30. }
  31.  
  32.  
  33. int unmapx(int x)
  34. {
  35.     return x * 4 / LOWORD(GetDialogBaseUnits());
  36. }
  37.  
  38.  
  39. int unmapy(int y)
  40. {
  41.     return y * 8 / HIWORD(GetDialogBaseUnits());
  42. }
  43.  
  44.  
  45. const char *LoadStringTemp(int id)
  46. {
  47.     static char buffer[256];
  48.  
  49.     if (!LoadString(0, id, buffer, sizeof(buffer)-2))
  50.         buffer[0] = 0;
  51.  
  52.     return buffer;
  53. }
  54.  
  55.  
  56. HWND KWindow::Createwindow(LPCTSTR   lpClassName,
  57.                            int       id_icon,
  58.                            int       id_cursor,
  59.  
  60.                            LPCTSTR   lpWindowName,    
  61.                            DWORD     dwStyle,    
  62.                            int       x, 
  63.                            int       y, 
  64.                            int       w, 
  65.                            int       h,
  66.                            HWND      hWndParent,    
  67.                            HINSTANCE hInstance,
  68.                            HMENU     hMenu,
  69.                            DWORD     dwExStyle,
  70.                            HBRUSH    hBrush)
  71. {
  72.     char ClassName[MAX_PATH];
  73.     HWND Sibling;
  74.  
  75.     wsprintf(ClassName, "%s_%08lx", lpClassName, hInstance);
  76.  
  77.     Sibling = FindWindow(ClassName, NULL);
  78.  
  79.     HCURSOR hCursor = LoadCursor(hInstance, MAKEINTRESOURCE(id_cursor));
  80.     if (hCursor==NULL)
  81.         hCursor = LoadCursor(NULL, IDC_ARROW);
  82.  
  83.     HICON   hIcon   = LoadIcon(hInstance, MAKEINTRESOURCE(id_icon));
  84.     
  85.     if (Sibling != NULL)
  86.     {
  87.         SetClassLong(Sibling, GCL_WNDPROC, (DWORD) Thunk);
  88.         SetClassLong(Sibling, GCL_HCURSOR, (DWORD) hCursor);
  89.         SetClassLong(Sibling, GCL_HICON,   (DWORD) hIcon);
  90.     }
  91.     else
  92.     {
  93.         WNDCLASS wc;
  94.  
  95.         if (GetClassInfo(hInstance, ClassName, &wc))
  96.             UnregisterClass(ClassName, hInstance);
  97.         
  98.         wc.style          = CS_HREDRAW | CS_VREDRAW;
  99.         wc.lpfnWndProc    = (WNDPROC) (void *) Thunk;
  100.         wc.cbClsExtra     = 0;
  101.         wc.cbWndExtra     = 0;       
  102.         wc.hInstance      = hInstance;
  103.         wc.hIcon          = hIcon;
  104.         wc.hCursor        = hCursor;
  105.         wc.hbrBackground  = hBrush; 
  106.         wc.lpszMenuName   = NULL;
  107.         wc.lpszClassName  = ClassName;
  108.  
  109.         if (!RegisterClass(&wc))
  110.             return NULL;
  111.     }
  112.  
  113.     m_hWnd = CreateWindowEx(dwExStyle, 
  114.                             ClassName,
  115.                             lpWindowName,
  116.                             dwStyle, 
  117.                             x, y, w, h,
  118.                             hWndParent, 
  119.                             hMenu, 
  120.                             hInstance,
  121.                             NULL);
  122.  
  123.     return m_hWnd;
  124.  
  125. }
  126.  
  127.  
  128. int KWindow::MessageLoop(int nCmdShow)
  129. {    
  130.     MSG  msg;
  131.     
  132.     ShowWindow(m_hWnd, nCmdShow);
  133.     
  134.     while (GetMessage(&msg, NULL, 0, 0))
  135.     {
  136.         TranslateMessage(&msg);
  137.         DispatchMessage(&msg);
  138.     }
  139.  
  140.     return msg.wParam;
  141. }
  142.  
  143. #define SCROLL_RATIO   5
  144.  
  145. void KWindow::Scroll (int message, WORD wPos, WORD wScrollType)
  146. {
  147.     int  xBar;                       // Where scrollbar is now.
  148.     int  nMin;                       // Minumum scroll bar value.
  149.     int  nMax;                       // Maximum scroll bar value.
  150.     int  dx;                         // How much to move.
  151.     int  nOneUnit;                   // # of pixels for LINEUP/LINEDOWN
  152.     int  cxClient;                   // Width of client area.
  153.     int  nHorzOrVert;                // Doing the horizontal or vertical?
  154.     RECT rect;                       // Client area.
  155.  
  156.     GetClientRect (m_hWnd, &rect);
  157.  
  158.     if (message == WM_HSCROLL)
  159.     {
  160.         nHorzOrVert = SB_HORZ;
  161.         cxClient    = rect.right - rect.left;
  162.     }
  163.     else
  164.     {
  165.         nHorzOrVert = SB_VERT;
  166.         cxClient    = rect.bottom - rect.top;
  167.     }
  168.  
  169.     // One a SB_LINEUP/SB_LINEDOWN we will move the DIB by
  170.     //  1/SCROLL_RATIO of the client area (i.e. if SCROLL_RATIO
  171.     //  is 4, it will scroll the DIB a quarter of the client
  172.     //  area's height or width.
  173.  
  174.     nOneUnit = cxClient / SCROLL_RATIO;
  175.     if (!nOneUnit)
  176.         nOneUnit = 1;
  177.  
  178.     xBar = GetScrollPos (m_hWnd, nHorzOrVert);
  179.     GetScrollRange (m_hWnd, nHorzOrVert, &nMin, &nMax);
  180.  
  181.     switch (wScrollType)
  182.     {
  183.         case SB_LINEDOWN:             // One line right.
  184.             dx = nOneUnit;
  185.             break;
  186.  
  187.         case SB_LINEUP:               // One line left.
  188.             dx = -nOneUnit;
  189.             break;
  190.  
  191.         case SB_PAGEDOWN:             // One page right.
  192.             dx = cxClient;
  193.             break;
  194.  
  195.         case SB_PAGEUP:               // One page left.
  196.             dx = -cxClient;
  197.             break;
  198.  
  199.         case SB_THUMBPOSITION:        // Absolute position.
  200.             dx = wPos - xBar;
  201.             break;
  202.  
  203.         default:                      // No change.
  204.             dx = 0;
  205.             break;
  206.     }
  207.  
  208.     if (dx)
  209.     {
  210.         xBar += dx;
  211.  
  212.         if (xBar < nMin)
  213.         {
  214.             dx  -= xBar - nMin;
  215.             xBar = nMin;
  216.         }
  217.  
  218.         if (xBar > nMax)
  219.         {
  220.             dx  -= xBar - nMax;
  221.             xBar = nMax;
  222.         }
  223.  
  224.         if (dx)
  225.         {
  226.             SetScrollPos (m_hWnd, nHorzOrVert, xBar, TRUE);
  227.  
  228.             if (nHorzOrVert == SB_HORZ)
  229.                 ScrollWindow (m_hWnd, -dx, 0, NULL, NULL);
  230.             else
  231.                 ScrollWindow (m_hWnd, 0, -dx, NULL, NULL);
  232.  
  233.             UpdateWindow (m_hWnd);
  234.         }
  235.     }
  236. }
  237.  
  238.  
  239.  
  240.