使用soui开发的mbc,只支持windows版本
w1146869587
2022-01-24 4905e2e7537d507f218e8e9595485e09d9f3a2b4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// MBAddIn.cpp : Implementation of CMBAddIn
 
#include "stdafx.h"
#include "MBAddIn.h"
#include "BaseFun.h"
// CMBAddIn
 
 
 
 
_ATL_FUNC_INFO OnClickButtonInfo ={CC_STDCALL,VT_EMPTY,2,{VT_DISPATCH,VT_BYREF | VT_BOOL}};
 
 
void _stdcall CMBAddIn::OnClickButton(IDispatch *Ctrl, VARIANT_BOOL *CancelDefault)
{
    USES_CONVERSION;
    HRESULT hr;
    TCHAR    szProjectName[MAX_PATH];
    memset( szProjectName, 0, MAX_PATH );
    
    wsprintf ( szProjectName, _T("%s"),IDS_PROJNAME );
    CComQIPtr<Office::_CommandBarButton> pCommandBarButton(Ctrl);
    CComBSTR bszFileName;
    //the button that raised the event. Do something with this...
    if ( m_spWordApp != NULL )
    {
        Word::_DocumentPtr spDoc;
        hr = m_spWordApp->get_ActiveDocument(&spDoc);
 
        if(spDoc != NULL)
        {
            spDoc->Save();
 
            hr = spDoc->get_FullName(&bszFileName);
            SendMbcFile( (LPCTSTR)(_bstr_t)bszFileName);
        }
    }
    else if ( m_spExcelApp != NULL )
    {
        Excel::_WorkbookPtr spWorkbook;
        hr = m_spExcelApp->get_ActiveWorkbook(&spWorkbook);
 
        if(spWorkbook != NULL)
        {
            spWorkbook->Save();
 
            spWorkbook->get_FullName(0, &bszFileName );
            SendMbcFile( (LPCTSTR)(_bstr_t)bszFileName);
        }
    }
    else if ( m_spPowerPointApp != NULL )
    {
        MSPPT::_PresentationPtr spPresentation;
        hr = m_spPowerPointApp->get_ActivePresentation(&spPresentation);
 
        if(spPresentation != NULL)
        {
            spPresentation->Save();
 
            hr = spPresentation->get_FullName(&bszFileName);
            SendMbcFile( (LPCTSTR)(_bstr_t)bszFileName);
        }
    }
}
 
STDMETHODIMP CMBAddIn::OnConnection(IDispatch* Application, ext_ConnectMode ConnectMode, IDispatch * AddInInst, SAFEARRAY * * custom)
{
//    AFX_MANAGE_STATE(AfxGetStaticModuleState())
 
    CComQIPtr<Word::_Application> spWordApp(Application);
    CComQIPtr<Excel::_Application> spExcelApp(Application);
    CComQIPtr<MSPPT::_Application>    spPowerPointApp(Application);
    m_spWordApp = spWordApp;
    m_spExcelApp = spExcelApp;
    m_spPowerPointApp = spPowerPointApp;
 
    CComPtr<Office::_CommandBars> spCmdBars;
    HRESULT hr;
    if ( spWordApp != NULL )
        hr = spWordApp->get_CommandBars(&spCmdBars);
    else if ( spExcelApp != NULL )
        hr = spExcelApp->get_CommandBars(&spCmdBars);
    else if ( spPowerPointApp != NULL )
        hr = spPowerPointApp->get_CommandBars(&spCmdBars );
    else
        return E_FAIL;
//    ATLASSERT(spCmdBars);
    HBITMAP    hBmp;
 
    hBmp = ::LoadBitmap( ::GetModuleHandle( _T("mbInside.dll") ), MAKEINTRESOURCE(IDB_MBC));
 
    ::OpenClipboard(NULL);
    ::SetClipboardData(CF_BITMAP, (HANDLE)hBmp);
    ::CloseClipboard();
    ::DeleteObject( hBmp );
 
    CComVariant vtName(_T("MBC"));
    CComVariant vtPos(1); // position it below all toolbands
    CComVariant vtTemp(VARIANT_TRUE); // is temporary
    CComVariant vtEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR);
    CComPtr<Office::CommandBar> spNewCmdBar;
    VARIANT_BOOL bVisible = VARIANT_TRUE;
 
    hr = spCmdBars->get_Item( vtName, &spNewCmdBar );
    if ( spNewCmdBar != NULL )
    {
        spNewCmdBar->get_Visible( &bVisible );
    }
    else
    {
        hr = spCmdBars->Add( vtName, vtPos, vtEmpty, vtTemp, &spNewCmdBar);
    }
    if(FAILED(hr))
        return hr;
    ATLASSERT(spNewCmdBar);
 
    CComPtr<Office::CommandBarControls> spBarControls;
    hr = spNewCmdBar->get_Controls( &spBarControls );
    if(FAILED(hr))
        return hr;
    ATLASSERT(spBarControls);
    CComVariant vtToolBarType(1);
    CComPtr <Office::CommandBarControl> spNewBar; 
    CComVariant vtShow(VARIANT_TRUE);
    // add  button
 
    CComBSTR bszEasySend;
    CString    strEasySend;
    strEasySend.Format ( _T("%s"),IDS_EASY_SEND );
 
    bszEasySend = strEasySend.AllocSysString();
    //CSTKInsideFun::WriteLog(_T("OnConnection 5.2"));
    hr = spBarControls->get_Item(CComVariant(bszEasySend), &spNewBar );
    
    //CSTKInsideFun::WriteLog(_T("OnConnection 5.3"));
    if ( spNewBar == NULL )
    {
        //CSTKInsideFun::WriteLog(_T("OnConnection 5.4"));
        hr = spBarControls->Add( vtToolBarType, vtEmpty, CComVariant(bszEasySend), vtEmpty, vtShow, &spNewBar);
    }
    if(FAILED(hr))
        return hr;
    //CSTKInsideFun::WriteLog(_T("OnConnection 5.5"));
    ATLASSERT(spNewBar);
    // get CommandBarButton interface so we can specify button styles and stuff
    // each button displays a bitmap and caption next to it
    CComQIPtr<Office::_CommandBarButton> spCmdButton(spNewBar);
    ATLASSERT(spCmdButton);
 
    spCmdButton->put_Style(Office::msoButtonIconAndCaption);
    spCmdButton->PasteFace();  // use own bitmap
    spCmdButton->put_Caption((BSTR)bszEasySend);
    spCmdButton->put_Enabled(VARIANT_TRUE);
    spCmdButton->put_TooltipText((BSTR)bszEasySend);
    spCmdButton->put_Tag((BSTR)bszEasySend);
    spCmdButton->put_Visible(VARIANT_TRUE);
    spNewCmdBar->put_Visible(VARIANT_TRUE); 
    m_spButton = spCmdButton;
    bszEasySend.Empty();
    hr = CommandButtonEvents::DispEventAdvise((IDispatch*)m_spButton);
    if(FAILED(hr))
        return hr;
    return S_OK;
}
 
STDMETHODIMP CMBAddIn::OnDisconnection(ext_DisconnectMode RemoveMode, SAFEARRAY * * custom)
{
    if ( m_spButton != NULL )
        CommandButtonEvents::DispEventUnadvise((IDispatch*)m_spButton);
 
    return S_OK;
}
 
 
 
void CMBAddIn::SendMbcFile(CString strFile)
{
    HRESULT            hr;
    CString            strMBExe;
    strMBExe.Format( _T("%s\\mbcU.exe"), CBaseFun::GetSTKWorkDir());
    HANDLE hEventShareMemory  =   OpenEvent(EVENT_ALL_ACCESS , FALSE, _T("mbcSharedMemoryEvent")); 
    if (NULL == hEventShareMemory)
    {
        long nErrorId = GetLastError();
        CString  strMessage;
        strMessage.Format(_T("OpenEvent mbcSharedMemoryEvent ErrorID:%d"),nErrorId);
        ShellExecute( NULL, _T("open"), strMBExe, _T(""), _T(""), SW_SHOW );
        //MessageBox(hWnd, strMessage, _T("mbEasySend"), MB_OK | MB_ICONINFORMATION );
        return;
    }
    HANDLE hShareMemory = OpenFileMapping( PAGE_READONLY,false,_T("mbcSharedMemory") );
    if (NULL == hShareMemory)
    {
        long nErrorId = GetLastError();
        CString  strMessage;
        strMessage.Format(_T("OpenFileMapping mbcSharedMemory ErrorID:%d"),nErrorId);
        //MessageBox(hWnd, strMessage, _T("mbEasySend"), MB_OK | MB_ICONINFORMATION );
        ShellExecute( NULL, _T("open"), strMBExe, _T(""), _T(""), SW_SHOW );
        return;
    }
    TCHAR* lpBuffer = (TCHAR*)MapViewOfFile(hShareMemory, FILE_MAP_WRITE, 0, 0, 2560);
    if (NULL == lpBuffer)
    {
        //MessageBox(hWnd, _T("MapViewOfFile mbcSharedMemoryʧ°Ü"), _T("mbEasySend"), MB_OK | MB_ICONINFORMATION );
        long nErrorId = GetLastError();
        CString  strMessage;
        strMessage.Format(_T("MapViewOfFile mbcSharedMemory ErrorID:%d"),nErrorId);
        return;
    }
    CString strXmlBuf;
 
    strXmlBuf.Format(_T("<body type='0'><file>%s</file></body>"),strFile);
    _stprintf(lpBuffer,_T("%s"), strXmlBuf );
    UnmapViewOfFile(lpBuffer);
    SetEvent(hEventShareMemory);
    CloseHandle(hEventShareMemory);
    CloseHandle(hShareMemory);
    return;
}