FVPROC.CPP
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:9k
源码类别:

Windows编程

开发平台:

Visual C++

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