soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SHostPresenter.cpp
1#include <souistd.h>
2#include <core/SHostPresenter.h>
3#include <core/SHostWnd.h>
4SNSBEGIN
5
6#ifdef _WIN32
7struct S_UPDATELAYEREDWINDOWINFO
8{
9 DWORD cbSize;
10 HDC hdcDst;
11 POINT *pptDst;
12 SIZE *psize;
13 HDC hdcSrc;
14 POINT *pptSrc;
15 COLORREF crKey;
16 BLENDFUNCTION *pblend;
17 DWORD dwFlags;
18 RECT *prcDirty;
19};
20
21typedef BOOL(WINAPI *FunUpdateLayeredWindow)(HWND hwnd, HDC hdcDst, const POINT *pptDst, const SIZE *psize, HDC hdcSrc, const POINT *pptSrc, COLORREF crKey, const BLENDFUNCTION *pblend, DWORD dwflags);
22typedef BOOL(WINAPI *FunUpdateLayeredWindowIndirect)(HWND hwnd, const S_UPDATELAYEREDWINDOWINFO *pULWInfo);
23
24static FunUpdateLayeredWindow s_funUpdateLayeredWindow = NULL;
25static FunUpdateLayeredWindowIndirect s_funUpdateLayeredWindowIndirect = NULL;
26
27BOOL WINAPI _SUpdateLayeredWindowIndirect(HWND hWnd, const S_UPDATELAYEREDWINDOWINFO *info)
28{
29 SASSERT(s_funUpdateLayeredWindow);
30 return (*s_funUpdateLayeredWindow)(hWnd, info->hdcDst, info->pptDst, info->psize, info->hdcSrc, info->pptSrc, info->crKey, info->pblend, info->dwFlags);
31}
32
33BOOL SWndSurface::Init()
34{
35 HMODULE hUser32 = GetModuleHandle(_T("user32"));
36 if (!hUser32)
37 {
38 SASSERT(FALSE);
39 return FALSE;
40 }
41 s_funUpdateLayeredWindow = (FunUpdateLayeredWindow)GetProcAddress(hUser32, "UpdateLayeredWindow");
42 if (!s_funUpdateLayeredWindow)
43 {
44 SASSERT(FALSE);
45 return FALSE;
46 }
47#ifdef _WIN64 // X64中使用UpdateLayeredWindowIndirect在Win7测试显示不出内容,只能使用UpdateLayeredWindow,fuck
48 // MS.
49 s_funUpdateLayeredWindowIndirect = _SUpdateLayeredWindowIndirect;
50#else
51 s_funUpdateLayeredWindowIndirect = (FunUpdateLayeredWindowIndirect)GetProcAddress(hUser32, "UpdateLayeredWindowIndirect");
52 if (!s_funUpdateLayeredWindowIndirect)
53 s_funUpdateLayeredWindowIndirect = _SUpdateLayeredWindowIndirect;
54#endif
55 return TRUE;
56}
57
58BOOL SWndSurface::SUpdateLayeredWindowIndirect(HWND hWnd, const S_UPDATELAYEREDWINDOWINFO *pULWInfo)
59{
60 BOOL bRet = s_funUpdateLayeredWindowIndirect(hWnd, pULWInfo);
61 return bRet;
62}
63
64//===========================================================
66 : m_pHostWnd(pHostWnd)
67{
68}
69
71{
72}
73
75{
76}
77
79{
80}
81
82void SHostPresenter::OnHostResize(THIS_ SIZE szHost)
83{
84}
85
86void SHostPresenter::OnHostPresent(THIS_ HDC hdc, IRenderTarget *pMemRT, LPCRECT rcInvalid, BYTE byAlpha)
87{
88 if (m_pHostWnd->IsTranslucent())
89 {
90 UpdateLayerFromRenderTarget(pMemRT, byAlpha, rcInvalid);
91 }
92 else
93 {
94 BOOL bGetDC = hdc == 0;
95 if (bGetDC)
96 hdc = m_pHostWnd->GetDC();
97 HDC memdc = pMemRT->GetDC(1);
98 ::BitBlt(hdc, rcInvalid->left, rcInvalid->top, rcInvalid->right - rcInvalid->left, rcInvalid->bottom - rcInvalid->top, memdc, rcInvalid->left, rcInvalid->top, SRCCOPY);
99 pMemRT->ReleaseDC(memdc, NULL);
100 if (bGetDC)
101 m_pHostWnd->ReleaseDC(hdc);
102 }
103}
104
105void SHostPresenter::UpdateLayerFromRenderTarget(IRenderTarget *pRT, BYTE byAlpha, LPCRECT prcDirty)
106{
107 RECT rc;
108 ((SNativeWnd *)m_pHostWnd)->GetWindowRect(&rc);
109 RECT rcDirty = { 0 };
110 if (prcDirty)
111 {
112 rcDirty = *prcDirty;
113 }
114 else
115 {
116 rcDirty.right = rc.right - rc.left;
117 rcDirty.bottom = rc.bottom - rc.top;
118 }
119 BLENDFUNCTION bf = { AC_SRC_OVER, 0, byAlpha, AC_SRC_ALPHA };
120
121 POINT ptDst = { rc.left, rc.top };
122 SIZE szDst = { rc.right - rc.left, rc.bottom - rc.top };
123 POINT ptSrc = { 0 };
124
125 HDC hdc = pRT->GetDC(1);
126 S_UPDATELAYEREDWINDOWINFO info = { sizeof(info), NULL, &ptDst, &szDst, hdc, &ptSrc, 0, &bf, ULW_ALPHA, &rcDirty };
127 SWndSurface::SUpdateLayeredWindowIndirect(m_pHostWnd->GetNative()->GetHwnd(), &info);
128 pRT->ReleaseDC(hdc, NULL);
129}
130
131#else
133 : m_pHostWnd(pHostWnd)
134{
135}
136
140
142{
143}
144
146{
147}
148
149void SHostPresenter::OnHostResize(THIS_ SIZE szHost)
150{
151}
152
153void SHostPresenter::OnHostPresent(THIS_ HDC hdc, IRenderTarget *pMemRT, LPCRECT rcInvalid, BYTE byAlpha)
154{
155 BOOL bGetDC = hdc == 0;
156 if (bGetDC)
157 hdc = m_pHostWnd->GetDC();
158 HDC memdc = pMemRT->GetDC(1);
159 ::BitBlt(hdc, rcInvalid->left, rcInvalid->top, rcInvalid->right - rcInvalid->left, rcInvalid->bottom - rcInvalid->top, memdc, rcInvalid->left, rcInvalid->top, SRCCOPY);
160 if (m_pHostWnd->GetHostAttr().m_bTranslucent && m_pHostWnd->GetHostAttr().m_bAutoShape)
161 {
162 HBITMAP hBmp = GetCurrentObject(memdc, OBJ_BITMAP);
163 HRGN hRgn = CreateRegionFromBitmap(hBmp, 0, 0xFF000000);
164 SetWindowRgn(m_pHostWnd->m_hWnd, hRgn, FALSE);
165 if (hRgn)
166 DeleteObject(hRgn);
167 }
168 pMemRT->ReleaseDC(memdc, NULL);
169 if (bGetDC)
170 m_pHostWnd->ReleaseDC(hdc);
171}
172
173#endif //_WIN32
174SNSEND
void UpdateLayerFromRenderTarget(IRenderTarget *pRT, BYTE byAlpha, LPCRECT prcDirty=NULL)
Updates the layer from the render target.
void OnHostPresent(HDC hdc, IRenderTarget *pMemRT, LPCRECT rcDirty, BYTE byAlpha) OVERRIDE
Called to present the host window.
void OnHostResize(SIZE szHost) OVERRIDE
Called when the host window is resized.
SHostPresenter(SHostWnd *pHostWnd)
Constructor for SHostPresenter.
void OnHostCreate() OVERRIDE
Called when the host window is created.
void OnHostDestroy() OVERRIDE
Called when the host window is destroyed.
~SHostPresenter(void)
Destructor for SHostPresenter.
The main host window class responsible for managing the layout, events, and rendering of SOUI windows...
Definition SHostWnd.h:318
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).