( This question is based on further investigations of this other question, but isn't the same question, this is very specific question about painting issues. )
I'm trying to draw a transparent surface overlapped on a target window, the problem is that I don't know how to paint it transparent, so by the moment my surface is black, and I cannot see the proper way to clear the black color of that surface in the code below.
I'd read about pixelformats and alphamodes, however, seems I cannot use the AlphaMode.Straight
which supposedly is for allow transparency.
I'm aware of a freeware application that can do this, its name is TurboHUD (an application that draws a transparent surface on the window of a game client to draw objects, that is, a HUD). To be honest and maybe ridiculous: I'm trying to acchieve this from more than two years ago, I still don't know how to start doing this by doing the transparency I need to start drawing objects on a transparent surface.
What I'm doing wrong?. This sample code is written in VB.NET, but I accept too a solution in C#.
Imports SharpDX
Imports SharpDX.Direct2D1
Imports SharpDX.Direct3D
Imports SharpDX.DXGI
Imports SharpDX.Mathematics.Interop
Imports SharpDX.Windows
Public NotInheritable Class Form1 : Inherits Form
Private factory As New Direct2D1.Factory(Direct2D1.FactoryType.SingleThreaded)
Private render As WindowRenderTarget
Private renderProps As HwndRenderTargetProperties
Private renderThread As Thread = Nothing
Private Sub Form1_Load() Handles MyBase.Shown
Dim hwnd As IntPtr = Process.GetProcessesByName("notepad").Single().MainWindowHandle
Me.renderProps = New HwndRenderTargetProperties()
Me.renderProps.Hwnd = hwnd
Me.renderProps.PixelSize = New Size2(1920, 1080)
Me.renderProps.PresentOptions = PresentOptions.None
Me.render = New WindowRenderTarget(Me.factory, New RenderTargetProperties(New PixelFormat(Format.B8G8R8A8_UNorm, Direct2D1.AlphaMode.Premultiplied)), Me.renderProps)
Me.renderThread = New Thread(New ParameterizedThreadStart(AddressOf Me.DoRender))
Me.renderThread.Priority = ThreadPriority.Normal
Me.renderThread.IsBackground = True
Me.renderThread.Start()
End Sub
Private Sub DoRender(ByVal sender As Object)
While True
Me.render.BeginDraw()
' Me.render.Clear(New RawColor4(0, 0, 0, 0))
Me.render.Clear(SharpDX.Color.Transparent)
Me.render.Flush()
Me.render.EndDraw()
End While
End Sub
End Class
The code above is a VB.NET adaption of the accepted answer of this question.
Straight
alpha mode is not supported when dealing with theID2D1HwndRenderTarget
interface (which I guess SharpDX is using). – SnowbirdDwmExtendFrameIntoClientArea
with the proper size in the corresponding margins struct and I still see a black background on the target window. The kind of hwnd render-target I'm using seems does not have an option/property to set the default color ...or I can't find it, so I can't verify the color is black, but I'm trying to "clear" black so I see a black surface. – EpiscopalismID2D1BitmapRenderTarget
orID2D1RenderTarget
instead,then the constructors of the SharpDX's wrapper class (BitmapRenderTarget
andWicRenderTarget
I supose) takes as parameter another (base)RenderTarget
class!!,so I'm trying to instantiate a class that inherits fromRenderTarget
but its constructor asks me for anotherRenderTarget
instance? I don't understand it;also I can pass a "native pointer" but I don't know what it means in this DirectX meaning.I'm totally lost. – EpiscopalismID2D1HwndRenderTarget
instance. In VB.NET/C# this will be anIntPtr
. – Snowbird