Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Strict Off
- Imports SharpDX
- Imports SharpDX.Direct2D1
- Imports SharpDX.DirectWrite
- Imports SharpDX.Windows
- Imports D2DFactory = SharpDX.Direct2D1.Factory
- Imports DWriteFactory = SharpDX.DirectWrite.Factory
- Imports SharpDX.Windows.RenderLoop
- Public Class Form1
- Private Shared d2dFactory As D2DFactory
- Private Shared dwFactory As DWriteFactory
- Private Shared mainForm As RenderForm
- Private Shared renderTarget As WindowRenderTarget
- Private Shared textFormat As TextFormat
- Private Shared textLayout As TextLayout
- 'Various brushes for our example
- Private Shared backgroundBrush As SolidColorBrush
- Private Shared defaultBrush As SolidColorBrush
- Private Shared greenBrush As SolidColorBrush
- Private Shared redBrush As SolidColorBrush
- Private Shared textRenderer As CustomColorRenderer
- Private Shared fullTextBackground As RectangleF
- 'This one is only a measured region
- Private Shared textRegionRect As RectangleF
- Private Shared introText As String = "Hello from SharpDX, this is a long text to show some more advanced features like paragraph alignment, custom drawing..."
- Private Sub CreateResources()
- If renderTarget IsNot Nothing Then
- renderTarget.Dispose()
- End If
- If defaultBrush IsNot Nothing Then
- defaultBrush.Dispose()
- End If
- If greenBrush IsNot Nothing Then
- greenBrush.Dispose()
- End If
- If redBrush IsNot Nothing Then
- redBrush.Dispose()
- End If
- If backgroundBrush IsNot Nothing Then
- backgroundBrush.Dispose()
- End If
- Dim wtp As New HwndRenderTargetProperties()
- wtp.Hwnd = Me.Handle
- wtp.PixelSize = New Size2(mainForm.ClientSize.Width, mainForm.ClientSize.Height)
- wtp.PresentOptions = PresentOptions.Immediately
- renderTarget = New WindowRenderTarget(d2dFactory, New RenderTargetProperties(), wtp)
- defaultBrush = New SolidColorBrush(renderTarget, Color.White)
- greenBrush = New SolidColorBrush(renderTarget, Color.Green)
- redBrush = New SolidColorBrush(renderTarget, Color.Red)
- backgroundBrush = New SolidColorBrush(renderTarget, New Color4(0.3F, 0.3F, 0.3F, 0.5F))
- textRenderer.AssignResources(renderTarget, defaultBrush)
- End Sub
- <STAThread>
- Private Sub Form1_Shown(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Shown
- mainForm = New RenderForm("Advanced Text rendering demo")
- mainForm.Opacity = 0
- d2dFactory = New D2DFactory()
- dwFactory = New DWriteFactory(SharpDX.DirectWrite.FactoryType.Shared)
- textRenderer = New CustomColorRenderer()
- CreateResources()
- Dim bgcolor As Color4 = New Color4(0.1F, 0.1F, 0.1F, 1.0F)
- 'This is the offset where we start our text layout
- Dim offset As New Vector2(202.0F, 250.0F)
- textFormat = New TextFormat(dwFactory, "Arial", FontWeight.Regular, FontStyle.Normal, 16.0F)
- textLayout = New TextLayout(dwFactory, introText, textFormat, 300.0F, 200.0F)
- 'Apply various modifications to text
- textLayout.SetUnderline(True, New TextRange(0, 5))
- textLayout.SetDrawingEffect(greenBrush, New TextRange(10, 20))
- textLayout.SetFontSize(24.0F, New TextRange(6, 4))
- textLayout.SetFontFamilyName("Comic Sans MS", New TextRange(11, 7))
- 'Measure full layout
- Dim textSize As TextMetrics = textLayout.Metrics
- fullTextBackground = New RectangleF(textSize.Left + offset.X, textSize.Top + offset.Y, textSize.Width, textSize.Height)
- 'Measure text to apply background to
- Dim metrics As HitTestMetrics = textLayout.HitTestTextRange(53, 4, 0.0F, 0.0F)(0)
- textRegionRect = New RectangleF(metrics.Left + offset.X, metrics.Top + offset.Y, metrics.Width, metrics.Height)
- 'Assign render target and brush to our custom renderer
- textRenderer.AssignResources(renderTarget, defaultBrush)
- RenderLoop.Run(mainForm, Function()
- renderTarget.BeginDraw()
- renderTarget.Clear(bgcolor)
- renderTarget.FillRectangle(fullTextBackground, backgroundBrush)
- renderTarget.FillRectangle(textRegionRect, redBrush)
- textLayout.Draw(textRenderer, offset.X, offset.Y)
- Try
- renderTarget.EndDraw()
- Catch
- CreateResources()
- End Try
- End Function)
- d2dFactory.Dispose()
- dwFactory.Dispose()
- renderTarget.Dispose()
- End Sub
- Public Class CustomColorRenderer
- Inherits SharpDX.DirectWrite.TextRendererBase
- Private renderTarget As RenderTarget
- Private defaultBrush As SolidColorBrush
- Public Sub AssignResources(renderTarget As RenderTarget, defaultBrush As SolidColorBrush)
- Me.renderTarget = renderTarget
- Me.defaultBrush = defaultBrush
- End Sub
- Public Overrides Function DrawGlyphRun(clientDrawingContext As Object, baselineOriginX As Single, baselineOriginY As Single, measuringMode As MeasuringMode, glyphRun As GlyphRun, glyphRunDescription As GlyphRunDescription, _
- clientDrawingEffect As ComObject) As Result
- Dim sb As SolidColorBrush = defaultBrush
- If clientDrawingEffect IsNot Nothing AndAlso TypeOf clientDrawingEffect Is SolidColorBrush Then
- sb = DirectCast(clientDrawingEffect, SolidColorBrush)
- End If
- Try
- Me.renderTarget.DrawGlyphRun(New Vector2(baselineOriginX, baselineOriginY), glyphRun, sb, measuringMode)
- Return Result.Ok
- Catch
- Return Result.Fail
- End Try
- End Function
- End Class
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement