gpt4 book ai didi

c# - 如何使用 SharpDX 绘制文本?

转载 作者:太空宇宙 更新时间:2023-11-03 10:21:21 24 4
gpt4 key购买 nike

我在互联网上四处寻找代码,试图找到允许我从常规字体(而不是 Sprite 字体)绘制文本的代码,但只找到了对我毫无帮助的片段。

有人有完整的源文件可以让我使用 SharpDX 和 D3D11/DirectWrite 轻松绘制文本吗?我想制作一个透明的覆盖窗口来显示一些信息(比如 Fraps)。我找到了一个这样做的教程,但它使用了 sprite 字体,我希望它可以在大小和字体方面进行自定义。

这是我目前所拥有的:

using System;
using SharpDX;
using SharpDX.Direct3D11;
using SharpDX.Windows;
using SharpDX.Direct2D1;
using SharpDX.DirectWrite;
using SharpDX.DXGI;

namespace SharpDXRenderer
{
static class Program
{

[STAThread]
static void Main()
{
SharpDX.DirectWrite.TextFormat directWriteTextFormat;
SharpDX.DirectWrite.Factory directWriteFactory;
SharpDX.Direct2D1.SolidColorBrush directWriteFontColor;
SharpDX.Direct2D1.RenderTarget direct2DRenderTarget;
Surface d2dSurface;

RenderForm form = new RenderForm("Example");

var desc = new SwapChainDescription()
{
BufferCount = 1,//buffer count
ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),//sview
IsWindowed = true,
OutputHandle = form.Handle,
SampleDescription = new SampleDescription(1, 0),
SwapEffect = SwapEffect.Discard,
Usage = Usage.RenderTargetOutput
};



var d2dFactory = new SharpDX.Direct2D1.Factory();
d2dSurface = backBuffer.QueryInterface<Surface>();

direct2DRenderTarget = new SharpDX.Direct2D1.RenderTarget(d2dFactory, d2dSurface, new SharpDX.Direct2D1.RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied)));

directWriteFactory = new SharpDX.DirectWrite.Factory();

directWriteTextFormat = new SharpDX.DirectWrite.TextFormat(directWriteFactory, "Calibri", 12) { TextAlignment = SharpDX.DirectWrite.TextAlignment.Leading, ParagraphAlignment = SharpDX.DirectWrite.ParagraphAlignment.Near };

directWriteFontColor = new SharpDX.Direct2D1.SolidColorBrush(direct2DRenderTarget, Color.White);
}
}
}

我知道我缺少一个 SwapChain、一个 Device 和一个 Texture,但我不确定我是否需要更多东西。这个 backBuffer 是我所缺少的,但我不知道它是什么类型。

d2dSurface = backBuffer.QueryInterface<Surface>();

感谢任何有关文本和基本形状绘图的帮助。

提前致谢。

最佳答案

我是 SharpDx 的新手,所以对我的建议持保留态度,但我正在使用 SharpDX.Direct2D1.DeviceContext 在我的应用程序中呈现文本。我的理解是 Direct2D 使文本相对容易呈现。 (至少在 2d 中)https://github.com/spazzarama/Direct3D-Rendering-Cookbook 有很多例子/细节。 (与同名的 SharpDx 书籍相关联)

context2D.DrawText("myTextHere", _textFormat, new RectangleF(0, 0, 100, 100), _gradeLines[ii].ForegroundBrush);

关于c# - 如何使用 SharpDX 绘制文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33376735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com