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

Windows编程

开发平台:

Visual C++

  1. // CmdWnd.cpp : Implementation of CCmdWnd
  2. #include "stdafx.h"
  3. #include "Cmdstub.h"
  4. #include "CmdWnd.h"
  5. /////////////////////////////////////////////////////////////////////////////
  6. // CCmdWnd
  7. CCmdWnd::CCmdWnd()
  8. {
  9. m_dwCookie = NULL;
  10. }
  11. CCmdWnd::~CCmdWnd()
  12. {
  13. if (m_pCommandWindow)
  14. {
  15. m_pCommandWindow.Release();
  16. m_pCommandWindow = NULL;
  17. }
  18. }
  19. HRESULT CCmdWnd::OnConnection(IApplication* pApp, VARIANT_BOOL bFirstTime, long dwAddInID, VARIANT_BOOL* bOnConnection)
  20. {
  21. HRESULT hr = S_OK;
  22. m_spApplication = pApp;
  23. m_dwAddInID = dwAddInID;
  24. // Connect up to application event sink
  25. AtlAdvise(pApp, GetUnknown(), IID_IApplicationEvents, &m_dwAppEvents);
  26. // Connect up to debugger event sink
  27. CComPtr<IDispatch> pDebugger;
  28. hr = m_spApplication->get_Debugger(&pDebugger);
  29. if (SUCCEEDED(hr))
  30. AtlAdvise(pDebugger, GetUnknown(), IID_IDebuggerEvents, &m_dwDbgEvents);
  31. hr = pApp->SetAddInInfo((long)_Module.GetModuleInstance(), 
  32. static_cast<ICmdWnd*>(this), IDB_TOOLBAR_MEDIUM_CMDWND, IDB_TOOLBAR_LARGE_CMDWND, dwAddInID);
  33. LPCTSTR szCommand = _T("Command Window");
  34. VARIANT_BOOL bRet;
  35. if (SUCCEEDED(hr))
  36. {
  37. hr = pApp->AddCommand(CComBSTR(_T("Command WindownCommand WindownCommand WindownCommand Window")),CComBSTR(_T("DoCmdWnd")), 0, dwAddInID, &bRet);
  38. }
  39. // Add toolbar buttons only if this is the first time the add-in
  40. // is being loaded.  Toolbar buttons are automatically remembered
  41. // by Developer Studio from session to session, so we should only
  42. // add the toolbar buttons once.
  43. if (bFirstTime)
  44. {
  45. if (SUCCEEDED(hr))
  46. {
  47. hr = pApp->AddCommandBarButton(dsGlyph, CComBSTR(_T("Command Window")), dwAddInID);
  48. }
  49. }
  50. *bOnConnection = SUCCEEDED(hr) ? VARIANT_TRUE :VARIANT_FALSE;
  51. return hr;
  52. }
  53. HRESULT CCmdWnd::OnDisconnection(VARIANT_BOOL bLastTime)
  54. {
  55. AtlUnadvise(m_spApplication, IID_IApplicationEvents, m_dwAppEvents);
  56. AtlUnadvise(m_spApplication, IID_IDebuggerEvents, m_dwDbgEvents);
  57. return S_OK;
  58. }
  59. /////////////////////////////////////////////////////////////////////////////
  60. // Application events
  61. HRESULT CCmdWnd::BeforeBuildStart()
  62. {
  63. return S_OK;
  64. }
  65. HRESULT CCmdWnd::BuildFinish(long nNumErrors, long nNumWarnings)
  66. {
  67. return S_OK;
  68. }
  69. HRESULT CCmdWnd::BeforeApplicationShutDown()
  70. {
  71. if (m_pCommandWindow)
  72. {
  73. m_pCommandWindow->DoClose();
  74. }
  75. return S_OK;
  76. }
  77. HRESULT CCmdWnd::DocumentOpen(IDispatch* theDocument)
  78. {
  79. return S_OK;
  80. }
  81. HRESULT CCmdWnd::BeforeDocumentClose(IDispatch* theDocument)
  82. {
  83. return S_OK;
  84. }
  85. HRESULT CCmdWnd::DocumentSave(IDispatch* theDocument)
  86. {
  87. return S_OK;
  88. }
  89. HRESULT CCmdWnd::NewDocument(IDispatch* theDocument)
  90. {
  91. return S_OK;
  92. }
  93. HRESULT CCmdWnd::WindowActivate(IDispatch* theWindow)
  94. {
  95. return S_OK;
  96. }
  97. HRESULT CCmdWnd::WindowDeactivate(IDispatch* theWindow)
  98. {
  99. return S_OK;
  100. }
  101. HRESULT CCmdWnd::WorkspaceOpen()
  102. {
  103. return S_OK;
  104. }
  105. HRESULT CCmdWnd::WorkspaceClose()
  106. {
  107. return S_OK;
  108. }
  109. HRESULT CCmdWnd::NewWorkspace()
  110. {
  111. return S_OK;
  112. }
  113. /////////////////////////////////////////////////////////////////////////////
  114. // Debugger event
  115. HRESULT CCmdWnd::BreakpointHit(IDispatch* pBreakpoint)
  116. {
  117. return S_OK;
  118. }
  119. HRESULT CCmdWnd::DoCmdWnd()
  120. {
  121. HRESULT hr = E_FAIL;
  122. if (m_pCommandWindow == NULL)
  123. {
  124. hr = CoCreateInstance(CLSID_CommandWindow, NULL, CLSCTX_ALL, IID_ICommandWindow, (LPVOID *)&m_pCommandWindow);
  125. if (SUCCEEDED(hr) && m_pCommandWindow != NULL) // if m_pCommandWindow is null, proxy problem or interface. Try rebuild ALL
  126. {
  127. hr = m_pCommandWindow->Open(m_spApplication);
  128. _ASSERTE(SUCCEEDED(hr));
  129. if (SUCCEEDED(hr))
  130. {
  131. CComQIPtr<_ICommandWindowEvents, &DIID__ICommandWindowEvents> pEvents;
  132. CComQIPtr<IUnknown, &IID_IUnknown> pUnkThis;
  133. CComQIPtr<IUnknown, &IID_IUnknown> pUnkCmdWnd = m_pCommandWindow;
  134. pUnkThis = (ICmdWnd *)this;
  135. pEvents = pUnkThis;
  136. _ASSERTE(pEvents);
  137. hr = AtlAdvise(pUnkCmdWnd, GetUnknown(), DIID__ICommandWindowEvents,  &m_dwCookie);
  138. }
  139. }
  140. if (FAILED(hr))
  141. {
  142. m_pCommandWindow.Release(); // don't hold onto this if we can't get told when it closes.
  143. m_pCommandWindow = NULL;
  144. }
  145. }
  146. else
  147. {
  148. m_pCommandWindow->SetFocus();
  149. }
  150. return S_OK;
  151. }
  152. HRESULT _stdcall CCmdWnd::OnClose()
  153. { // called when IShinc is closing down.
  154. HRESULT hr = S_OK;
  155. m_pCommandWindow.Release();
  156. m_pCommandWindow = NULL;
  157. return(hr);
  158. }