2012-03-25 4 views
3

DirectWrite를 사용하여 WinForm 응용 프로그램의 PictureBox에 텍스트를 렌더링 할 수 있습니까?SharpDX, DirectWrite 및 Windows Forms

저는 SharpDX를 사용하고 있으며 가능한 가장 간단한 작업 케이스를 만들기 위해 DirectWrite 샘플을 작성했습니다.

Form을 만들고 pictureBox 만 추가했습니다. 다음 코드. 양식은 표시되지만 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); 
    } 

} 
} 
+0

창이 보이기 전에 아무것도 렌더링하지 않습니다. 표시된 이벤트는 창을 표시하기 위해 제기 된 첫 번째 이벤트입니다. 대신 Paint 이벤트를 사용하십시오. –

+0

도와 줘서 고마워, 한스. 불행히도 ... 기쁨이 없습니다. pictureBox와 폼 자체에 대한 Render()에 대한 호출 핸들러를 Paint 핸들러에 넣었습니다. 아무런 변화가 없습니다. – vocalionecho

답변

6

안녕하세요 저는 당신의 질문을 따르고 있습니다.하지만 저는 게임을 독립적으로 시작하기 위해 마침내 그것을 다룰 수있었습니다. 모든 그리기 방법은 RenderTarget 클래스의 beginDrawendDraw 클래스 내에 있어야합니다. ' 당신은 또한 RenderLoop 클래스를 구현해야합니다. 이것은 콜백 루프 델리게이트를 줄 것입니다. 내 코드의 거룩함에 대한 사과.

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(); 
     } 

    } 
} 

이것은 왼쪽 상단 구석에 빨간색 사각형이있는 파란색 양식을 만들어야합니다.

1

HwndRenderTarget을 사용하여 실제로 "PictureBox에"렌더링하고 싶지 않습니다. 실제로는 PictureBox에 렌더링 작업이 있음을 비트 맵에 렌더링하려고합니다. "공유 비트 맵"(SharpDX에 익숙하지 않지만 기본 메서드는 ID2D1RenderTarget :: CreateSharedBitmap())을 렌더링하고 EndDraw()를 호출 한 후 PictureBox를 무효화하면됩니다.