桌面 透明 三角形 分层窗口 DX

  1 //桌面 透明 三角形 分层窗口 DX
  2 //IDirect3DSurface9 GetDC  UpdateLayeredWindow
  3 
  4 #include <Windows.h>
  5 #include <mmsystem.h>
  6 #include <d3dx9.h>
  7 #pragma warning( disable : 4996 ) 
  8 #include <strsafe.h>
  9 #pragma warning( default : 4996 )
 10 
 11 //include path $(DXSDK_DIR)Include;
 12 //library path $(DXSDK_DIR)Lib\x86
 13 //library d3dx9.lib d3d9.lib Winmm.lib
 14 
 15 
 16 LPDIRECT3D9             g_pD3D = NULL;
 17 LPDIRECT3DDEVICE9       g_pd3dDevice = NULL;
 18 LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL;
 19 
 20 HWND g_hWnd = NULL;
 21 IDirect3DSurface9*        g_pkRenderTarget = NULL;
 22 IDirect3DSurface9*        g_pkOffscreenPlainSurface = NULL;
 23 
 24 struct CUSTOMVERTEX
 25 {
 26     FLOAT x, y, z;
 27     DWORD color;
 28 };
 29 
 30 #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
 31 
 32 HRESULT InitD3D(HWND hWnd)
 33 {
 34     if (NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
 35     {
 36         return E_FAIL;
 37     }
 38 
 39     D3DPRESENT_PARAMETERS d3dpp;
 40     ZeroMemory(&d3dpp, sizeof(d3dpp));
 41     d3dpp.Windowed = TRUE;
 42     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
 43     d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
 44 
 45     if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice)))
 46     {
 47         return E_FAIL;
 48     }
 49 
 50     g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
 51     g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
 52 
 53     return S_OK;
 54 }
 55 
 56 HRESULT InitGeometry()
 57 {
 58     CUSTOMVERTEX g_Vertices[] =
 59     {
 60         { -1.0f, -1.0f, 0.0f, 0xffff0000, },
 61         { 1.0f, -1.0f, 0.0f, 0xff0000ff, },
 62         { 0.0f, 1.0f, 0.0f, 0xffffffff, },
 63     };
 64 
 65     if (FAILED(g_pd3dDevice->CreateVertexBuffer(3 * sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL)))
 66     {
 67         return E_FAIL;
 68     }
 69 
 70     VOID* pVertices;
 71     if (FAILED(g_pVB->Lock(0, sizeof(g_Vertices), (void**)&pVertices, 0)))
 72     {
 73         return E_FAIL;
 74     }
 75     memcpy(pVertices, g_Vertices, sizeof(g_Vertices));
 76     g_pVB->Unlock();
 77 
 78     return S_OK;
 79 }
 80 
 81 VOID Cleanup()
 82 {
 83     if (g_pVB != NULL)
 84     {
 85         g_pVB->Release();
 86     }
 87     if (g_pd3dDevice != NULL)
 88     {
 89         g_pd3dDevice->Release();
 90     }
 91     if (g_pD3D != NULL)
 92     {
 93         g_pD3D->Release();
 94     }
 95 }
 96 
 97 VOID SetupMatrices()
 98 {
 99     D3DXMATRIXA16 matWorld;
100     UINT iTime = timeGetTime() % 1000;
101     FLOAT fAngle = iTime * (2.0f * D3DX_PI) / 1000.0f;
102     D3DXMatrixRotationY(&matWorld, fAngle);
103     g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
104 
105     D3DXVECTOR3 vEyePt(0.0f, 3.0f, -5.0f);
106     D3DXVECTOR3 vLookatPt(0.0f, 0.0f, 0.0f);
107     D3DXVECTOR3 vUpVec(0.0f, 1.0f, 0.0f);
108     D3DXMATRIXA16 matView;
109     D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookatPt, &vUpVec);
110     g_pd3dDevice->SetTransform(D3DTS_VIEW, &matView);
111 
112     D3DXMATRIXA16 matProj;
113     D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI / 4, 1.0f, 1.0f, 100.0f);
114     g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
115 
116 }
117 
118 VOID Render()
119 {
120 
121     RECT kRect;
122     GetWindowRect(g_hWnd, &kRect);
123 
124     UINT uiWndWidth = kRect.right - kRect.left;
125     UINT uiWndHeight = kRect.bottom - kRect.top;
126 
127     if (!g_pkOffscreenPlainSurface)
128     {
129         g_pd3dDevice->CreateOffscreenPlainSurface(uiWndWidth, uiWndHeight, D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, &g_pkOffscreenPlainSurface, 0);
130         g_pd3dDevice->CreateRenderTarget(uiWndWidth, uiWndHeight, D3DFMT_X8R8G8B8, D3DMULTISAMPLE_NONE, 0, true, &g_pkRenderTarget, 0);
131     }
132     else
133     {
134         D3DSURFACE_DESC kDesc;
135         g_pkOffscreenPlainSurface->GetDesc(&kDesc);
136 
137         if (kDesc.Width != uiWndWidth || kDesc.Width != uiWndHeight)
138         {
139             g_pkOffscreenPlainSurface->Release();
140             g_pkRenderTarget->Release();
141             g_pd3dDevice->CreateOffscreenPlainSurface(uiWndWidth, uiWndHeight, D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, &g_pkOffscreenPlainSurface, 0);
142             g_pd3dDevice->CreateRenderTarget(uiWndWidth, uiWndHeight, D3DFMT_X8R8G8B8, D3DMULTISAMPLE_NONE, 0, true, &g_pkRenderTarget, 0);
143         }
144     }
145 
146     if (!g_pkOffscreenPlainSurface || !g_pkRenderTarget)
147     {
148         return;
149     }
150 
151     HRESULT hr = g_pd3dDevice->SetRenderTarget(0, g_pkRenderTarget);
152     g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
153 
154     if (SUCCEEDED(g_pd3dDevice->BeginScene()))
155     {
156         SetupMatrices();
157 
158         g_pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX));
159         g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
160         g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 1);
161 
162         g_pd3dDevice->EndScene();
163     }
164 
165     g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
166 
167     hr = g_pd3dDevice->GetRenderTargetData(g_pkRenderTarget, g_pkOffscreenPlainSurface);
168 
169     HDC hDC = 0;
170     //g_pkRenderTarget->GetDC(&hDC);
171     g_pkOffscreenPlainSurface->GetDC(&hDC);
172 
173     POINT kPoint = { 0, 0 };
174     SIZE kSize = { uiWndWidth, uiWndHeight };
175 
176     BLENDFUNCTION kBlend;
177     kBlend.AlphaFormat = AC_SRC_ALPHA;
178     kBlend.SourceConstantAlpha = 255;
179     kBlend.BlendFlags = 0;
180     kBlend.BlendOp = AC_SRC_OVER;
181 
182     UpdateLayeredWindow(g_hWnd, 0, &kPoint, &kSize, hDC, &kPoint, 0, &kBlend, ULW_ALPHA);
183 
184     g_pkOffscreenPlainSurface->ReleaseDC(hDC);
185     //g_pkRenderTarget->ReleaseDC(hDC);
186 }
187 
188 LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
189 {
190     switch (msg)
191     {
192     case WM_DESTROY:
193         Cleanup();
194         PostQuitMessage(0);
195         return 0;
196     }
197 
198     return DefWindowProc(hWnd, msg, wParam, lParam);
199 }
200 
201 INT WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, INT)
202 {
203     UNREFERENCED_PARAMETER(hInst);
204 
205     WNDCLASSEX wc =
206     {
207         sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, L"D3D Tutorial", NULL
208     };
209     RegisterClassEx(&wc);
210 
211     g_hWnd = CreateWindowEx(WS_EX_LAYERED, L"D3D Tutorial", L"D3D Tutorial 03: Matrices", WS_POPUP | WS_THICKFRAME, 100, 100, 512, 512, NULL, NULL, wc.hInstance, NULL);
212 
213     if (SUCCEEDED(InitD3D(g_hWnd)))
214     {
215         if (SUCCEEDED(InitGeometry()))
216         {
217             ShowWindow(g_hWnd, SW_SHOWDEFAULT);
218             UpdateWindow(g_hWnd);
219 
220             MSG msg;
221             ZeroMemory(&msg, sizeof(msg));
222             while (msg.message != WM_QUIT)
223             {
224                 if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
225                 {
226                     TranslateMessage(&msg);
227                     DispatchMessage(&msg);
228                 }
229                 else
230                 {
231                     Render();
232                 }
233             }
234         }
235     }
236 
237     UnregisterClass(L"D3D Tutorial", wc.hInstance);
238     return 0;
239 }

 

posted @ 2014-10-20 21:42  BinSys  阅读(747)  评论(0编辑  收藏  举报