如果我的怀疑是愚蠢的或愚蠢的,请原谅我。我对 DirectX 编程完全陌生。只要有 C++ 知识(非常基本的 COM 知识)。
以下代码示例来自 MSDN Creating D3D device其中解释了如何从头开始创建 D3D 设备。
MyDoubt 是:
Here the function "pD3D->CreateDeviceEx()" takes in a parameter HWND hwnd. What if I am trying to create a D3D device from a commadline C++ win32 app where I need to use some of the functions in D3D device's interfaces. How do I get the HWND field. In this case how do I create D3D device. PLease explain in detail.
HRESULT InitD3D9Ex( /* IN */ HWND hWnd, /* OUT */ IDirect3DDevice9Ex ** ppD3DDevice )
{
HRESULT hr = E_FAIL;
IDirect3D9Ex * pD3D = NULL;
IDirect3DDevice9Ex * pDevice = NULL;
if(ppD3DDevice == NULL)
{
return hr;
}
// Create the D3D object, which is needed to create the D3DDevice.
if(FAILED(hr = Direct3DCreate9Ex( D3D_SDK_VERSION, &pD3D )))
{
*ppD3DDevice = NULL;
return hr;
}
// Set up the structure used to create the D3DDevice.
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
// Create the Direct3D device.
if( FAILED( hr = pD3D->CreateDeviceEx( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, NULL, &pDevice ) ) )
{
*ppD3DDevice = NULL;
return hr;
}
// Device state would normally be set here
*ppD3DDevice = pDevice;
return hr;
}
最佳答案
在 Windows 中,所有视觉对象都由窗口句柄控制。您不能创建 D3D“设备”并将其附加到“无”。您必须将“D3D 设备”与某个窗口(您自己的窗口或桌面)相关联。
你的控制台窗口是由系统创建的,你无法控制它的创建标志,所以即使你使用 GetConsoleWindow功能,您不能在 Direct3D 设备创建功能中使用此 HWND(这可能随着 Aero 的引入而改变)。
您无法避免在您的控制台应用程序中创建另一个窗口句柄。使用 RegisterWindowClass
和 CreateWindow
函数创建一个新窗口或找到您桌面的句柄(我怀疑您会想要那个)。
关于c++ - 创建没有 HWND 输入参数的 D3D 设备到 MSFT CreateDevice() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13137047/