home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winui / shell / fileview / fvproc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-12  |  9.3 KB  |  302 lines

  1. //THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF 
  2. //ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
  3. //THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright  1994-1996  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //    PROGRAM:FVPROC.CPP        
  9. //
  10. //    PURPOSE:  Window procedures for main window and About box of the
  11. // sample FileViewer for text files.
  12. //
  13. //    PLATFORMS:    Windows 95
  14. //
  15. //    SPECIAL INSTRUCTIONS: N/A
  16. //
  17. //Always include the master FileViewer source include file here.
  18. #include "fileview.h"
  19.  
  20. //
  21. //   FUNCTION:      FileViewerFrameProc
  22. //
  23. //   PURPOSE:  Standard window procedure for the text file viewer frame window.
  24. //  Processes menu commands, acclerators, and handles resizing of
  25. //  the window  
  26. //
  27. long WINAPI FileViewerFrameProc(HWND hWnd, UINT iMsg, WPARAM wParam
  28.     , LPARAM lParam)
  29.     {
  30.     LONG            lRet;
  31.     PCFileViewer    pObj;
  32.     LPTOOLTIPTEXT   pTTT;
  33.  
  34.     //This is invalid until WM_NCCREATE is called.
  35.     pObj=(PCFileViewer)GetWindowLong(hWnd, FVWL_OBJECTPOINTER);
  36.  
  37.     switch (iMsg)
  38.         {
  39.         case WM_NCCREATE:
  40.             //Save the the CFileViewer object pointer we're passed
  41.             lRet=(LONG)((LPCREATESTRUCT)lParam)->lpCreateParams;
  42.             SetWindowLong(hWnd, FVWL_OBJECTPOINTER, lRet);
  43.             return DefWindowProc(hWnd, iMsg, wParam, lParam);
  44.  
  45.         case WM_COMMAND:
  46.             pObj->OnCommand(LOWORD(wParam), HIWORD(wParam), (HWND)lParam);
  47.             break;
  48.  
  49.         case WM_NOTIFY:
  50.              // The toolbar, created with TBSTYLE_TOOLTIPS, will
  51.              // send notifications for each button.  lParam will
  52.              // be an LPTOOLTIPTEXT in such a case, but even when
  53.              // the structure is something different it will always
  54.              // have a hdr field of type NMHDR as the first field
  55.              // which we use to see if it comes from the toolbar.
  56.              // The notification we want is TTN_NEEDTEXT.
  57.             pTTT=(LPTOOLTIPTEXT)lParam;
  58.  
  59.             if (NULL==pTTT)
  60.                 return 0L;
  61.  
  62.             if (TTN_NEEDTEXT==pTTT->hdr.code)
  63.                 pTTT->lpszText=pObj->PszToolTip(pTTT->hdr.idFrom);
  64.  
  65.             return 0L;
  66.  
  67.  
  68.         case WM_CLOSE:
  69.             DestroyWindow(hWnd);
  70.             break;
  71.  
  72.     case WM_DESTROY:
  73.             if (NULL != pObj)
  74.             {
  75.                 ODSu("FileViewerFrameProc Quit? ", pObj->m_fPostQuitMsg);
  76.                 if (pObj->m_fPostQuitMsg)
  77.                     PostQuitMessage(0);
  78.                 pObj->m_fPostQuitMsg = TRUE;    // One shot that it did not...
  79.  
  80.                 if (pObj->m_hWnd == hWnd)
  81.                     pObj->m_hWnd = NULL;        // Don't try to destory this again...
  82.             }
  83.             break;
  84.  
  85.         case WM_SIZE:
  86.             //Resize frame tools and viewport
  87.             pObj->ChildrenResize();
  88.             break;
  89.  
  90.         case WM_MENUSELECT:
  91.             //Win32 Parameters are wItem, wFlags, and hMenu
  92.             pObj->m_pSH->MenuSelect(LOWORD(wParam)
  93.                 , HIWORD(wParam), (HMENU)lParam);
  94.             break;
  95.  
  96.         case WM_DROPFILES:
  97.             // We have a new file dropped on us so we need to pass this
  98.             // information back to the caller of the viewer...
  99.             pObj->DropFiles((HDROP)wParam);
  100.             break;
  101.  
  102.         default:
  103.             return DefWindowProc(hWnd, iMsg, wParam, lParam);
  104.         }
  105.  
  106.     return 0L;
  107.     }
  108. //
  109. //   FUNCTION:  ViewportWndProc    
  110. //
  111. //   PURPOSE: Window procedure for the viewport window.  The viewport in
  112. //  this example just does draws the text into itself using an
  113. //  edit control.  
  114. //
  115. //   COMMENTS:
  116. //   This will need to be modified for your file viewer.
  117. //
  118. long WINAPI ViewportWndProc(HWND hWnd, UINT iMsg, WPARAM wParam
  119.     , LPARAM lParam)
  120.     {
  121.     LONG            lRet;
  122.     PCFileViewer    pObj;
  123.     PAINTSTRUCT     ps;
  124.  
  125.     //This is invalid until WM_NCCREATE is called.
  126.     pObj=(PCFileViewer)GetWindowLong(hWnd, VPWL_OBJECTPOINTER);
  127.  
  128.     switch (iMsg)
  129.         {
  130.         case WM_NCCREATE:
  131.             //Save the the CFileViewer object pointer we're passed
  132.             lRet=(LONG)((LPCREATESTRUCT)lParam)->lpCreateParams;
  133.             SetWindowLong(hWnd, VPWL_OBJECTPOINTER, lRet);
  134.             return DefWindowProc(hWnd, iMsg, wParam, lParam);
  135.  
  136.         case WM_PAINT:
  137.             BeginPaint(hWnd, &ps);
  138.  
  139.             if (NULL!=pObj->m_hMemText)
  140.                 {
  141.                 LPSTR       psz;
  142.                 RECT        rc;
  143.                 HFONT       hFont;
  144.  
  145.                 GetClientRect(hWnd, &rc);
  146.  
  147.                 SetTextColor(ps.hdc, GetSysColor(COLOR_WINDOWTEXT));
  148.                 SetBkColor(ps.hdc, GetSysColor(COLOR_WINDOW));
  149.                 hFont=(HFONT)SelectObject(ps.hdc, pObj->m_hFont);
  150.  
  151.                 psz=(LPSTR)GlobalLock(pObj->m_hMemText);
  152.  
  153.                  // We can use the client area rectangle here for
  154.                  // drawing since we also use DT_NOCLIP, so we'll
  155.                  // always draw all over the client area.
  156.                 OffsetRect(&rc, -pObj->m_xPos, -pObj->m_yPos);
  157.                 DrawText(ps.hdc, psz, -1, &rc, DT_LEFT
  158.                     | DT_NOCLIP| DT_EXPANDTABS);
  159.  
  160.                 GlobalUnlock(pObj->m_hMemText);
  161.                 SelectObject(ps.hdc, hFont);
  162.                 }
  163.             EndPaint(hWnd, &ps);
  164.             break;
  165.  
  166.         case WM_MOUSEMOVE:
  167.              // If this message is already displayed, CStatusHelper
  168.              // will just ignore this call and return very fast.
  169.             pObj->m_pSH->MessageDisplay(ID_MSGCHOOSEOPEN);
  170.             break;
  171.  
  172.         case WM_RBUTTONDOWN:
  173.             {
  174.             HMENU           hMenu, hMenuRes;
  175.             POINT           pt;
  176.             UINT            i, cItems;
  177.  
  178.             //Load our context menu.
  179.             hMenuRes=LoadMenu(pObj->m_hInst
  180.                 , MAKEINTRESOURCE(IDR_MENUVIEWPORT));
  181.  
  182.             if (NULL==hMenuRes)
  183.                 break;
  184.  
  185.              // Make a copy popup menu because you cannot
  186.              // use a resource-loaded menu with TrackPopupMenu.
  187.             cItems=GetMenuItemCount(hMenuRes);
  188.             hMenu=CreatePopupMenu();
  189.  
  190.             for (i=0; i < cItems; i++)
  191.                 {
  192.                 char    szItem[80];
  193.                 int     id, uFlags;
  194.  
  195.                 GetMenuString(hMenuRes, i, szItem, sizeof(szItem)
  196.                     , MF_BYPOSITION);
  197.                 id=GetMenuItemID(hMenuRes, i);
  198.  
  199.                 uFlags=(0==id) ? MF_SEPARATOR : MF_STRING | MF_ENABLED;
  200.                 AppendMenu(hMenu, uFlags, id, szItem);
  201.                 }
  202.  
  203.             DestroyMenu(hMenuRes);
  204.  
  205.             pt.x=LOWORD(lParam);
  206.             pt.y=HIWORD(lParam);
  207.             ClientToScreen(hWnd, &pt);
  208.  
  209.             //Messages sent to frame window from this menu.
  210.             TrackPopupMenu(hMenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON
  211.                 , pt.x, pt.y, 0, pObj->m_hWnd, NULL);
  212.  
  213.             DestroyMenu(hMenu);
  214.             }
  215.             break;
  216.  
  217.         case WM_HSCROLL:
  218.         case WM_VSCROLL:
  219.             {
  220.             int             iTmp;
  221.             UINT            idScroll;
  222.             SCROLLINFO      si;
  223.  
  224.             idScroll=(WM_HSCROLL==iMsg) ? SB_HORZ : SB_VERT;
  225.  
  226.             si.cbSize = sizeof(SCROLLINFO);
  227.             si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE;
  228.             GetScrollInfo(hWnd, idScroll, &si);
  229.  
  230.             iTmp=si.nPos;
  231.  
  232.             switch (LOWORD(wParam))
  233.                 {
  234.                 case SB_LINEUP:     si.nPos -= 20;  break;
  235.                 case SB_PAGEUP:     si.nPos -=si.nPage;  break;
  236.                 case SB_LINEDOWN:   si.nPos += 20;  break;
  237.                 case SB_PAGEDOWN:   si.nPos +=si.nPage;  break;
  238.  
  239.                 case SB_THUMBPOSITION:
  240.                 case SB_THUMBTRACK:
  241.                     si.nPos=HIWORD(wParam);
  242.                     break;
  243.                 }
  244.  
  245.             si.nPos=max(si.nMin, min(si.nPos, si.nMax));
  246.  
  247.             if (si.nPos!=iTmp)
  248.                 {
  249.                 //Set the new position and scroll the window
  250.                 SetScrollPos(hWnd, idScroll, si.nPos, TRUE);
  251.  
  252.                 if (SB_HORZ==idScroll)
  253.                     {
  254.                     pObj->m_xPos=si.nPos;
  255.                     ScrollWindow(hWnd, iTmp-si.nPos, 0, NULL, NULL);
  256.                     }
  257.                 else
  258.                     {
  259.                     pObj->m_yPos=si.nPos;
  260.                     ScrollWindow(hWnd, 0, iTmp-si.nPos, NULL, NULL);
  261.                     }
  262.  
  263.                 UpdateWindow(hWnd);
  264.                 }
  265.             }
  266.             break;
  267.  
  268.         default:
  269.             return DefWindowProc(hWnd, iMsg, wParam, lParam);
  270.         }
  271.  
  272.     return 0L;
  273.     }
  274. //
  275. //   FUNCTION:     AboutProc
  276. //
  277. //   PURPOSE:  Dialog procedure for the omnipresent About box. 
  278. //
  279. BOOL APIENTRY AboutProc(HWND hDlg, UINT iMsg, WPARAM wParam
  280.     , LPARAM lParam)
  281.     {
  282.     switch (iMsg)
  283.         {
  284.         case WM_INITDIALOG:
  285.             return TRUE;
  286.  
  287.         case WM_COMMAND:
  288.             switch (LOWORD(wParam))
  289.                 {
  290.                 case IDOK:
  291.                     EndDialog(hDlg, TRUE);
  292.                     break;
  293.                 }
  294.             break;
  295.  
  296.         case WM_CLOSE:
  297.             EndDialog(hDlg, FALSE);
  298.             break;
  299.         }
  300.     return FALSE;
  301.     }
  302.