有一个不错的 DirectWrite 包装器,名为 http://fw1.codeplex.com/
它在 c++ 中,但是为它制作一个混合模式包装器非常简单(这是我为我的 c# 项目所做的)。
这是一个简单的例子:
.h 文件
#pragma once
#include "Lib/FW1FontWrapper.h"
using namespace System::Runtime::InteropServices;
public ref class DX11FontWrapper
{
public:
DX11FontWrapper(SlimDX::Direct3D11::Device^ device);
void Draw(System::String^ str,float size,int x,int y,int color);
private:
SlimDX::Direct3D11::Device^ device;
IFW1FontWrapper* pFontWrapper;
};
.cpp 文件
#include "StdAfx.h"
#include "DX11FontWrapper.h"
DX11FontWrapper::DX11FontWrapper(SlimDX::Direct3D11::Device^ device)
{
this->device = device;
IFW1Factory *pFW1Factory;
FW1CreateFactory(FW1_VERSION, &pFW1Factory);
ID3D11Device* dev = (ID3D11Device*)device->ComPointer.ToPointer();
IFW1FontWrapper* pw;
pFW1Factory->CreateFontWrapper(dev, L"Arial", &pw);
pFW1Factory->Release();
this->pFontWrapper = pw;
}
void DX11FontWrapper::Draw(System::String^ str,float size,int x,int y, int color)
{
ID3D11DeviceContext* pImmediateContext =
(ID3D11DeviceContext*)this->device->ImmediateContext->ComPointer.ToPointer();
void* txt = (void*)Marshal::StringToHGlobalUni(str);
pFontWrapper->DrawString(pImmediateContext, (WCHAR*)txt, size, x, y, color, 0);
Marshal::FreeHGlobal(System::IntPtr(txt));
}