请帮助我。 我创建了一个简单的窗口(使用c ++ winapi)和初始直接设备:
long _stdcall WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc (hWnd, message, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hprevinstance,LPSTR lpCmdLine ,int nCmdShow)
{
MSG msg;
WNDCLASSEX windclassex;
windclassex.cbSize = sizeof(WNDCLASSEX);
windclassex.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC | CS_DBLCLKS ;
windclassex.lpfnWndProc = WindowProc;
windclassex.cbClsExtra = NULL;
windclassex.cbWndExtra = NULL;
windclassex.hInstance = hinstance;
windclassex.hIcon = LoadIcon(NULL , IDI_APPLICATION);
windclassex.hIconSm = LoadIcon(NULL , IDI_APPLICATION);
windclassex.hCursor = LoadCursor(NULL , IDC_ARROW);
windclassex.hbrBackground = (HBRUSH)BLACK_BRUSH;
windclassex.lpszMenuName = NULL;
windclassex.lpszClassName = "WindowsClass";
RegisterClassEx(&windclassex);
hwnd = CreateWindowExA(0,"WindowsClass" ,"DirectX9" ,WS_POPUP,x ,y ,800 ,600 ,NULL ,NULL ,hinstance ,NULL);
InitialDirect3D(hwnd));
ShowWindow(hwnd , nCmdShow);
UpdateWindow(hwnd);
while(msg.message != WM_QUIT)
{
if(PeekMessageA(&msg,0,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
RenderingDirect3D();
}
}
}
long InitialDirect3D(HWND hwnd)
{
if(!pDirect3D)pDirect3D = Direct3DCreate9(D3D_SDK_VERSION);
D3DDISPLAYMODE Display;
pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT , &Display);
D3DPRESENT_PARAMETERS Direct3DParametr;
ZeroMemory(&Direct3DParametr , sizeof(D3DPRESENT_PARAMETERS));
Direct3DParametr.Windowed = 1;
Direct3DParametr.SwapEffect = D3DSWAPEFFECT_DISCARD;
Direct3DParametr.BackBufferFormat = Display.Format;
pDirect3D->CreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE::D3DDEVTYPE_HAL ,
hwnd ,
D3DCREATE_SOFTWARE_VERTEXPROCESSING ,
&Direct3DParametr ,
&pDirect3DDevice
);
return S_OK;
}
void RenderingDirect3D()
{
pDirect3DDevice->BeginScene();
draw->box(5,5,100,100,0xFFFF0000);
pDirect3DDevice->EndScene();
pDirect3DDevice->Present(0,0,0,0);
}
我渲染简单的方形 - 全部渲染完美。 我需要制作透明窗口(alpha color = ~150)并将此代码添加到WinMain函数:
SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);
SetLayeredWindowAttributes(hwnd, 0, 150, ULW_ALPHA);
全部渲染完美。窗户是透明的。 但我的广场也变得清晰!!!为什么?我的窗口有透明(alpha = 150),正方形有透明(alpha = 255),正方形应该在前面绘制。怎么解决?我需要在带有alpha(150)的透明窗口中绘制正方形alpha(255)。请帮忙。
答案 0 :(得分:0)
其工作方式如下:首先,DirectX渲染你的方块。重要的是要意识到DX不知道或不关心您在窗口上设置的alpha值,因此它会在没有任何alpha信息的情况下呈现它。然后将渲染图像移交给窗口管理器。现在知道你想要一个透明窗口,所以它用你给它的alpha值将缓冲区的内容绘制到屏幕上。但是,由于已经绘制了图形,因此您将获得整个窗口内容的相同alpha值;你不能用这种方法使窗口的某些部分或多或少半透明。