首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Windows Forms中的DirectX9

Windows Forms中的DirectX9
EN

Stack Overflow用户
提问于 2012-01-06 04:28:35
回答 2查看 1.7K关注 0票数 1

我需要在C++窗体中集成一个DirectX9设备--我该怎么做?

一方面,我拿到了我的DirectX申请……

代码语言:javascript
运行
复制
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窗体

代码语言:javascript
运行
复制
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false); 
Application::Run(gcnew Form1());

我如何合并它?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-06 04:44:14

来自此来源的一些信息:http://www.codeproject.com/KB/directx/Irrational_Thinking.aspx

代码语言:javascript
运行
复制
//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
EN

Stack Overflow用户

发布于 2012-01-06 04:44:35

您可以在窗体的OnLoad()方法重写或加载事件处理程序中初始化DirectX代码。您将需要它的句柄属性:

代码语言:javascript
运行
复制
protected:
    void OnLoad(EventArgs^ e) override {
        HWND hWnd = (HWND)(void*)this->Handle;
        YourDirectXInitializeFunction(hWnd);
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8749305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档