I am currently trying to implement a button on top of a RenderForm
, but upon using a RenderControl
, the RenderForm
becomes a white canvas without anything.
I tried offloading everything to another class and using the same RenderTarget
, but that didn't work as well. The only workaround I'm using right now is a custom MouseMove
code that reads where the mouse pointer is, then changes a custom ButtonState
enum that decides what images should be rendered in the button's bounds. This is slow and has a delay between when the button's state is changed to MouseOver
.
EDIT: Used MonoGame instead. SharpDX is too unstable for anything at all.
P.S. I can read and convert C# code too.
Code for main RenderForm:
Imports SharpDX
Imports SharpDX.Direct2D1
Imports SharpDX.Direct3D
Imports SharpDX.Direct3D11
Imports SharpDX.DirectWrite
Imports SharpDX.DXGI
Imports SharpDX.IO
Imports SharpDX.WIC
Imports SharpDX.Windows
Imports Device = SharpDX.Direct3D11.Device
Imports FactoryD2D = SharpDX.Direct2D1.Factory
Imports FactoryDXGI = SharpDX.DXGI.Factory1
Imports FactoryDW = SharpDX.DirectWrite.Factory
Imports PixelFormat = SharpDX.Direct2D1.PixelFormat
Imports Bitmap = SharpDX.Direct2D1.Bitmap
Imports PointD = SharpDX.Point
Imports Point = System.Drawing.Point
Imports Font = System.Drawing.Font
Imports Storytime.GameEnums
Imports Storytime.GraphicsEngine
Public Class Canvas2
Inherits RenderForm
Public SwapChainDesc As New SwapChainDescription()
Public Device As Device
Public SwapChain As SwapChain
Public BackBuffer As Surface
Public RenderTarget As RenderTarget
Private Sub Canvas2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim XButton1 As New XButton
XButton1.[Text] = "samplesamplesamplesamplesamplesamplesample"
With SwapChainDesc
.BufferCount = 2
.Usage = Usage.RenderTargetOutput
.OutputHandle = Handle
.IsWindowed = True
.ModeDescription = New ModeDescription(0, 0, New Rational(120, 1), Format.R8G8B8A8_UNorm)
.SampleDescription = New SampleDescription(1, 0)
.Flags = SwapChainFlags.AllowModeSwitch
.SwapEffect = SwapEffect.Sequential
End With
Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, SwapChainDesc, Device, SwapChain)
BackBuffer = Surface.FromSwapChain(SwapChain, 0)
Using Factory As New FactoryD2D()
Dim DPI = Factory.DesktopDpi
Dim RenderTargetProp As New RenderTargetProperties()
With RenderTargetProp
.DpiX = DPI.Height
.DpiY = DPI.Width
.MinLevel = Direct2D1.FeatureLevel.Level_DEFAULT
.PixelFormat = New PixelFormat(Format.Unknown, AlphaMode.Premultiplied)
.Type = RenderTargetType.Default
.Usage = RenderTargetUsage.None
End With
RenderTarget = New RenderTarget(Factory, BackBuffer, RenderTargetProp)
End Using
SwapChain.GetParent(Of FactoryDXGI)().MakeWindowAssociation(Handle, WindowAssociationFlags.IgnoreAltEnter)
With RenderTarget
.AntialiasMode = AntialiasMode.PerPrimitive
.TextAntialiasMode = TextAntialiasMode.Cleartype
End With
RenderLoop.Run(Me, AddressOf DrawCanvas)
Controls.Add(XButton1)
RenderTarget.Dispose()
SwapChain.Dispose()
Device.Dispose()
End Sub
Delay is because same thread is used for rendering and change of button state. Try to use different thread for your button work.
In simple Try multi threading to avoid delay.