当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - draw use directx 9 in transparent window

问题描述:

Please help me gues.

I create simple window(use c++ winapi) and initial direct device:

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);

}

I Renderer simple square - all rendered perfectly.

I need make transparent window (alpha color = ~150 ) and i add this code to WinMain function:

SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);

SetLayeredWindowAttributes(hwnd, 0, 150, ULW_ALPHA);

All rendered perfectly. The window was transparent.

But my square became clear too!!!Why? My window have transparent(alpha = 150) , square have transparent(alpha = 255) , the square should be drawn in front. How to fix it? I need draw the square whith alpha(255) in transparent window with alpha(150). Please help.

网友答案:

The way this works is the following: first, DirectX renders your square. It's important to realize that DX does not know or care about the alpha value that you set on your window, so it renders it without any alpha information. The rendered image is then handed over to the window manager. This now knows that you want to have a transparent window, so it draws the contents of the buffer onto the screen with the alpha value you gave it. However, since the graphics are already drawn, you will get the same alpha value for your entire window contents; you can't make some parts of the window more or less translucent with this method.

分享给朋友:
您可能感兴趣的文章:
随机阅读: