首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SharpDX、DirectWrite和Windows窗体

SharpDX、DirectWrite和Windows窗体
EN

Stack Overflow用户
提问于 2012-03-26 04:17:54
回答 2查看 8.1K关注 0票数 3

可以在WinForm应用程序中使用DirectWrite向PictureBox呈现文本吗?

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

我创建了一个表单,并且只向其中添加了一个pictureBox。然后是下面的代码。将显示表单,但PictureBox中看不到任何内容。

任何指导都将不胜感激。

谢谢!

代码语言:javascript
运行
复制
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);
    }

}
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-14 20:23:48

嗨,我有点像你的问题例子,但我终于设法解决了,因为我想独立地开始编写游戏,所有的绘制方法都必须在RenderTarget类的beginDrawendDraw方法中。您还需要实现RenderLoop类,这将为您提供回调循环委托。很抱歉我的代码太粗糙了。

代码语言:javascript
运行
复制
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();
        }

    }
}

这将创建一个在左上角带有红色正方形的蓝色窗体。

票数 6
EN

Stack Overflow用户

发布于 2012-04-11 02:55:15

实际上,您并不想使用HwndRenderTarget呈现“到PictureBox中”。实际上,您希望渲染到PictureBox负责渲染的位图中。您可以通过创建一个“共享位图”(我对SharpDX不是特别熟悉,但底层方法是ID2D1RenderTarget::CreateSharedBitmap()),渲染到其中,然后在调用EndDraw()之后使PictureBox无效。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9863665

复制
相关文章

相似问题

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