⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graphicsform.vb

📁 Windows mobile 6 Direct3D 编程例子
💻 VB
字号:
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic
Imports Microsoft.WindowsMobile.DirectX
Imports Microsoft.WindowsMobile.DirectX.Direct3D


Module ProgramMain
    Sub Main()
        Dim frm As GraphicsForm = New GraphicsForm

        ' Initialize Direct3D
        If Not frm.Init() Then
            MessageBox.Show("Could not initialize Direct3D")
            Return
        End If

        System.Windows.Forms.Application.Run(frm)
    End Sub
End Module
Public Class GraphicsForm
    Inherits Form
    ' Our global variables for me project
    Dim DeviceForm As Device = Nothing
    Dim vertices(2) As CustomVertex.PositionColored

    Dim VBuffer As VertexBuffer = Nothing
    Dim PParameters As New PresentParameters

    Dim xPos As Single = 0.0F
    Dim yPos As Single = 0.0F
    Dim zPos As Single = 0.0F

    Dim rotateAngle As Single = 0.0F
    Dim rotateStep As Single = 0.02F

    ' Setup the rendering device
    Public Function Init() As Boolean
        Try
            ' We don't want to run fullscreen
            PParameters.Windowed = True
            ' Discard the frames
            PParameters.SwapEffect = SwapEffect.Discard
            'Create a DeviceForm
            DeviceForm = New Device(0, DeviceType.Default, Me, _
                CreateFlags.None, PParameters)
            'Turn off lighting
            DeviceForm.RenderState.Lighting = False

            DeviceForm.Transform.View = Matrix.LookAtLH( _
                New Vector3(0.0F, 0.0F, -5.0F), _
                New Vector3(0.0F, 0.0F, 0.0F), _
                New Vector3(0.0F, 1.0F, 0.0F))

            DeviceForm.Transform.Projection = Matrix.PerspectiveFovLH( _
                CSng(Math.PI) / 4, 1.0F, 1.0F, 100.0F)

            DeviceForm.Transform.World = Matrix.Translation(xPos, yPos, zPos)

            'Set the vertex values
            vertices(0).X = 0.0
            vertices(0).Y = 1.0
            vertices(0).Z = 0.0
            vertices(0).Color = System.Drawing.Color.Red.ToArgb()

            vertices(1).X = 1.0
            vertices(1).Y = -1.0
            vertices(1).Z = 0.0
            vertices(1).Color = System.Drawing.Color.Green.ToArgb()

            vertices(2).X = -1.0
            vertices(2).Y = -1.0
            vertices(2).Z = 0.0
            vertices(2).Color = System.Drawing.Color.Blue.ToArgb()

            VBuffer = New VertexBuffer( _
                            GetType(CustomVertex.PositionColored), 3, DeviceForm, 0, _
                            CustomVertex.PositionColored.Format, Pool.SystemMemory)

            VBuffer.SetData(vertices, 0, LockFlags.None)

        Catch
            ' Catch any errors and return a failure
            Return False
        End Try

        Return True
    End Function

    'All rendering for each frame occurs here
    Private Sub Render()
        If Not IsNothing(DeviceForm) Then
            'Clear the backbuffer to a white color
            DeviceForm.Clear(ClearFlags.Target, _
                System.Drawing.Color.White, 1.0F, 0)
            'Begin the scene
            DeviceForm.BeginScene()
            rotateAngle = rotateAngle + rotateStep
            DeviceForm.SetStreamSource(0, VBuffer, 0)
            DeviceForm.Transform.World = Matrix.RotationZ(rotateAngle) _
                * Matrix.Translation(xPos, yPos, zPos)
            DeviceForm.DrawPrimitives(PrimitiveType.TriangleList, 0, 1)
            DeviceForm.EndScene()
            ' Update the screen
            DeviceForm.Present()
        Else
            'DeviceForm is has not been set
        End If
    End Sub

    'Does nothing because we are in control of all the drawing
    Protected Overrides Sub OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)
    End Sub

    'Fired when the form needs to be redrawn
    Protected Overrides Sub OnPaint( _
        ByVal e As System.Windows.Forms.PaintEventArgs)

        ' Render on painting
        Me.Render()
    End Sub

    Private Sub resetTriangle()
        xPos = 0.0F
        yPos = 0.0F
        zPos = 0.0F
        updateTriangle()
    End Sub
    Private stepSize As Double = 0.2

    Private Sub updateTriangle()
        Me.Text = "X:" + xPos.ToString() + _
        " Y:" + yPos.ToString() + _
        " Z:" + zPos.ToString()
        VBuffer.SetData(vertices, 0, LockFlags.None)
        Me.Invalidate()
    End Sub
    Private Sub moveLeft()
        xPos = xPos - stepSize
        updateTriangle()
    End Sub

    Private Sub moveRight()
        xPos = xPos + stepSize
        updateTriangle()
    End Sub

    Private Sub moveUp()
        yPos = yPos - stepSize
        updateTriangle()
    End Sub
    Private Sub moveDown()
        yPos = yPos + stepSize
        updateTriangle()
    End Sub
    Private Sub moveAway()
        zPos = zPos + stepSize
        updateTriangle()
    End Sub
    Private Sub moveTowards()
        zPos = zPos - stepSize
        updateTriangle()
    End Sub

    Private Sub xPlusMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xPlusMenuItem.Click
        moveRight()
    End Sub

    Private Sub xMinusMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xMinusMenuItem.Click
        moveLeft()
    End Sub

    Private Sub yPlusMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yPlusMenuItem.Click
        moveUp()
    End Sub

    Private Sub yMinusMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yMinusMenuItem.Click
        moveDown()
    End Sub

    Private Sub zPlusMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles zPlusMenuItem.Click
        moveAway()
    End Sub

    Private Sub zMinusMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles zMinusMenuItem.Click
        moveTowards()
    End Sub

    Private Sub resetMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles resetMenuItem.Click
        resetTriangle()
    End Sub

    Private Sub exitMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitMenuItem.Click
        Application.Exit()
    End Sub

    Private Sub GraphicsForm_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If (e.KeyCode = System.Windows.Forms.Keys.Up) Then
            moveDown()
        End If
        If (e.KeyCode = System.Windows.Forms.Keys.Down) Then
            moveUp()
        End If
        If (e.KeyCode = System.Windows.Forms.Keys.Left) Then
            moveLeft()
        End If
        If (e.KeyCode = System.Windows.Forms.Keys.Right) Then
            moveRight()
        End If
        If (e.KeyCode = System.Windows.Forms.Keys.Enter) Then
            resetTriangle()
        End If

    End Sub

    Private Sub rotateTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rotateTimer.Tick
        Me.Invalidate()
    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -