Open suhao opened 5 years ago
//桌面 透明 三角形 分层窗口 DX
//IDirect3DSurface9 GetDC UpdateLayeredWindow
#include <Windows.h>
#include <mmsystem.h>
#include <d3dx9.h>
#pragma warning( disable : 4996 )
#include <strsafe.h>
#pragma warning( default : 4996 )
//include path $(DXSDK_DIR)Include;
//library path $(DXSDK_DIR)Lib\x86
//library d3dx9.lib d3d9.lib Winmm.lib
LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL;
HWND g_hWnd = NULL;
IDirect3DSurface9* g_pkRenderTarget = NULL;
IDirect3DSurface9* g_pkOffscreenPlainSurface = NULL;
struct CUSTOMVERTEX
{
FLOAT x, y, z;
DWORD color;
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
HRESULT InitD3D(HWND hWnd)
{
if (NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
{
return E_FAIL;
}
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice)))
{
return E_FAIL;
}
g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
return S_OK;
}
HRESULT InitGeometry()
{
CUSTOMVERTEX g_Vertices[] =
{
{ -1.0f, -1.0f, 0.0f, 0xffff0000, },
{ 1.0f, -1.0f, 0.0f, 0xff0000ff, },
{ 0.0f, 1.0f, 0.0f, 0xffffffff, },
};
if (FAILED(g_pd3dDevice->CreateVertexBuffer(3 * sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL)))
{
return E_FAIL;
}
VOID* pVertices;
if (FAILED(g_pVB->Lock(0, sizeof(g_Vertices), (void**)&pVertices, 0)))
{
return E_FAIL;
}
memcpy(pVertices, g_Vertices, sizeof(g_Vertices));
g_pVB->Unlock();
return S_OK;
}
VOID Cleanup()
{
if (g_pVB != NULL)
{
g_pVB->Release();
}
if (g_pd3dDevice != NULL)
{
g_pd3dDevice->Release();
}
if (g_pD3D != NULL)
{
g_pD3D->Release();
}
}
VOID SetupMatrices()
{
D3DXMATRIXA16 matWorld;
UINT iTime = timeGetTime() % 1000;
FLOAT fAngle = iTime * (2.0f * D3DX_PI) / 1000.0f;
D3DXMatrixRotationY(&matWorld, fAngle);
g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
D3DXVECTOR3 vEyePt(0.0f, 3.0f, -5.0f);
D3DXVECTOR3 vLookatPt(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 vUpVec(0.0f, 1.0f, 0.0f);
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookatPt, &vUpVec);
g_pd3dDevice->SetTransform(D3DTS_VIEW, &matView);
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI / 4, 1.0f, 1.0f, 100.0f);
g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
}
VOID Render()
{
RECT kRect;
GetWindowRect(g_hWnd, &kRect);
UINT uiWndWidth = kRect.right - kRect.left;
UINT uiWndHeight = kRect.bottom - kRect.top;
if (!g_pkOffscreenPlainSurface)
{
g_pd3dDevice->CreateOffscreenPlainSurface(uiWndWidth, uiWndHeight, D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, &g_pkOffscreenPlainSurface, 0);
g_pd3dDevice->CreateRenderTarget(uiWndWidth, uiWndHeight, D3DFMT_X8R8G8B8, D3DMULTISAMPLE_NONE, 0, true, &g_pkRenderTarget, 0);
}
else
{
D3DSURFACE_DESC kDesc;
g_pkOffscreenPlainSurface->GetDesc(&kDesc);
if (kDesc.Width != uiWndWidth || kDesc.Width != uiWndHeight)
{
g_pkOffscreenPlainSurface->Release();
g_pkRenderTarget->Release();
g_pd3dDevice->CreateOffscreenPlainSurface(uiWndWidth, uiWndHeight, D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, &g_pkOffscreenPlainSurface, 0);
g_pd3dDevice->CreateRenderTarget(uiWndWidth, uiWndHeight, D3DFMT_X8R8G8B8, D3DMULTISAMPLE_NONE, 0, true, &g_pkRenderTarget, 0);
}
}
if (!g_pkOffscreenPlainSurface || !g_pkRenderTarget)
{
return;
}
HRESULT hr = g_pd3dDevice->SetRenderTarget(0, g_pkRenderTarget);
g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
if (SUCCEEDED(g_pd3dDevice->BeginScene()))
{
SetupMatrices();
g_pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX));
g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 1);
g_pd3dDevice->EndScene();
}
g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
hr = g_pd3dDevice->GetRenderTargetData(g_pkRenderTarget, g_pkOffscreenPlainSurface);
HDC hDC = 0;
//g_pkRenderTarget->GetDC(&hDC);
g_pkOffscreenPlainSurface->GetDC(&hDC);
POINT kPoint = { 0, 0 };
SIZE kSize = { uiWndWidth, uiWndHeight };
BLENDFUNCTION kBlend;
kBlend.AlphaFormat = AC_SRC_ALPHA;
kBlend.SourceConstantAlpha = 255;
kBlend.BlendFlags = 0;
kBlend.BlendOp = AC_SRC_OVER;
UpdateLayeredWindow(g_hWnd, 0, &kPoint, &kSize, hDC, &kPoint, 0, &kBlend, ULW_ALPHA);
g_pkOffscreenPlainSurface->ReleaseDC(hDC);
//g_pkRenderTarget->ReleaseDC(hDC);
}
LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
Cleanup();
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
INT WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, INT)
{
UNREFERENCED_PARAMETER(hInst);
WNDCLASSEX wc =
{
sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, L"D3D Tutorial", NULL
};
RegisterClassEx(&wc);
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);
if (SUCCEEDED(InitD3D(g_hWnd)))
{
if (SUCCEEDED(InitGeometry()))
{
ShowWindow(g_hWnd, SW_SHOWDEFAULT);
UpdateWindow(g_hWnd);
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
Render();
}
}
}
}
UnregisterClass(L"D3D Tutorial", wc.hInstance);
return 0;
}
透明窗口
透明窗口的硬件加速
https://docs.microsoft.com/zh-cn/windows/desktop/winmsg/window-features