c# - SharpDX、DirectWrite 和 Windows 窗体

标签 c# winforms sharpdx

可以使用 DirectWrite 将文本呈现到 WinForm 应用程序中的 PictureBox 吗?

我正在使用 SharpDX 并浏览了 DirectWrite 示例,试图尽可能构建最简单的工作案例。

我创建了一个表单,并只向其中添加了一个图片框。然后是下面的代码。窗体显示,但 PictureBox 不可见。

任何指导将不胜感激。

谢谢!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
//using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SharpDX.Direct2D1;
using SharpDX.DXGI;
using SharpDX;
using SharpDX.DirectWrite;

using AlphaMode = SharpDX.Direct2D1.AlphaMode;
using Factory = SharpDX.Direct2D1.Factory;

namespace d2dwTextEdit
{
public partial class Form1 : Form
{
    public Factory Factory2D { get; private set; }
    public SharpDX.DirectWrite.Factory FactoryDWrite { get; private set; }
    public WindowRenderTarget RenderTarget2D { get; private set; }
    public SolidColorBrush SceneColorBrush { get; private set; }
    public TextFormat TextFormat { get; private set; }
    public SharpDX.RectangleF ClientRectangle { get; private set; }


    public Form1()
    {
        InitializeComponent();
        Initialize();
        Render();
    }

    protected void Initialize()
    {
        Factory2D = new SharpDX.Direct2D1.Factory();
        FactoryDWrite = new SharpDX.DirectWrite.Factory();

        HwndRenderTargetProperties properties = new HwndRenderTargetProperties();
        properties.Hwnd = pictureBox1.Handle;
        properties.PixelSize = new System.Drawing.Size(pictureBox1.Width, pictureBox1.Height);
        properties.PresentOptions = PresentOptions.None;

        TextFormat = new TextFormat(FactoryDWrite, "Calibri", 30) { TextAlignment = TextAlignment.Center, ParagraphAlignment = ParagraphAlignment.Center };

        RenderTarget2D = new WindowRenderTarget(Factory2D, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)), properties);
        RenderTarget2D.AntialiasMode = AntialiasMode.PerPrimitive;
        RenderTarget2D.TextAntialiasMode = TextAntialiasMode.Cleartype;

        ClientRectangle = new RectangleF(0, 0, pictureBox1.Width, pictureBox1.Height);

        SceneColorBrush = new SolidColorBrush(RenderTarget2D, Colors.White);
        SceneColorBrush.Color = Colors.Black;               


    }

    private void Render()
    {
        RenderTarget2D.Clear(Colors.White);
        RenderTarget2D.DrawText("Hello Marshall", TextFormat, ClientRectangle, SceneColorBrush);
    }

}
}

最佳答案

您好,我有点按照您的问题示例进行操作,但我最终设法解决了问题,因为我想独立开始编写游戏,所有绘制方法都必须在 beginDrawRenderTarget 类的 endDraw 方法'。您还需要实现 RenderLoop 类,这将为您提供回调循环委托(delegate)。为我的代码的粗鲁道歉。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using SharpDX.Direct3D;
using SharpDX.Direct2D1;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using SharpDX.DXGI;
using SharpDX;
using SharpDX.Windows;
using System.Globalization;


using SharpDX.DirectWrite;

namespace dx11
{


    public partial class Form1 : Form
    {
        private SharpDX.Direct3D10.Device _mDevice = null;
        WindowRenderTarget wndRender = null;
        SharpDX.Direct2D1.Factory fact = new SharpDX.Direct2D1.Factory(SharpDX.Direct2D1.FactoryType.SingleThreaded);
        SolidColorBrush scenebrush;
        RenderTargetProperties rndTargProperties;
        HwndRenderTargetProperties hwndProperties;
        SharpDX.Windows.RenderForm form = new RenderForm();
        RenderLoop.RenderCallback callback;

        public Form1()
        {
            InitializeComponent();

            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);

            form.Width = 600;
            form.Height = 600;
            test();
            callback = new RenderLoop.RenderCallback(Render);

            RenderLoop.Run(form, callback);                            
        }

        private void test()
        {
                rndTargProperties = new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied));

                hwndProperties = new HwndRenderTargetProperties();
                hwndProperties.Hwnd = form.Handle;
                hwndProperties.PixelSize = new SharpDX.DrawingSize(form.ClientSize.Width, form.ClientSize.Height);
                hwndProperties.PresentOptions = PresentOptions.None;
                wndRender = new WindowRenderTarget(fact, rndTargProperties, hwndProperties);
                scenebrush = new SolidColorBrush(wndRender, Colors.Red);
               // scenebrush.Color = Colors.Cornsilk;
                form.Show();
        }

        public void Render()
        {    

            wndRender.BeginDraw();
            wndRender.Clear(Colors.DarkBlue);
            wndRender.DrawRectangle(new SharpDX.RectangleF(10, 10, 50, 50), scenebrush, 2.00F);
            wndRender.Flush();
               wndRender.EndDraw();
        }

    }
}

这应该会创建一个左上角带有红色方 block 的蓝色表单。

关于c# - SharpDX、DirectWrite 和 Windows 窗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9863665/

相关文章:

c# - 更改全局程序集缓存 (GAC) 中的 MySql.data 版本

c# - 需要捕捉按下 ListBoxItem 的事件

c# - 服务崩溃,事件名称 [CLR20r3]

c# - ASCII 编码和元音变音和重音

c# - 如何在卸载时从应用程序数据目录中删除文件?

c# - 文件标签不可见

c# - 如何使用 XAudio2 以毫秒为单位寻找位置

.net - 免费的 .NET Windows 控件库?

c# - Winforms 托盘应用程序和 WM_CLOSE

c# - 是否有适用于 Windows 通用应用程序的 SharpDx 模板?