Windows 窗体中的 DirectX9

发布于 2024-12-25 12:44:31 字数 1375 浏览 0 评论 0原文

我需要将 DirectX9 设备集成到 C++ Windows 窗体中 - 我该怎么做?

一方面我得到了我的 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());

我如何合并它?

I need to integrate a DirectX9 device in a C++ Windows Form - how do I do that?

On the one hand I got my DirectX application....

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

and on the other hand I got my Windows Form

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

How do I merge this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人间☆小暴躁 2025-01-01 12:44:31

来自此来源的一些信息: 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
}

Some info from this Source : 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
}
镜花水月 2025-01-01 12:44:31

您可以在表单的 OnLoad() 方法重写或 Load 事件处理程序中初始化 DirectX 代码。您将需要它的 Handle 属性:

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

You can initialize your DirectX code in the form's OnLoad() method override or Load event handler. You'll need its Handle property:

protected:
    void OnLoad(EventArgs^ e) override {
        HWND hWnd = (HWND)(void*)this->Handle;
        YourDirectXInitializeFunction(hWnd);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文