WordEventSink.cpp
上传用户:fda_625
上传日期:2007-01-02
资源大小:32k
文件大小:5k
源码类别:

文件操作

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "test.h"
  3. #include "testdlg.h"
  4. #include "WordEventSink.h"
  5. /*----------------------------------------------------------------------------*/
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /*----------------------------------------------------------------------------*/
  12. BEGIN_MESSAGE_MAP(CWordEventSink, CCmdTarget)
  13. //{{AFX_MSG_MAP(CWordEventSink)
  14. // NOTE - the ClassWizard will add and remove mapping macros here.
  15. //}}AFX_MSG_MAP
  16. END_MESSAGE_MAP()
  17. /*----------------------------------------------------------------------------*/
  18. BEGIN_DISPATCH_MAP(CWordEventSink, CCmdTarget)
  19. //{{AFX_DISPATCH_MAP(CWordEventSink)
  20. DISP_FUNCTION(CWordEventSink, "Startup", OnAppStartup, VT_EMPTY, VTS_NONE)
  21. DISP_FUNCTION(CWordEventSink, "Quit", OnAppQuit, VT_EMPTY, VTS_NONE)
  22. DISP_FUNCTION(CWordEventSink, "DocumentChange", OnAppDocumentChange, VT_EMPTY, VTS_NONE)
  23. DISP_FUNCTION(CWordEventSink, "New", OnDocNew, VT_EMPTY, VTS_NONE)
  24. DISP_FUNCTION(CWordEventSink, "Open", OnDocOpen, VT_EMPTY, VTS_NONE)
  25. DISP_FUNCTION(CWordEventSink, "Close", OnDocClose, VT_EMPTY, VTS_NONE)
  26. //DISP_FUNCTION_ID(CWordEventSink, "Startup", 0x01, OnAppStartup, VT_EMPTY, VTS_NONE)
  27. //DISP_FUNCTION_ID(CWordEventSink, "Quit", 0x02, OnAppQuit, VT_EMPTY, VTS_NONE)
  28. //DISP_FUNCTION_ID(CWordEventSink, "DocumentChange", 0x03, OnAppDocumentChange, VT_EMPTY, VTS_NONE)
  29. //DISP_FUNCTION_ID(CWordEventSink, "New", 0x04, OnDocNew, VT_EMPTY, VTS_NONE)
  30. //DISP_FUNCTION_ID(CWordEventSink, "Open", 0x05, OnDocOpen, VT_EMPTY, VTS_NONE)
  31. //DISP_FUNCTION_ID(CWordEventSink, "Close", 0x06, OnDocClose, VT_EMPTY, VTS_NONE)
  32.   //}}AFX_DISPATCH_MAP
  33. END_DISPATCH_MAP()
  34. /*----------------------------------------------------------------------------*/
  35. BEGIN_INTERFACE_MAP(CWordEventSink, CCmdTarget)
  36. INTERFACE_PART(CWordEventSink, IID_IWordAppEventSink, Dispatch)
  37. INTERFACE_PART(CWordEventSink, IID_IWordDocEventSink, Dispatch)
  38. END_INTERFACE_MAP()
  39. /*----------------------------------------------------------------------------*/
  40. IMPLEMENT_DYNCREATE(CWordEventSink, CCmdTarget)
  41. /*----------------------------------------------------------------------------*/
  42. CWordEventSink::CWordEventSink() :
  43. m_AppEventsAdvisor(IID_IWordAppEventSink), 
  44. m_DocEventsAdvisor(IID_IWordDocEventSink)
  45. {
  46. m_pWordLauncher = NULL;
  47. EnableAutomation();
  48. }
  49. /*----------------------------------------------------------------------------*/
  50. CWordEventSink::~CWordEventSink()
  51. {
  52. }
  53. /*----------------------------------------------------------------------------*/
  54. void CWordEventSink::OnFinalRelease()
  55. {
  56. // When the last reference for an automation object is released
  57. // OnFinalRelease is called.  The base class will automatically
  58. // deletes the object.  Add additional cleanup required for your
  59. // object before calling the base class.
  60. CCmdTarget::OnFinalRelease();
  61. }
  62. /*----------------------------------------------------------------------------*/
  63. void CWordEventSink::OnAppStartup() 
  64. {
  65. // You will never receive this event 
  66. AfxMessageBox("Quit event received");
  67. }
  68. /*----------------------------------------------------------------------------*/
  69. void CWordEventSink::OnAppQuit() 
  70. {
  71. AfxMessageBox("AppQuit event received");
  72. AfxGetMainWnd()->PostMessage(WM_COMMAND, IDCANCEL, 0L);
  73. }
  74. /*----------------------------------------------------------------------------*/
  75. void CWordEventSink::OnAppDocumentChange() 
  76. {
  77. AfxMessageBox("AppDocumentChange event received");
  78. }
  79. /*----------------------------------------------------------------------------*/
  80. void CWordEventSink::OnDocNew() 
  81. {
  82. AfxMessageBox("DocNew event received");
  83. }
  84. /*----------------------------------------------------------------------------*/
  85. void CWordEventSink::OnDocOpen() 
  86. {
  87. AfxMessageBox("DocOpen event received");
  88. }
  89. /*----------------------------------------------------------------------------*/
  90. void CWordEventSink::OnDocClose() 
  91. {
  92. m_pWordLauncher->OnDocClose();
  93. }
  94. /*----------------------------------------------------------------------------*/
  95. BOOL CWordEventSink::Advise(IUnknown* pSource, REFIID iid)
  96. {
  97. // This GetInterface does not AddRef
  98. IUnknown* pUnknownSink = GetInterface(&IID_IUnknown);
  99. if (pUnknownSink == NULL)
  100. {
  101. return FALSE;
  102. }
  103. if (iid == IID_IWordAppEventSink)
  104. {
  105. return m_AppEventsAdvisor.Advise(pUnknownSink, pSource);
  106. }
  107. else if (iid == IID_IWordDocEventSink)
  108. {
  109. return m_DocEventsAdvisor.Advise(pUnknownSink, pSource);
  110. }
  111. else 
  112. {
  113. return FALSE;
  114. }
  115. }
  116. /*----------------------------------------------------------------------------*/
  117. BOOL CWordEventSink::Unadvise(REFIID iid)
  118. {
  119. if (iid == IID_IWordAppEventSink)
  120. {
  121. return m_AppEventsAdvisor.Unadvise();
  122. }
  123. else if (iid == IID_IWordDocEventSink)
  124. {
  125. return m_DocEventsAdvisor.Unadvise();
  126. }
  127. else 
  128. {
  129. return FALSE;
  130. }
  131. }
  132. /*----------------------------------------------------------------------------*/
  133. void CWordEventSink::SetLauncher(CTestDlg* pWordLauncher)
  134. {
  135. m_pWordLauncher = pWordLauncher;
  136. }