Asik
Asik

Reputation: 22123

Creating a dummy HWND to pass to Direct3D

I'm trying to translate the D3DImage sample to pure C# with SharpDX. Direct3D requires a HWND to initialize, and this is how it's done in C++:

WNDCLASSEX g_wc = { sizeof(WNDCLASSEX), CS_CLASSDC, nullptr, 0L, 0L,
      GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, L"Foo", nullptr };
HWND g_windowHandle;

// Create invisible window to get HWND
RegisterClassEx(&g_wc);
g_windowHandle = CreateWindow(L"Foo", L"Foo", WS_OVERLAPPEDWINDOW, 
    0, 0, 0, 0, nullptr, nullptr, nullptr, nullptr);

    // then eventually we can create the device
Direct3DCreate9Ex(D3D_SDK_VERSION, &m_d3d);
m_d3d->CreateDeviceEx(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_windowHandle,
    vertexProcessing | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
    &d3dpp, nullptr, &m_d3dDevice);

What would be the best way of obtaining this dummy HWND in C#? It must be different from the main window's HWND. My application is a WPF project.

Upvotes: 3

Views: 2338

Answers (1)

meklarian
meklarian

Reputation: 6625

The documentation for IDirect3D9Ex::CreateDeviceEx says the HWND parameter is optional if you are in windowed mode and your presentation HWND in your D3DPRESENT_PARAMETERS structure is set (late edit: oops, can't be blank).

From IDirect3D9Ex::CreateDeviceEx @ MSDN

hFocusWindow [in]
Type: HWND
The focus window alerts Direct3D when an application switches from foreground mode to background mode. For full-screen mode, the window specified must be a top-level window. For windowed mode, this parameter may be NULL only if the hDeviceWindow member of pPresentationParameters is set to a valid, non-NULL value.

D3DPRESENT_PARAMETERS struct @ MSDN

To get a window handle for use, you can make another top-level window via regular .NET framework methods and grab its window handle instead of going through the motions with Win32 method calls.

If you find can't use the Handle provided by WPF (per Hans Passant's comment), you can also make a dummy Windows Form and instantiate it as a hidden window, and use its handle instead.

WindowInteropHelper (WPF) @ MSDN

Control.Handle (Windows Forms) @ MSDN

Footnote: Your existing WPF main window handle is probably fine unless some mechanism in SharpDX or an existing Viewport3D has a conflict with using Direct3D9 in this manner.

3-D Graphics Overview (WPF) @ MSDN
Viewport3D Class (WPF) @ MSDN

Upvotes: 2

Related Questions