首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用SharpDX绘制文本?

如何使用SharpDX绘制文本?
EN

Stack Overflow用户
提问于 2015-10-28 03:19:15
回答 1查看 4.5K关注 0票数 2

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

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

这就是我到目前为止所知道的:

代码语言:javascript
运行
复制
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,一部设备,我想是一种质感,但我不确定我是否需要更多的东西。这个backBuffer是我缺少的,但我不知道它是什么类型。

代码语言:javascript
运行
复制
d2dSurface = backBuffer.QueryInterface<Surface>();

任何关于文本和基本形状绘制的帮助都是值得感谢的。

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2016-01-23 05:13:10

我对SharpDx完全陌生,所以对我的建议持保留态度,但我使用SharpDX.Direct2D1.DeviceContext在我的应用程序中呈现文本。我的理解是,是Direct2D使得文本相对容易呈现。(至少在2d中)在https://github.com/spazzarama/Direct3D-Rendering-Cookbook上有很多示例/细节(它以相同的名称与SharpDx书绑定)

代码语言:javascript
运行
复制
context2D.DrawText("myTextHere", _textFormat, new RectangleF(0, 0, 100, 100), _gradeLines[ii].ForegroundBrush);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33376735

复制
相关文章

相似问题

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