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

📄 graphicsform.vb

📁 清华大学出版社出版的 移动应用开发宝典 张大威(2008)的附书源代码
💻 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

    Public Class ImageBackground
        Public BackgroundTexture As Texture
        Public Vertices As CustomVertex.PositionNormalTextured()
        Public VertBuffer As VertexBuffer = Nothing

        Public Sub New(ByVal device As Device, ByVal textureStream As System.IO.Stream, ByVal sz As Single, ByVal z As Single)
            BackgroundTexture = TextureLoader.FromStream(device, textureStream)
            Vertices = New CustomVertex.PositionNormalTextured(5) {}
            Vertices(0) = New CustomVertex.PositionNormalTextured(-sz, -sz, z, 0.0F, 0.0F, -1.0F, 0.0F, 1.0F)
            Vertices(1) = New CustomVertex.PositionNormalTextured(-sz, sz, z, 0.0F, 0.0F, -1.0F, 0.0F, 0.0F)
            Vertices(2) = New CustomVertex.PositionNormalTextured(sz, -sz, z, 0.0F, 0.0F, -1.0F, 1.0F, 1.0F)
            Vertices(3) = New CustomVertex.PositionNormalTextured(sz, -sz, z, 0.0F, 0.0F, -1.0F, 1.0F, 1.0F)
            Vertices(4) = New CustomVertex.PositionNormalTextured(-sz, sz, z, 0.0F, 0.0F, -1.0F, 0.0F, 0.0F)
            Vertices(5) = New CustomVertex.PositionNormalTextured(sz, sz, z, 0.0F, 0.0F, -1.0F, 1.0F, 0.0F)
            VertBuffer = New VertexBuffer(GetType(CustomVertex.PositionNormalTextured), Vertices.Length, device, Usage.WriteOnly, CustomVertex.PositionNormalTextured.Format, Pool.SystemMemory)
        End Sub

        Public Sub Draw(ByVal device As Device)

            ' Set the data in the vertex buffer to our triangles
            VertBuffer.SetData(Vertices, 0, LockFlags.None)

            ' clear off any existing transformations
            device.Transform.World = Matrix.Identity

            ' Point the device at our vertex buffer
            device.SetStreamSource(0, VertBuffer, 0)

            ' Assign the texture to the device.
            device.SetTexture(0, BackgroundTexture)

            ' Ask the device to draw the contents of the buffer
            device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2)

            ' Stop using this texture
            device.SetTexture(0, Nothing)
        End Sub
    End Class

    Dim iImageBackground As ImageBackground

    ' <summary>
    ' Currently executing assembly. Cached because it is used
    ' to load a lot of resources. 
    ' </summary>
    ReadOnly assembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly

    ' 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

    ' position of the triangle
    Dim xPos As Single = 0.0F
    Dim yPos As Single = 0.0F
    Dim zPos As Single = 0.0F

    'rotation about Y
    Dim yawAngle As Single = 0.0F
    Dim yawStep As Single = 0.1F

    'rotation about X
    Dim pitchAngle As Single = 0.0F
    Dim pitchStep As Single = 0.01F

    'rotation about Z
    Dim rollAngle As Single = 0.0F
    Dim rollStep As Single = 0.001F

    ' 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

            'Turn off Culling
            DeviceForm.RenderState.CullMode = Cull.None

            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)

            iImageBackground = New ImageBackground(DeviceForm, _
                assembly.GetManifestResourceStream("MoveTriangle.Background.png"), _
                3.0F, 0.2F)

        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()

            ' Get the background to draw itself
            iImageBackground.Draw(DeviceForm)

            ' Point the device at our vertex buffer
            DeviceForm.SetStreamSource(0, VBuffer, 0)

            ' Rotate the triangle
            yawAngle = yawAngle + yawStep
            pitchAngle = pitchAngle + pitchStep
            rollAngle = rollAngle + rollStep

            ' Create a translation matrix to move our triangle
            DeviceForm.Transform.World = _
                Matrix.RotationYawPitchRoll(yawAngle, pitchAngle, rollAngle) _
                * Matrix.Translation(xPos, yPos, zPos)

            ' Ask the device to draw the contents of the buffer
            DeviceForm.DrawPrimitives(PrimitiveType.TriangleList, 0, 1)

            ' End of the draw process
            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 + -