在 SharpDX 项目的 RenderForm 上使用 RenderControl

Using a RenderControl on a RenderForm on a SharpDX Project

我目前正在尝试在 RenderForm 上实现一个按钮,但是在使用 RenderControl 后,RenderForm 变成了白色的 canvas,没有任何东西。

我尝试将所有内容卸载到另一个 class 并使用相同的 RenderTarget,但效果不佳。我现在使用的唯一解决方法是自定义 MouseMove 代码,它读取鼠标指针的位置,然后更改自定义 ButtonState 枚举,该枚举决定应在按钮的边界内呈现哪些图像。这很慢,并且在按钮状态更改为 MouseOver.

之间有延迟

编辑:改为使用 MonoGame。 SharpDX 太不稳定了。

P.S。我也可以阅读和转换 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

延迟是因为同一个线程用于呈现和更改按钮状态。尝试为您的按钮工作使用不同的线程。

简单尝试多线程避免延迟。