2012-01-05 48 views
1

我需要將DirectX9設備集成到C++ Windows窗體中 - 我該怎麼做?Windows窗體中的DirectX9

一方面,我得到了我的DirectX程序....

HWND hWnd; 
WNDCLASSEX wc; 

ZeroMemory(&wc, sizeof(WNDCLASSEX)); 

wc.cbSize = sizeof(WNDCLASSEX); 
wc.style = CS_HREDRAW | CS_VREDRAW; 
wc.lpfnWndProc = WindowProc; 
wc.hInstance = hInstance; 
wc.hCursor = LoadCursor(NULL, IDC_ARROW); 
wc.lpszClassName = "WindowClass"; 

RegisterClassEx(&wc); 

hWnd = CreateWindowEx(NULL, "WindowClass", "Engine", 
         WS_OVERLAPPEDWINDOW, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 
         NULL, NULL, hInstance, NULL); 

ShowWindow(hWnd, nCmdShow); 

m_D3D = Direct3DCreate9(D3D_SDK_VERSION); 

D3DPRESENT_PARAMETERS d3dpp; 

ZeroMemory(&d3dpp, sizeof(d3dpp)); 
d3dpp.Windowed = TRUE; 
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; 
d3dpp.hDeviceWindow = hWnd; 
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; 
d3dpp.BackBufferWidth = SCREEN_WIDTH; 
d3dpp.BackBufferHeight = SCREEN_HEIGHT; 
d3dpp.EnableAutoDepthStencil = TRUE; 
d3dpp.AutoDepthStencilFormat = D3DFMT_D16; 

// creates device 
m_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &m_Device); 

,並在另一方面我有我的Windows窗體

Application::EnableVisualStyles(); 
Application::SetCompatibleTextRenderingDefault(false); 
Application::Run(gcnew Form1()); 

如何合併呢?

+0

記住你不想創建你的設備,直到實際顯示窗口。在WindowsXP上這將是一個問題。所以通常我會覆蓋OnShown和init。還請記住在Windows Vista-7上使用D3D9_EX設備,因爲它可以正確處理資源。 – zezba9000 2012-01-25 22:50:49

回答

1

從這個來源的一些信息:http://www.codeproject.com/KB/directx/Irrational_Thinking.aspx

//File: DX9Form.cpp 
#include "DX9Form.h" 

using namespace Forms_DX9; 
using namespace globals; 

void DX9Form::initD3D(HWND hHandle) 
{ 

    globals::d3d = Direct3DCreate9(D3D_SDK_VERSION); 
    // create the Direct3D interface 

    D3DPRESENT_PARAMETERS d3dpp; 
    // create a struct to hold various device information 

    ZeroMemory(&d3dpp, sizeof(d3dpp)); // clear out the struct for use 

    d3dpp.Windowed = true; // program windowed, not fullscreen 

    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // discard old frames 

    d3dpp.hDeviceWindow = hHandle; 
    // set the window to be used by Direct3D 

    // create a device class using this information 
    // and information from the d3dpp stuct 
    globals::d3d->CreateDevice(D3DADAPTER_DEFAULT, 
      D3DDEVTYPE_HAL, 
      hHandle, 
      D3DCREATE_SOFTWARE_VERTEXPROCESSING, 
      &d3dpp, 
      &globals::d3ddev); 
} 

void DX9Form::render(void) 
{ 

    // clear the window to a deep blue 
    globals::d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, 
           D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0); 
    globals::d3ddev->BeginScene(); // begins the 3D scene 

    // do 3D rendering on the back buffer here 
    globals::d3ddev->EndScene(); // ends the 3D scene 
    globals::d3ddev->Present(NULL, NULL, NULL, NULL); 
    // displays the created frame 
} 
1

您可以在窗體的OnLoad()方法重寫或Load事件處理程序初始化的DirectX代碼。你需要它的Handle屬性:

protected: 
    void OnLoad(EventArgs^ e) override { 
     HWND hWnd = (HWND)(void*)this->Handle; 
     YourDirectXInitializeFunction(hWnd); 
    } 
+0

我試過這種方法,但是我沒有得到我的directX cpp代碼運行,因爲它沒有被管理:( – Schifty 2012-01-05 21:58:50

+0

沒關係,C++/CLI沒有問題調用本地代碼,這是使用它的唯一原因。你做錯了什麼,但根本不留下任何麪包屑來猜測什麼,甚至沒有錯誤信息,更新你的問題。 – 2012-01-05 22:00:49