soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SActiveX.cpp
1#if defined(_WIN32) && !defined(__MINGW32__)
2#include "souistd.h"
3#include "activex/SBStr.h"
5#include "control/SActiveX.h"
6
7SNSBEGIN
8//////////////////////////////////////////////////////////////////////////
9class SAxContainerImpl
10 : public SAxContainer
11 , public IAxHostDelegate {
12 public:
13 SAxContainerImpl(SActiveX *pOwner)
14 : m_pOwner(pOwner)
15 {
16 SetAxHost(this);
17 }
18
19 ~SAxContainerImpl()
20 {
21 Clear();
22 }
23
24 protected:
25 virtual HWND GetAxHostWindow() const
26 {
27 return m_pOwner->GetContainer()->GetHostHwnd();
28 }
29 virtual void OnAxActivate(IUnknown *pCtrl)
30 {
31 m_pOwner->OnAxActivate(pCtrl);
32 }
33
34 virtual void OnAxInvalidate(LPCRECT pRect, BOOL bErase)
35 {
36 m_pOwner->InvalidateRect(pRect);
37 }
38
39 virtual void OnAxSetCapture(BOOL fCapture)
40 {
41 if (fCapture)
42 {
43 m_pOwner->SetCapture();
44 }
45 else
46 {
47 m_pOwner->ReleaseCapture();
48 }
49 }
50
51 virtual HRESULT OnAxGetDC(LPCRECT pRect, DWORD grfFlags, HDC *phDC)
52 {
53 return S_FALSE;
54 }
55
56 virtual HRESULT OnAxReleaseDC(HDC hdc)
57 {
58 return S_FALSE;
59 }
60
61 SActiveX *m_pOwner;
62};
63
64//////////////////////////////////////////////////////////////////////////
65
67 : m_axContainer(new SAxContainerImpl(this))
68 , m_clsid(CLSID_NULL)
69 , m_clsCtx(CLSCTX_ALL)
70 , m_bDelayInit(FALSE)
71{
72}
73
75{
76 delete m_axContainer;
77}
78
80{
81#if _MSC_VER <= 1400
82 return FALSE;
83#endif
84 BOOL bRet = m_axContainer->CreateControl(m_clsid, m_clsCtx);
85 if (bRet)
86 {
87 CRect rcClient;
88 GetClientRect(&rcClient);
89 m_axContainer->ActivateAx(NULL);
91 m_axContainer->OnPosRectChange(rcClient);
92 }
94 return bRet;
95}
96
98{
99 HDC hdc = pRT->GetDC(0);
100 CRect rcClient = GetClientRect();
101 m_axContainer->Draw(hdc, &rcClient);
102 pRT->ReleaseDC(hdc, &rcClient);
103}
104
105int SActiveX::OnCreate(LPVOID)
106{
107 int nRet = __baseCls::OnCreate(NULL);
108 if (nRet != 0)
109 return nRet;
110 if (IsEqualCLSID(m_clsid, CLSID_NULL))
111 return 0;
112
113 if (!m_bDelayInit)
114 InitActiveX();
115 return 0;
116}
117
118void SActiveX::OnSize(UINT nType, CSize size)
119{
120 __baseCls::OnSize(nType, size);
121 if (m_axContainer->GetActiveXControl())
122 {
123 m_axContainer->OnPosRectChange(GetWindowRect());
124 }
125}
126
127void SActiveX::OnShowWindow(BOOL bShow, UINT nStatus)
128{
129 __baseCls::OnShowWindow(bShow, nStatus);
130
131 if (IsVisible(TRUE) && m_bDelayInit)
132 {
133 InitActiveX(); //窗口显示时才初始化
134 m_bDelayInit = FALSE;
135 }
136
138}
139
140LRESULT SActiveX::OnMouseEvent(UINT uMsg, WPARAM wp, LPARAM lp)
141{
142 if (!m_axContainer->GetActiveXControl())
143 return 0;
144 if (uMsg == WM_LBUTTONDOWN)
145 SetFocus();
146 return m_axContainer->OnWindowMessage(uMsg, wp, lp);
147}
148
149LRESULT SActiveX::OnKeyEvent(UINT uMsg, WPARAM wp, LPARAM lp)
150{
151 if (!m_axContainer->GetActiveXControl())
152 return 0;
153 return m_axContainer->OnWindowMessage(uMsg, wp, lp);
154}
155
156HRESULT SActiveX::OnAttrClsid(const SStringW &strValue, BOOL bLoading)
157{
158 OLECHAR szCLSID[100] = { 0 };
159 wcscpy(szCLSID, strValue);
160
161 HRESULT hr = E_FAIL;
162 if (szCLSID[0] == L'{')
163 hr = ::CLSIDFromString(szCLSID, &m_clsid);
164 else
165 hr = ::CLSIDFromProgID(szCLSID, &m_clsid);
166
167 if (!SUCCEEDED(hr))
168 return S_FALSE;
169 return S_OK;
170}
171
172void SActiveX::SetActiveXVisible(BOOL bVisible)
173{
174 if (m_axContainer->GetActiveXControl())
175 {
176 SComQIPtr<IOleWindow> ole_window = m_axContainer->GetActiveXControl();
177 if (!ole_window)
178 {
179 return;
180 }
181
182 HWND window = NULL;
183 ole_window->GetWindow(&window);
184 if (!window)
185 {
186 return;
187 }
188 HWND hWnd = NULL;
189
190 if (!bVisible)
191 {
192 HWND hFocus = ::GetFocus();
193 hWnd = hFocus;
194 while (hWnd && hWnd != window)
195 {
196 hWnd = ::GetParent(hWnd);
197 }
198 }
199 ShowWindow(window, bVisible ? SW_SHOW : SW_HIDE);
200 if (hWnd == window) //避免主窗口失去焦点
202 }
203}
204
205IUnknown *SActiveX::GetIUnknown()
206{
207 if (!m_axContainer)
208 return NULL;
209 return m_axContainer->GetActiveXControl();
210}
211
212void SActiveX::SetExternalUIHandler(IDocHostUIHandler *pUiHandler)
213{
214 if (m_axContainer)
215 m_axContainer->SetExternalUIHandler(pUiHandler);
216}
217
218SNSEND
219
220#endif // defined(_WIN32) && !defined(__MINGW32__)
Header file for ActiveX container implementation.
void SetAxHost(IAxHostDelegate *pAxHost)
SActiveX()
Default constructor.
int OnCreate(LPVOID lp)
Called when the window is created.
LRESULT OnMouseEvent(UINT uMsg, WPARAM wp, LPARAM lp)
Called when a mouse event occurs.
BOOL m_bDelayInit
Flag indicating whether the initialization of the ActiveX control should be delayed.
Definition SActiveX.h:176
LRESULT OnKeyEvent(UINT uMsg, WPARAM wp, LPARAM lp)
Called when a keyboard event occurs.
CLSID m_clsid
CLSID of the ActiveX control.
Definition SActiveX.h:166
void OnShowWindow(BOOL bShow, UINT nStatus)
Called when the window is shown or hidden.
HRESULT OnAttrClsid(const SStringW &strValue, BOOL bLoading)
Handles the "clsID" attribute.
virtual ~SActiveX()
Destructor.
void OnPaint(IRenderTarget *pRT)
Called when the window needs to be painted.
virtual void OnInitActiveXFinished()
Called when the initialization of the ActiveX control is finished.
Definition SActiveX.h:135
void OnSize(UINT nType, CSize size)
Called when the window size changes.
void SetExternalUIHandler(IDocHostUIHandler *pUiHandler)
Sets the external UI handler for the ActiveX control.
IUnknown * GetIUnknown()
Retrieves the IUnknown interface of the ActiveX control.
void SetActiveXVisible(BOOL bVisible)
Sets the visibility of the ActiveX control.
DWORD m_clsCtx
Context in which the ActiveX control should be created.
Definition SActiveX.h:171
BOOL InitActiveX()
Initializes the ActiveX control.
SAxContainerImpl * m_axContainer
Pointer to the internal ActiveX container implementation.
Definition SActiveX.h:161
Implementation of an ActiveX container.
A class representing an ASCII string.
Definition sstringw.h:96
SWindow * GetParent() const
Retrieves the parent window.
Definition Swnd.cpp:3488
CRect GetWindowRect() const
Retrieves the bounding rectangle of the window.
Definition Swnd.cpp:230
BOOL IsVisible(BOOL bCheckParent=FALSE) SCONST OVERRIDE
Checks if the window is visible.
Definition Swnd.cpp:646
ISwndContainer * GetContainer() OVERRIDE
Retrieves the container associated with this window.
Definition Swnd.cpp:679
virtual CRect GetClientRect() const
Retrieves the client rectangle of the window.
Definition Swnd.cpp:243
void SetFocus() OVERRIDE
Sets the focus to the window.
Definition Swnd.cpp:2620
HWND GetHostHwnd() OVERRIDE
Retrieves the host window handle.
Definition Swnd.cpp:3616
Delegate interface for ActiveX host operations.
virtual void OnAxInvalidate(LPCRECT pRect, BOOL bErase)=0
Notify the host that the ActiveX control needs to be invalidated.
virtual void OnAxSetCapture(BOOL fCapture)=0
Notify the host to set or release the mouse capture.
virtual void OnAxActivate(IUnknown *pCtrl)=0
Notify the host that the ActiveX control is being activated.
virtual HRESULT OnAxGetDC(LPCRECT pRect, DWORD grfFlags, HDC *phDC)=0
Get a device context for the ActiveX control.
virtual HWND GetAxHostWindow() const =0
Get the window handle of the ActiveX host.
virtual HRESULT OnAxReleaseDC(HDC hdc)=0
Release a previously obtained device context.
Interface for rendering target objects.
Definition SRender-i.h:1440
HDC GetDC(UINT uFlag) PURE
Retrieves a device context (DC) compatible with the render target.
void ReleaseDC(HDC hdc, LPCRECT pRc=NULL) PURE
Releases a previously retrieved device context (DC).