Advertisement
PiToLoKo

Untitled

Mar 10th, 2015
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.10 KB | None | 0 0
  1. Option Strict Off
  2.  
  3. Imports SharpDX
  4. Imports SharpDX.Direct2D1
  5. Imports SharpDX.DirectWrite
  6. Imports SharpDX.Windows
  7.  
  8. Imports D2DFactory = SharpDX.Direct2D1.Factory
  9. Imports DWriteFactory = SharpDX.DirectWrite.Factory
  10. Imports SharpDX.Windows.RenderLoop
  11.  
  12. Public Class Form1
  13.  
  14.     Private Shared d2dFactory As D2DFactory
  15.     Private Shared dwFactory As DWriteFactory
  16.     Private Shared mainForm As RenderForm
  17.  
  18.     Private Shared renderTarget As WindowRenderTarget
  19.  
  20.     Private Shared textFormat As TextFormat
  21.     Private Shared textLayout As TextLayout
  22.  
  23.     'Various brushes for our example
  24.     Private Shared backgroundBrush As SolidColorBrush
  25.     Private Shared defaultBrush As SolidColorBrush
  26.     Private Shared greenBrush As SolidColorBrush
  27.     Private Shared redBrush As SolidColorBrush
  28.  
  29.     Private Shared textRenderer As CustomColorRenderer
  30.  
  31.  
  32.     Private Shared fullTextBackground As RectangleF
  33.  
  34.     'This one is only a measured region
  35.     Private Shared textRegionRect As RectangleF
  36.  
  37.     Private Shared introText As String = "Hello from SharpDX, this is a long text to show some more advanced features like paragraph alignment, custom drawing..."
  38.  
  39.     Private Sub CreateResources()
  40.         If renderTarget IsNot Nothing Then
  41.             renderTarget.Dispose()
  42.         End If
  43.         If defaultBrush IsNot Nothing Then
  44.             defaultBrush.Dispose()
  45.         End If
  46.         If greenBrush IsNot Nothing Then
  47.             greenBrush.Dispose()
  48.         End If
  49.         If redBrush IsNot Nothing Then
  50.             redBrush.Dispose()
  51.         End If
  52.         If backgroundBrush IsNot Nothing Then
  53.             backgroundBrush.Dispose()
  54.         End If
  55.  
  56.         Dim wtp As New HwndRenderTargetProperties()
  57.         wtp.Hwnd = Me.Handle
  58.         wtp.PixelSize = New Size2(mainForm.ClientSize.Width, mainForm.ClientSize.Height)
  59.         wtp.PresentOptions = PresentOptions.Immediately
  60.         renderTarget = New WindowRenderTarget(d2dFactory, New RenderTargetProperties(), wtp)
  61.  
  62.         defaultBrush = New SolidColorBrush(renderTarget, Color.White)
  63.         greenBrush = New SolidColorBrush(renderTarget, Color.Green)
  64.         redBrush = New SolidColorBrush(renderTarget, Color.Red)
  65.         backgroundBrush = New SolidColorBrush(renderTarget, New Color4(0.3F, 0.3F, 0.3F, 0.5F))
  66.  
  67.         textRenderer.AssignResources(renderTarget, defaultBrush)
  68.  
  69.     End Sub
  70.  
  71.     <STAThread>
  72.     Private Sub Form1_Shown(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Shown
  73.  
  74.         mainForm = New RenderForm("Advanced Text rendering demo")
  75.         mainForm.Opacity = 0
  76.  
  77.         d2dFactory = New D2DFactory()
  78.         dwFactory = New DWriteFactory(SharpDX.DirectWrite.FactoryType.Shared)
  79.  
  80.         textRenderer = New CustomColorRenderer()
  81.  
  82.         CreateResources()
  83.  
  84.         Dim bgcolor As Color4 = New Color4(0.1F, 0.1F, 0.1F, 1.0F)
  85.  
  86.         'This is the offset where we start our text layout
  87.         Dim offset As New Vector2(202.0F, 250.0F)
  88.  
  89.         textFormat = New TextFormat(dwFactory, "Arial", FontWeight.Regular, FontStyle.Normal, 16.0F)
  90.         textLayout = New TextLayout(dwFactory, introText, textFormat, 300.0F, 200.0F)
  91.  
  92.         'Apply various modifications to text
  93.         textLayout.SetUnderline(True, New TextRange(0, 5))
  94.         textLayout.SetDrawingEffect(greenBrush, New TextRange(10, 20))
  95.         textLayout.SetFontSize(24.0F, New TextRange(6, 4))
  96.         textLayout.SetFontFamilyName("Comic Sans MS", New TextRange(11, 7))
  97.  
  98.         'Measure full layout
  99.         Dim textSize As TextMetrics = textLayout.Metrics
  100.         fullTextBackground = New RectangleF(textSize.Left + offset.X, textSize.Top + offset.Y, textSize.Width, textSize.Height)
  101.  
  102.         'Measure text to apply background to
  103.         Dim metrics As HitTestMetrics = textLayout.HitTestTextRange(53, 4, 0.0F, 0.0F)(0)
  104.         textRegionRect = New RectangleF(metrics.Left + offset.X, metrics.Top + offset.Y, metrics.Width, metrics.Height)
  105.  
  106.         'Assign render target and brush to our custom renderer
  107.         textRenderer.AssignResources(renderTarget, defaultBrush)
  108.  
  109.         RenderLoop.Run(mainForm, Function()
  110.                                      renderTarget.BeginDraw()
  111.                                      renderTarget.Clear(bgcolor)
  112.  
  113.                                      renderTarget.FillRectangle(fullTextBackground, backgroundBrush)
  114.  
  115.                                      renderTarget.FillRectangle(textRegionRect, redBrush)
  116.  
  117.                                      textLayout.Draw(textRenderer, offset.X, offset.Y)
  118.  
  119.                                      Try
  120.                                          renderTarget.EndDraw()
  121.                                      Catch
  122.                                          CreateResources()
  123.                                      End Try
  124.  
  125.                                  End Function)
  126.  
  127.         d2dFactory.Dispose()
  128.         dwFactory.Dispose()
  129.         renderTarget.Dispose()
  130.  
  131.     End Sub
  132.  
  133.     Public Class CustomColorRenderer
  134.         Inherits SharpDX.DirectWrite.TextRendererBase
  135.         Private renderTarget As RenderTarget
  136.         Private defaultBrush As SolidColorBrush
  137.  
  138.         Public Sub AssignResources(renderTarget As RenderTarget, defaultBrush As SolidColorBrush)
  139.             Me.renderTarget = renderTarget
  140.             Me.defaultBrush = defaultBrush
  141.         End Sub
  142.  
  143.         Public Overrides Function DrawGlyphRun(clientDrawingContext As Object, baselineOriginX As Single, baselineOriginY As Single, measuringMode As MeasuringMode, glyphRun As GlyphRun, glyphRunDescription As GlyphRunDescription, _
  144.             clientDrawingEffect As ComObject) As Result
  145.             Dim sb As SolidColorBrush = defaultBrush
  146.             If clientDrawingEffect IsNot Nothing AndAlso TypeOf clientDrawingEffect Is SolidColorBrush Then
  147.                 sb = DirectCast(clientDrawingEffect, SolidColorBrush)
  148.             End If
  149.  
  150.             Try
  151.                 Me.renderTarget.DrawGlyphRun(New Vector2(baselineOriginX, baselineOriginY), glyphRun, sb, measuringMode)
  152.                 Return Result.Ok
  153.             Catch
  154.                 Return Result.Fail
  155.             End Try
  156.         End Function
  157.     End Class
  158.  
  159. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement