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

Windows编程

开发平台:

Visual C++

  1. /*
  2.  * PATRON.CPP
  3.  * Patron Chapter 1
  4.  *
  5.  * Frame window class for Patron.
  6.  *
  7.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Microsoft
  10.  * Internet  :  kraigb@microsoft.com
  11.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  12.  */
  13. #include "patron.h"
  14. /*
  15.  * WinMain
  16.  *
  17.  * Purpose:
  18.  *  Main entry point of application.  Should register the app class
  19.  *  if a previous instance has not done so and do any other one-time
  20.  *  initializations.
  21.  */
  22. int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev
  23.     , LPSTR pszCmdLine, int nCmdShow)
  24.     {
  25.     PCPatronFrame   pFR;
  26.     FRAMEINIT       fi;
  27.     WPARAM          wRet=0;
  28.     //Attempt to allocate and initialize the application
  29.     pFR=new CPatronFrame(hInst, hPrev, pszCmdLine, nCmdShow);
  30.     if (NULL==pFR)
  31.         return -1;
  32.     fi.idsMin=IDS_FRAMEMIN;
  33.     fi.idsMax=IDS_FRAMEMAX;
  34.     fi.idsStatMin=IDS_STATMESSAGEMIN;
  35.     fi.idsStatMax=IDS_STATMESSAGEMAX;
  36.     fi.idStatMenuMin=ID_MENUFILE;
  37.     fi.idStatMenuMax=ID_MENUHELP;
  38.     fi.iPosWindowMenu=WINDOW_MENU;
  39.     fi.cMenus=CMENUS;
  40.     fi.x=CW_USEDEFAULT;
  41.     fi.y=CW_USEDEFAULT;
  42.     fi.cx=CW_USEDEFAULT;
  43.     fi.cy=CW_USEDEFAULT;
  44.     //If we can initialize pFR, start chugging messages
  45.     if (pFR->Init(&fi))
  46.         wRet=pFR->MessageLoop();
  47.     delete pFR;
  48.     return wRet;
  49.     }
  50. /*
  51.  * CPatronFrame::CPatronFrame
  52.  * CPatronFrame::~CPatronFrame
  53.  *
  54.  * Constructor Parameters:
  55.  *  hInst           HINSTANCE from WinMain
  56.  *  hInstPrev       HINSTANCE from WinMain
  57.  *  pszCmdLine      LPSTR from WinMain
  58.  *  nCmdShow        int from WInMain
  59.  */
  60. CPatronFrame::CPatronFrame(HINSTANCE hInst, HINSTANCE hInstPrev
  61.     , LPSTR pszCmdLine, int nCmdShow)
  62.     : CFrame(hInst, hInstPrev, pszCmdLine, nCmdShow)
  63.     {
  64.     return;
  65.     }
  66. CPatronFrame::~CPatronFrame(void)
  67.     {
  68.     return;
  69.     }
  70. /*
  71.  * CPatronFrame::CreateCClient
  72.  *
  73.  * Purpose:
  74.  *  Constructs a new client specific to the application.
  75.  *
  76.  * Parameters:
  77.  *  None
  78.  *
  79.  * Return Value:
  80.  *  PCClient        Pointer to the new client object.
  81.  */
  82. PCClient CPatronFrame::CreateCClient(void)
  83.     {
  84.     return (PCClient)(new CPatronClient(m_hInst, this));
  85.     }
  86. /*
  87.  * CPatronFrame::RegisterAllClasses
  88.  *
  89.  * Purpose:
  90.  *  Registers all classes used in this application.
  91.  *
  92.  * Parameters:
  93.  *  None
  94.  *
  95.  * Return Value:
  96.  *  BOOL            TRUE if registration succeeded, FALSE otherwise.
  97.  */
  98. BOOL CPatronFrame::RegisterAllClasses(void)
  99.     {
  100.     WNDCLASS        wc;
  101.     //First let the standard frame do its thing
  102.     if (!CFrame::RegisterAllClasses())
  103.         return FALSE;
  104.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  105.     wc.hInstance     = m_hInst;
  106.     wc.cbClsExtra    = 0;
  107.     wc.lpfnWndProc   = PagesWndProc;
  108.     wc.cbWndExtra    = CBPAGESWNDEXTRA;
  109.     wc.hIcon         = NULL;
  110.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  111.     wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1);
  112.     wc.lpszMenuName  = NULL;
  113.     wc.lpszClassName = SZCLASSPAGES;
  114.     if (!RegisterClass(&wc))
  115.         return FALSE;
  116.     return TRUE;
  117.     }
  118. /*
  119.  * CPatronFrame::OnCommand
  120.  *
  121.  * Purpose:
  122.  *  WM_COMMAND handler for the Patron frame window that processes
  123.  *  extra File menu items as well as the Page menu.
  124.  *
  125.  * Parameters:
  126.  *  hWnd            HWND of the frame window.
  127.  *  wParam          WPARAM of the message.
  128.  *  lParam          LPARAM of the message.
  129.  *
  130.  * Return Value:
  131.  *  LRESULT         Return value for the message.
  132.  */
  133. LRESULT CPatronFrame::OnCommand(HWND hWnd, WPARAM wParam
  134.     , LPARAM lParam)
  135.     {
  136.     PCPatronDoc     pDoc;
  137.     COMMANDPARAMS(wID, wCode, hWndMsg);
  138.     /*
  139.      * Don't bother with anything during first initialization,
  140.      * skipping many toolbar notifications.
  141.      */
  142.     if (m_fInit)
  143.         return 0L;
  144.     pDoc=(PCPatronDoc)m_pCL->ActiveDocument();
  145.     switch (wID)
  146.         {
  147.         case IDM_FILEPRINT:
  148.             pDoc->Print(m_hWnd);
  149.             return 0L;
  150.         case IDM_FILEPRINTERSETUP:
  151.             pDoc->PrinterSetup(m_hWnd, FALSE);
  152.             return 0L;
  153.         case IDM_PAGENEWPAGE:
  154.             pDoc->NewPage();
  155.             break;
  156.         case IDM_PAGEDELETEPAGE:
  157.             pDoc->DeletePage();
  158.             break;
  159.         case IDM_PAGENEXTPAGE:
  160.             pDoc->NextPage();
  161.             break;
  162.         case IDM_PAGEPREVIOUSPAGE:
  163.             pDoc->PreviousPage();
  164.             break;
  165.         case IDM_PAGEFIRSTPAGE:
  166.             pDoc->FirstPage();
  167.             break;
  168.         case IDM_PAGELASTPAGE:
  169.             pDoc->LastPage();
  170.             break;
  171.         default:
  172.            return CFrame::OnCommand(hWnd, wParam, lParam);
  173.         }
  174.     return 0L;
  175.     }
  176. /*
  177.  * CPatronFrame::CreateToolbar
  178.  *
  179.  * Purpose:
  180.  *  Procedure to create all the necessary toolbar buttons.
  181.  *
  182.  * Parameters:
  183.  *  None
  184.  *
  185.  * Return Value:
  186.  *  UINT            Number of tools added to the bar.
  187.  */
  188. UINT CPatronFrame::CreateToolbar(void)
  189.     {
  190.     UINT            iLast;
  191.     UINT            uState=GIZMO_NORMAL;
  192.     UINT            utCmd =GIZMOTYPE_BUTTONCOMMAND;
  193.     //Insert the standard ones.
  194.     iLast=CFrame::CreateToolbar();
  195.     //Remove Undo:  we don't use it.
  196.     m_pTB->Remove(IDM_EDITUNDO);
  197.     /*
  198.      * Insert Print File Import in the 5th position and account
  199.      * for it in iLast.
  200.      */
  201.     m_pTB->Add(utCmd, 4, IDM_FILEPRINT, m_dxB, m_dyB
  202.         , NULL, NULL, 6, uState);
  203.     iLast++;
  204.     m_pTB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB
  205.         , NULL, NULL, 0, uState);
  206.     //Add New Page, and Delete Page
  207.     m_pTB->Add(utCmd, iLast++, IDM_PAGENEWPAGE, m_dxB, m_dyB
  208.         , NULL, m_hBmp, 2, uState);
  209.     m_pTB->Add(utCmd, iLast++, IDM_PAGEDELETEPAGE, m_dxB, m_dyB
  210.         , NULL, m_hBmp, 3, uState);
  211.     m_pTB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB
  212.         , NULL, NULL, 0, uState);
  213.     //First, Prev, Next, Last pages.
  214.     m_pTB->Add(utCmd, iLast++, IDM_PAGEFIRSTPAGE, m_dxB, m_dyB
  215.         , NULL, m_hBmp, 4, uState);
  216.     m_pTB->Add(utCmd, iLast++, IDM_PAGEPREVIOUSPAGE, m_dxB, m_dyB
  217.         , NULL, m_hBmp, 5, uState);
  218.     m_pTB->Add(utCmd, iLast++, IDM_PAGENEXTPAGE, m_dxB, m_dyB
  219.         , NULL, m_hBmp, 6, uState);
  220.     m_pTB->Add(utCmd, iLast++, IDM_PAGELASTPAGE, m_dxB, m_dyB
  221.         , NULL, m_hBmp, 7, uState);
  222.     return iLast;
  223.     }
  224. /*
  225.  * CPatronFrame::UpdateMenus
  226.  *
  227.  * Purpose:
  228.  *  Handles the WM_INITMENU message for the frame window.  Depending
  229.  *  on the existence of an active window, menu items are selectively
  230.  *  enabled and disabled.
  231.  *
  232.  * Parameters:
  233.  *  hMenu           HMENU of the menu to intialize
  234.  *  iMenu           UINT position of the menu.
  235.  *
  236.  * Return Value:
  237.  *  None
  238.  */
  239. void CPatronFrame::UpdateMenus(HMENU hMenu, UINT iMenu)
  240.     {
  241.     PCPatronDoc     pDoc;
  242.     BOOL            fOK=FALSE;
  243.     BOOL            fCallDefault=TRUE;
  244.     UINT            uTemp;
  245.     UINT            uTempE;
  246.     UINT            uTempD;
  247.     pDoc=(PCPatronDoc)m_pCL->ActiveDocument();
  248.     uTempE=MF_ENABLED | MF_BYCOMMAND;
  249.     uTempD=MF_DISABLED | MF_GRAYED | MF_BYCOMMAND;
  250.     uTemp=((NULL!=pDoc) ? uTempE : uTempD);
  251.     //File menu
  252.     if (m_phMenu[0]==hMenu)
  253.         {
  254.         EnableMenuItem(hMenu, IDM_FILEPRINT, uTemp);
  255.         EnableMenuItem(hMenu, IDM_FILEPRINTERSETUP, uTemp);
  256.         }
  257.     //Page menu
  258.     if (m_phMenu[2]==hMenu)
  259.         {
  260.         EnableMenuItem(hMenu, IDM_PAGENEWPAGE,      uTemp);
  261.         EnableMenuItem(hMenu, IDM_PAGEDELETEPAGE,   uTemp);
  262.         EnableMenuItem(hMenu, IDM_PAGENEXTPAGE,     uTemp);
  263.         EnableMenuItem(hMenu, IDM_PAGEPREVIOUSPAGE, uTemp);
  264.         EnableMenuItem(hMenu, IDM_PAGEFIRSTPAGE,    uTemp);
  265.         EnableMenuItem(hMenu, IDM_PAGELASTPAGE,     uTemp);
  266.         }
  267.     if (fCallDefault)
  268.         CFrame::UpdateMenus(hMenu, iMenu);
  269.     return;
  270.     }
  271. /*
  272.  * CPatronFrame::UpdateToolbar
  273.  *
  274.  * Purpose:
  275.  *  Enables and disables tools depending on whether we have
  276.  *  a document or not.
  277.  *
  278.  * Parameters:
  279.  *  None
  280.  *
  281.  * Return Value:
  282.  *  None
  283.  */
  284. void CPatronFrame::UpdateToolbar(void)
  285.     {
  286.     PCDocument  pDoc;
  287.     BOOL        fEnable;
  288.     //Let the default hack on its tools.
  289.     CFrame::UpdateToolbar();
  290.     pDoc=m_pCL->ActiveDocument();
  291.     fEnable=(NULL!=pDoc);
  292.     //No document, disable just about everything
  293.     m_pTB->Enable(IDM_FILEPRINT,        fEnable);
  294.     m_pTB->Enable(IDM_FILEPRINTERSETUP, fEnable);
  295.     m_pTB->Enable(IDM_PAGENEWPAGE,      fEnable);
  296.     m_pTB->Enable(IDM_PAGEDELETEPAGE,   fEnable);
  297.     m_pTB->Enable(IDM_PAGEFIRSTPAGE,    fEnable);
  298.     m_pTB->Enable(IDM_PAGEPREVIOUSPAGE, fEnable);
  299.     m_pTB->Enable(IDM_PAGENEXTPAGE,     fEnable);
  300.     m_pTB->Enable(IDM_PAGELASTPAGE,     fEnable);
  301.     return;
  302.     }