Quantcast
Viewing all articles
Browse latest Browse all 27059

Use DirextX to draw on another form

Based on a question I asked earlier, I did some work an managed to draw a line on my form using DirectX. This is the code if anyone is interested:

Code:


Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Public Class Form1
    Dim device As Direct3D.Device
    Dim buffer As VertexBuffer
    Private Function GetHandle() As IntPtr
        Dim p As Process = Process.GetProcessesByName("SA RS Tools")(0)
        Return p.Handle
    End Function

    Private Sub initilizdx()
        Dim present As New PresentParameters
        present.BackBufferCount = 1
        present.BackBufferFormat = Manager.Adapters(0).CurrentDisplayMode.Format
        present.BackBufferHeight = Me.Height
        present.BackBufferWidth = Me.Width
        present.Windowed = True
        present.SwapEffect = SwapEffect.Discard
        device = New Device(0, DeviceType.Hardware, GetHandle(), CreateFlags.SoftwareVertexProcessing, present)
        device.RenderState.CullMode = Cull.None
    End Sub
    Private Sub creat_vertxbuffer()
        buffer = New VertexBuffer(GetType(CustomVertex.TransformedColored), 2, device, Usage.None, CustomVertex.TransformedColored.Format, Pool.Managed)
        Dim ver(1) As CustomVertex.TransformedColored
        ver(0) = New CustomVertex.TransformedColored(10, 50, 0, 1, Color.Red.ToArgb)
        ver(1) = New CustomVertex.TransformedColored(100, 50, 0, 1, Color.Green.ToArgb)
        buffer.SetData(ver, 0, LockFlags.None)
    End Sub
    Private Sub draw_me()
        device.Clear(ClearFlags.Target, Color.Transparent, 0, 0)
        device.BeginScene()
        device.VertexFormat = CustomVertex.TransformedColored.Format
        device.SetStreamSource(0, buffer, 0)
        device.DrawPrimitives(PrimitiveType.LineStrip, 0, 1)
        device.EndScene()
        device.Present()

    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        initilizdx()
        creat_vertxbuffer()
        draw_me()
    End Sub
End Class

Now based on this, whenever I wrote Me.Handle instead of GetHandle() on the following line:

Code:

device = New Device(0, DeviceType.Hardware, GetHandle, CreateFlags.SoftwareVertexProcessing, present)
It seemed to work and draw the line on my own form.

But, as you might know I want to draw it on a another form and therefore I aquired that handle. Anyone got any ideas why it doesn't work?

Again many thanks in advance.

Viewing all articles
Browse latest Browse all 27059

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>