2012-03-25 30 views
3

DirectWriteを使用してWinFormアプリケーションのPictureBoxにテキストをレンダリングできますか?SharpDX、DirectWriteおよびWindowsフォーム

私はSharpDXを使用していますが、DirectWriteのサンプルを読んで、できる限り簡単なケースを構築しようとしています。

私はフォームを作成し、それに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

助けてくれてありがとう、ハンス。残念ながら...喜びはありません。私はpaintBoxとフォーム自体の両方のPaintハンドラにRender()への呼び出しを行い、変更はありません。 – 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を使用して「ピクチャボックスに」レンダリングする必要はありません。実際には、ピクチャボックスがレンダリングの仕事を持っていることをビットマップにレンダリングする必要があります。私はSharpDXに特に精通していませんが、基本的な方法はID2D1RenderTarget :: CreateSharedBitmap()です。それをレンダリングし、EndDraw()を呼び出した後にPictureBoxを無効にします。

関連する問題