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

Windows编程

开发平台:

Visual C++

  1. //**********************************************************************
  2. // File name: app.cpp
  3. //
  4. //    Implementation file for the CSimpleApp Class
  5. //
  6. // Functions:
  7. //
  8. //    See app.h for a list of member functions.
  9. //
  10. // Copyright (c) 1992 - 1997 Microsoft Corporation. All rights reserved.
  11. //**********************************************************************
  12. #include "pre.h"
  13. #include "iocs.h"
  14. #include "ias.h"
  15. #include "app.h"
  16. #include "site.h"
  17. #include "doc.h"
  18. //**********************************************************************
  19. //
  20. // CSimpleApp::CSimpleApp()
  21. //
  22. // Purpose:
  23. //
  24. //      Constructor for CSimpleApp
  25. //
  26. // Parameters:
  27. //
  28. //      None
  29. //
  30. // Return Value:
  31. //
  32. //      None
  33. //
  34. // Function Calls:
  35. //      Function                    Location
  36. //
  37. //      OutputDebugString           Windows API
  38. //      SetRectEmpty                Windows API
  39. //
  40. // Comments:
  41. //
  42. //********************************************************************
  43. CSimpleApp::CSimpleApp()
  44. {
  45.         OutputDebugString("In CSimpleApp's Constructor rn");
  46.         // clear members
  47.         m_hAppWnd = NULL;
  48.         m_hInst = NULL;
  49.         m_lpDoc = NULL;
  50.         // clear flags
  51.         m_fInitialized = FALSE;
  52.         m_fOleStdInit = FALSE;
  53. }
  54. //**********************************************************************
  55. //
  56. // CSimpleApp::~CSimpleApp()
  57. //
  58. // Purpose:
  59. //
  60. //      Destructor for CSimpleApp Class.
  61. //
  62. // Parameters:
  63. //
  64. //      None
  65. //
  66. // Return Value:
  67. //
  68. //      None
  69. //
  70. // Function Calls:
  71. //      Function                    Location
  72. //
  73. //      OutputDebugString           Windows API
  74. //      OleUninitialize             OLE API
  75. //
  76. // Comments:
  77. //
  78. //********************************************************************
  79. CSimpleApp::~CSimpleApp()
  80. {
  81.         OutputDebugString("In CSimpleApp's Destructorrn");
  82.         // need to uninit the library...
  83.         if (m_fOleStdInit)
  84.                 OleStdUninitialize();
  85.         if (m_fInitialized)
  86.                 OleUninitialize();
  87. }
  88. //**********************************************************************
  89. //
  90. // CSimpleApp::DestroyDocs()
  91. //
  92. // Purpose:
  93. //
  94. //      Destroys all of the open documents in the application (Only one
  95. //      since this is an SDI app, but could easily be modified to
  96. //      support MDI).
  97. //
  98. // Parameters:
  99. //
  100. //      None
  101. //
  102. // Return Value:
  103. //
  104. //      None
  105. //
  106. // Function Calls:
  107. //      Function                    Location
  108. //
  109. // Comments:
  110. //
  111. //********************************************************************
  112. void CSimpleApp::DestroyDocs()
  113. {
  114.         CStabilize stabilize(this);
  115.         m_lpDoc->Close();   // we have only 1 document
  116. }
  117. //**********************************************************************
  118. //
  119. // CSimpleApp::QueryInterface
  120. //
  121. // Purpose:
  122. //
  123. //      Used for interface negotiation at the Frame level.
  124. //
  125. // Parameters:
  126. //
  127. //      REFIID riid         -   A reference to the interface that is
  128. //                              being queried.
  129. //
  130. //      LPVOID FAR* ppvObj  -   An out parameter to return a pointer to
  131. //                              the interface.
  132. //
  133. // Return Value:
  134. //
  135. //      S_OK    -   The interface is supported.
  136. //      S_FALSE -   The interface is not supported
  137. //
  138. // Function Calls:
  139. //      Function                    Location
  140. //
  141. //      OutputDebugString           Windows API
  142. //                   OLE API
  143. //
  144. // Comments:
  145. //
  146. //********************************************************************
  147. STDMETHODIMP CSimpleApp::QueryInterface(REFIID riid, LPVOID FAR* ppvObj)
  148. {
  149.         OutputDebugString("In CSimpleApp::QueryInterfacern");
  150.         *ppvObj = NULL;     // must set out pointer parameters to NULL
  151.         // Not a supported interface
  152.         return E_NOINTERFACE;
  153. }
  154. //**********************************************************************
  155. //
  156. // CSimpleApp::AddRef
  157. //
  158. // Purpose:
  159. //
  160. //      Adds to the reference count at the Application level.
  161. //
  162. // Parameters:
  163. //
  164. //      None
  165. //
  166. // Return Value:
  167. //
  168. //      ULONG   -   The new reference count of the application.
  169. //
  170. // Function Calls:
  171. //      Function                    Location
  172. //
  173. //      OutputDebugString           Windows API
  174. //
  175. // Comments:
  176. //
  177. //      Due to the reference counting model that is used in this
  178. //      implementation, this reference count is the sum of the
  179. //      reference counts on all interfaces of all objects open
  180. //      in the application.
  181. //
  182. //********************************************************************
  183. STDMETHODIMP_(ULONG) CSimpleApp::AddRef()
  184. {
  185.         OutputDebugString("In CSimpleApp::AddRefrn");
  186.         return SafeAddRef();
  187. }
  188. //**********************************************************************
  189. //
  190. // CSimpleApp::Release
  191. //
  192. // Purpose:
  193. //
  194. //      Decrements the reference count at this level
  195. //
  196. // Parameters:
  197. //
  198. //      None
  199. //
  200. // Return Value:
  201. //
  202. //      ULONG   -   The new reference count of the application.
  203. //
  204. // Function Calls:
  205. //      Function                    Location
  206. //
  207. //      OutputDebugString           Windows API
  208. //
  209. // Comments:
  210. //
  211. //********************************************************************
  212. STDMETHODIMP_(ULONG) CSimpleApp::Release()
  213. {
  214.         OutputDebugString("In CSimpleApp::Releasern");
  215.         return SafeRelease();
  216. }
  217. //**********************************************************************
  218. //
  219. // CSimpleApp::fInitApplication
  220. //
  221. // Purpose:
  222. //
  223. //      Initializes the application
  224. //
  225. // Parameters:
  226. //
  227. //      HANDLE hInstance    -   Instance handle of the application.
  228. //
  229. // Return Value:
  230. //
  231. //      TRUE    -   Application was successfully initialized.
  232. //      FALSE   -   Application could not be initialized
  233. //
  234. // Function Calls:
  235. //      Function                    Location
  236. //
  237. //      LoadIcon                    Windows API
  238. //      LoadCursor                  Windows API
  239. //      GetStockObject              Windows API
  240. //      RegisterClass               Windows API
  241. //
  242. // Comments:
  243. //
  244. //********************************************************************
  245. BOOL CSimpleApp::fInitApplication(HANDLE hInstance)
  246. {
  247.         WNDCLASS  wc;
  248.         CStabilize stabilize(this);
  249.         // Fill in window class structure with parameters that describe the
  250.         // main window.
  251.         wc.style = NULL;                    // Class style(s).
  252.         wc.lpfnWndProc = MainWndProc;       // Function to retrieve messages for
  253.                                                                                 // windows of this class.
  254.         wc.cbClsExtra = 0;                  // No per-class extra data.
  255.         wc.cbWndExtra = 0;                  // No per-window extra data.
  256.         wc.hInstance = (HINSTANCE) hInstance; // Application that owns the class.
  257.         wc.hIcon = LoadIcon((HINSTANCE) hInstance,"SimpDnd");
  258.         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  259.         wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  260.         wc.lpszMenuName =  "SIMPLEMENU";    // Name of menu resource in .RC file.
  261.         wc.lpszClassName = "SimpDndAppWClass";  // Name used in CreateWindow call.
  262.         if (!RegisterClass(&wc))
  263.                 return FALSE;
  264.         wc.style = CS_DBLCLKS;              // Class style(s). allow DBLCLK's
  265.         wc.lpfnWndProc = DocWndProc;        // Function to retrieve messages for
  266.                                                                                 // windows of this class.
  267.         wc.cbClsExtra = 0;                  // No per-class extra data.
  268.         wc.cbWndExtra = 0;                  // No per-window extra data.
  269.         wc.hInstance = (HINSTANCE) hInstance; // Application that owns the class.
  270.         wc.hIcon = NULL;
  271.         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  272.         wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  273.         wc.lpszMenuName =  NULL;
  274.         wc.lpszClassName = "SimpDndDocWClass"; // Name used in CreateWindow call.
  275.         // Register the window class and return success/failure code.
  276.         return (RegisterClass(&wc));
  277. }
  278. //**********************************************************************
  279. //
  280. // CSimpleApp::fInitInstance
  281. //
  282. // Purpose:
  283. //
  284. //      Instance initialization.
  285. //
  286. // Parameters:
  287. //
  288. //      HANDLE hInstance    -   App. Instance Handle.
  289. //
  290. //      int nCmdShow        -   Show parameter from WinMain
  291. //
  292. // Return Value:
  293. //
  294. //      TRUE    -   Initialization Successful
  295. //      FALSE   -   Initialization Failed.
  296. //
  297. //
  298. // Function Calls:
  299. //      Function                    Location
  300. //
  301. //      CreateWindow                Windows API
  302. //      ShowWindow                  Windows API
  303. //      UpdateWindow                Windows API
  304. //      OleBuildVersion             OLE API
  305. //      OleInitialize               OLE API
  306. //
  307. // Comments:
  308. //
  309. //      Note that successful Initalization of the OLE libraries
  310. //      is remembered so the UnInit is only called if needed.
  311. //
  312. //********************************************************************
  313. BOOL CSimpleApp::fInitInstance (HANDLE hInstance, int nCmdShow)
  314. {
  315.         CStabilize stabilize(this);
  316.         DWORD dwVer = OleBuildVersion();
  317.         LPMALLOC lpMalloc = NULL;
  318.         // check to see if we are compatible with this version of the libraries
  319.         if (HIWORD(dwVer) != rmm || LOWORD(dwVer) < rup) {
  320. #ifdef _DEBUG
  321.                 OutputDebugString("WARNING: Incompatible OLE library versionrn");
  322. #else
  323.                 return FALSE;
  324. #endif
  325.         }
  326.         if (SUCCEEDED(OleInitialize(NULL)))
  327.         {
  328.             m_fInitialized = TRUE;
  329.         }
  330.         m_fOleStdInit = OleStdInitialize((HINSTANCE) hInstance);
  331.         OleDbgSetDbgLevel(10);
  332.         m_hInst = (HINSTANCE) hInstance;
  333.         // Create the "application" windows
  334.         m_hAppWnd = CreateWindow ("SimpDndAppWClass",
  335.                                                           "Simple OLE 2.0 Drag/Drop Container",
  336.                                                           WS_OVERLAPPEDWINDOW,
  337.                                                           CW_USEDEFAULT,
  338.                                                           CW_USEDEFAULT,
  339.                                                           CW_USEDEFAULT,
  340.                                                           CW_USEDEFAULT,
  341.                                                           NULL,
  342.                                                           NULL,
  343.                                                           (HINSTANCE) hInstance,
  344.                                                           NULL);
  345.         if (!m_hAppWnd)
  346.                 return FALSE;
  347.         // delay before dragging should start, in milliseconds
  348.         m_nDragDelay = GetProfileInt(
  349.                         "windows",
  350.                         "DragDelay",
  351.                         DD_DEFDRAGDELAY
  352.         );
  353.         // minimum distance (radius) before drag should start, in pixels
  354.         m_nDragMinDist = GetProfileInt(
  355.                         "windows",
  356.                         "DragMinDist",
  357.                         DD_DEFDRAGMINDIST
  358.         );
  359.         // delay before scrolling, in milliseconds
  360.         m_nScrollDelay = GetProfileInt(
  361.                         "windows",
  362.                         "DragScrollDelay",
  363.                         DD_DEFSCROLLDELAY
  364.         );
  365.         // inset-width of the hot zone, in pixels
  366.         m_nScrollInset = GetProfileInt(
  367.                         "windows",
  368.                         "DragScrollInset",
  369.                         DD_DEFSCROLLINSET
  370.         );
  371.         // scroll interval, in milliseconds
  372.         m_nScrollInterval = GetProfileInt(
  373.                         "windows",
  374.                         "DragScrollInterval",
  375.                         DD_DEFSCROLLINTERVAL
  376.         );
  377.         ShowWindow (m_hAppWnd, nCmdShow);
  378.         UpdateWindow (m_hAppWnd);
  379.         return m_fInitialized && m_fOleStdInit;
  380. }
  381. //**********************************************************************
  382. //
  383. // CSimpleApp::lCommandHandler
  384. //
  385. // Purpose:
  386. //
  387. //      Handles the processing of WM_COMMAND.
  388. //
  389. // Parameters:
  390. //
  391. //      HWND hWnd       -   Handle to the application Window
  392. //
  393. //      UINT message    -   message (always WM_COMMAND)
  394. //
  395. //      WPARAM wParam   -   Same as passed to the WndProc
  396. //
  397. //      LPARAM lParam   -   Same as passed to the WndProc
  398. //
  399. // Return Value:
  400. //
  401. //      NULL
  402. //
  403. // Function Calls:
  404. //      Function                                    Location
  405. //
  406. //      IOleObject::DoVerb                          Object
  407. //      GetClientRect                               Windows API
  408. //      MessageBox                                  Windows API
  409. //      DialogBox                                   Windows API
  410. //      MakeProcInstance                            Windows API
  411. //      FreeProcInstance                            Windows API
  412. //      SendMessage                                 Windows API
  413. //      DefWindowProc                               Windows API
  414. //      CSimpleDoc::InsertObject                    DOC.CPP
  415. //
  416. // Comments:
  417. //
  418. //********************************************************************
  419. long CSimpleApp::lCommandHandler (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  420. {
  421.         RECT rect;
  422.         CStabilize stabilize(this);
  423.         // see if the command is a verb selections
  424.         //@@WTK WIN32, UNICODE
  425.         //if (wParam >= IDM_VERB0)
  426.         if (LOWORD(wParam) >= IDM_VERB0)
  427.                 {
  428.                 // get the rectangle of the object
  429.                 m_lpDoc->m_lpSite->GetObjRect(&rect);
  430.                 m_lpDoc->m_lpSite->m_lpOleObject->DoVerb(
  431.                                 LOWORD(wParam) - IDM_VERB0, NULL,
  432.                                 &m_lpDoc->m_lpSite->m_OleClientSite, -1,
  433.                                 m_lpDoc->m_hDocWnd, &rect);
  434.                 }
  435.         else
  436.                 {
  437.                 //@@WTK WIN32, UNICODE
  438.                 //switch (wParam) {
  439.                 switch (LOWORD(wParam)) {
  440.                         // bring up the About box
  441.                         case IDM_ABOUT:
  442.                                 {
  443.                                 DialogBox(m_hInst,               // current instance
  444.                                         "AboutBox",                  // resource to use
  445.                                         m_hAppWnd,                   // parent handle
  446.                                         (DLGPROC) About);                // About() instance address
  447.                                 break;
  448.                                 }
  449.                         // bring up the InsertObject Dialog
  450.                         case IDM_INSERTOBJECT:
  451.                                 m_lpDoc->InsertObject();
  452.                                 break;
  453.                         // Copy the object to the Clipboard
  454.                         case IDM_COPY:
  455.                                 m_lpDoc->CopyObjectToClip();
  456.                                 break;
  457.                         // exit the application
  458.                         case IDM_EXIT:
  459.                                 SendMessage(hWnd, WM_SYSCOMMAND, SC_CLOSE, 0L);
  460.                                 break;
  461.                         case IDM_NEW:
  462.                                 m_lpDoc->Close();
  463.                                 m_lpDoc = NULL;
  464.                                 lCreateDoc(hWnd, 0, 0, 0);
  465.                                 break;
  466.                         default:
  467.                                 return (DefWindowProc(hWnd, message, wParam, lParam));
  468.                         }   // end of switch
  469.                 }  // end of else
  470.         return NULL;
  471. }
  472. //**********************************************************************
  473. //
  474. // CSimpleApp::lSizeHandler
  475. //
  476. // Purpose:
  477. //
  478. //      Handles the WM_SIZE message
  479. //
  480. // Parameters:
  481. //
  482. //      HWND hWnd       -   Handle to the application Window
  483. //
  484. //      UINT message    -   message (always WM_SIZE)
  485. //
  486. //      WPARAM wParam   -   Same as passed to the WndProc
  487. //
  488. //      LPARAM lParam   -   Same as passed to the WndProc
  489. //
  490. // Return Value:
  491. //
  492. //      LONG    -   returned from the "document" resizing
  493. //
  494. // Function Calls:
  495. //      Function                    Location
  496. //
  497. //      GetClientRect               Windows API
  498. //      CSimpleDoc::lResizeDoc      DOC.CPP
  499. //
  500. // Comments:
  501. //
  502. //********************************************************************
  503. long CSimpleApp::lSizeHandler (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  504. {
  505.         RECT rect;
  506.         CStabilize stabilize(this);
  507.         GetClientRect(m_hAppWnd, &rect);
  508.         return m_lpDoc->lResizeDoc(&rect);
  509. }
  510. //**********************************************************************
  511. //
  512. // CSimpleApp::lCreateDoc
  513. //
  514. // Purpose:
  515. //
  516. //      Handles the creation of a document.
  517. //
  518. // Parameters:
  519. //
  520. //      HWND hWnd       -   Handle to the application Window
  521. //
  522. //      UINT message    -   message (always WM_CREATE)
  523. //
  524. //      WPARAM wParam   -   Same as passed to the WndProc
  525. //
  526. //      LPARAM lParam   -   Same as passed to the WndProc
  527. //
  528. // Return Value:
  529. //
  530. //      NULL
  531. //
  532. // Function Calls:
  533. //      Function                    Location
  534. //
  535. //      GetClientRect               Windows API
  536. //      CSimpleDoc::CSimpleDoc      DOC.CPP
  537. //
  538. // Comments:
  539. //
  540. //********************************************************************
  541. long CSimpleApp::lCreateDoc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  542. {
  543.         RECT rect;
  544.         CStabilize stabilize(this);
  545.         GetClientRect(hWnd, &rect);
  546.         m_lpDoc = CSimpleDoc::Create(this, &rect, hWnd);
  547.         return NULL;
  548. }
  549. //**********************************************************************
  550. //
  551. // CSimpleApp::HandleAccelerators
  552. //
  553. // Purpose:
  554. //
  555. //      To properly handle accelerators in the Message Loop
  556. //
  557. // Parameters:
  558. //
  559. //      LPMSG lpMsg -   A pointer to the message structure.
  560. //
  561. // Return Value:
  562. //
  563. //      TRUE    -   The accelerator was handled
  564. //      FALSE   -   The accelerator was not handled
  565. //
  566. // Function Calls:
  567. //      Function                                        Location
  568. //
  569. // Comments:
  570. //
  571. //********************************************************************
  572. BOOL CSimpleApp::HandleAccelerators(LPMSG lpMsg)
  573. {
  574.         BOOL retval = FALSE;
  575.         // we do not have any accelerators
  576.         return retval;
  577. }
  578. //**********************************************************************
  579. //
  580. // CSimpleApp::PaintApp
  581. //
  582. // Purpose:
  583. //
  584. //      Handles the painting of the doc window.
  585. //
  586. //
  587. // Parameters:
  588. //
  589. //      HDC hDC -   hDC to the Doc Window.
  590. //
  591. // Return Value:
  592. //
  593. //      None
  594. //
  595. // Function Calls:
  596. //      Function                    Location
  597. //
  598. //      CSimpleDoc::PaintDoc        DOC.CPP
  599. //
  600. // Comments:
  601. //
  602. //      This is an app level function in case we want to do palette
  603. //      management.
  604. //
  605. //********************************************************************
  606. void CSimpleApp::PaintApp (HDC hDC)
  607. {
  608.         CStabilize stabilize(this);
  609.         // at this level, we could enumerate through all of the
  610.         // visible objects in the application, so that a palette
  611.         // that best fits all of the objects can be built.
  612.         // This app is designed to take on the same palette
  613.         // functionality that was provided in OLE 1.0, the palette
  614.         // of the last object drawn is realized.  Since we only
  615.         // support one object at a time, it shouldn't be a big
  616.         // deal.
  617.         // if we supported multiple documents, we would enumerate
  618.         // through each of the open documents and call paint.
  619.         if (m_lpDoc)
  620.                 m_lpDoc->PaintDoc(hDC);
  621. }