【发布时间】:2014-08-22 02:38:23
【问题描述】:
我需要从 directx3d 获取设备上下文 (DC)。这里有一些代码快照。
1.创建设备:
int windowWidth = 640;
int windowHeight = 480;
IDirect3D9* direct3D9 = Direct3DCreate9(D3D_SDK_VERSION);
if(direct3D9 == NULL)
{
return FALSE;
}
D3DDISPLAYMODE *d3ddisplayMode =(D3DDISPLAYMODE *)calloc(1,sizeof(D3DDISPLAYMODE));
hr = direct3D9->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,d3ddisplayMode);
if(hr != D3D_OK)
{
free(d3ddisplayMode);
direct3D9->Release();
return FALSE;
}
D3DPRESENT_PARAMETERS *d3dpresentParam =(D3DPRESENT_PARAMETERS*)calloc(1,sizeof(D3DPRESENT_PARAMETERS));
d3dpresentParam->Windowed = TRUE;
d3dpresentParam->hDeviceWindow = NULL;
d3dpresentParam->SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpresentParam->BackBufferFormat = d3ddisplayMode->Format;
d3dpresentParam->BackBufferWidth = windowWidth;
d3dpresentParam->BackBufferHeight = windowHeight;
d3dpresentParam->BackBufferCount = 1;
free(d3ddisplayMode);
hr = direct3D9->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,NULL,D3DCREATE_SOFTWARE_VERTEXPROCESSING,d3dpresentParam,&direct3D9Device);
2.CRETAE 纹理:
hr = D3DXCreateTexture(direct3D9Device,bmpWidth,bmpHeight,1,0,D3DFMT_X8R8G8B8,D3DPOOL_MANAGED,&pTexture);
3.显示图像:
float left = 0,top =0,width =640,height=480;
direct3D9Device->BeginScene();
D3DXMATRIX mat;
D3DXVECTOR3 pos;
pos.x = (bmpWidth * left) / width;
pos.y = (bmpHeight * top) / height;
pos.z = 0;
d3dxSprite->Begin(D3DXSPRITE_ALPHABLEND);
D3DXVECTOR2 scaling((width/bmpWidth),(height/bmpHeight));
if(pTexture == direct3DTextureRemote )
{
D3DXVECTOR2 spriteCentre((width/2),(height/2));
D3DXMatrixTransformation2D(&mat,NULL,0.0,&scaling,&spriteCentre,NULL,NULL);
}
else
{
D3DXMatrixTransformation2D(&mat,NULL,0.0,&scaling,NULL,NULL,NULL);
}
d3dxSprite->SetTransform(&mat);
d3dxSprite->Draw(pTexture,NULL,NULL,&pos,0xFFFFFFFF);
d3dxSprite->End();
direct3D9Device->EndScene();
direct3D9Device->Present( NULL, NULL, NULL, NULL );
现在正在调查中。我可以从像 HDC hdc = ::GetDC(hwnd) 这样的窗口获取 dc,但在我的情况下,如果没有窗口(即无窗口),那么我如何从 directx 获取 DC。请给一些代码从directx设备获取DC。
【问题讨论】:
-
您向
CreateDevice提供了HWND,为什么不使用它? -
在上面的示例中,我向 createDevice 提供了 hwnd。但现在我需要在无窗口部分中绘制图像。所以我只问如何从 directx 获取 Dc。
-
@user3336737 你认为如何“在无窗口部分绘制图像”?那是什么部分?你到底想达到什么目标?
-
提供
NULL作为Present()的窗口句柄只是意味着它将使用来自D3DPRESENT_PARAMETERS的HWND。您可能必须解释您要达到的目标。 -
嗨@Drop:我正在开发一个用于绘图的NPAPI无窗口插件。所以我不需要创建一个窗口。所以我在 createDevice 中提供了 NULL。我的问题是“我们可以从 directx 设备获取 Dc 吗?” .如果有可能获得 Dc,那么我将使用 DC 从 NPP_HandleEvent 中绘制图像。