我目前正试图在RenderForm之上实现一个按钮,但是使用RenderControl后,RenderForm就变成了一个没有任何东西的白色画布。
我尝试将所有内容卸载到另一个类中,并使用相同的RenderTarget,但这也不起作用。我现在使用的唯一解决方法是一个自定义MouseMove代码,它读取鼠标指针所在的位置,然后更改自定义ButtonState枚举,以决定应该在按钮的边界上呈现哪些图像。这很慢,在按钮的状态更改为MouseOver之间会有延迟。
编辑:使用MonoGame代替。SharpDX太不稳定了,对任何事情来说都不稳定。
我也能读取和转换C#代码。
主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发布于 2016-01-07 21:56:55
延迟是因为相同的线程用于呈现和更改按钮状态。尝试在按钮工作中使用不同的线程。
简单地尝试多线程以避免延迟。
https://stackoverflow.com/questions/34627628
复制相似问题