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

📄 graphicsform.cs

📁 清华大学出版社出版的 移动应用开发宝典 张大威(2008)的附书源代码
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.DirectX;
using Microsoft.WindowsMobile.DirectX.Direct3D;

namespace DrawTriangle
{
    public partial class GraphicsForm : Form
    {
        /// <summary>
        /// Device we are going to use to render our graphics
        /// </summary>
        private Device device;

        public GraphicsForm()
        {
            InitializeComponent();
        }

        VertexBuffer vertBuffer;
        
        public void InitializeGraphics()
        {
            PresentParameters presentParams = new PresentParameters();
            presentParams.Windowed = true;
            presentParams.SwapEffect = SwapEffect.Discard;

            device = new Device(0, DeviceType.Default, this, CreateFlags.None, presentParams);

            // no need for lighting, we are setting the color of our triangle ourselves
            device.RenderState.Lighting = false;

            // three vertices for a single triangle
            CustomVertex.PositionColored[] vertices = new CustomVertex.PositionColored[3];

            // Origin (0,0) is the middle of the screen 
            // The left hand edge is at X=-1, right hand edge at X=1
            // The top is y=1, the bottom is y=-1
            // This triangle will fill the screen

            vertices[0].X = 0.0f; 
            vertices[0].Y = 1.0f; 
            vertices[0].Z = 0.0f; 
            vertices[0].Color = Color.Red.ToArgb();

            vertices[1].X = 1.0f;
            vertices[1].Y = -1.0f; 
            vertices[1].Z = 0.0f; 
            vertices[1].Color = Color.Green.ToArgb();

            vertices[2].X = -1.0f; 
            vertices[2].Y = -1.0f; 
            vertices[2].Z = 0.0f; 
            vertices[2].Color = Color.Blue.ToArgb();

            // Create a vertex buffer to hold our triangle information
            vertBuffer = new VertexBuffer(
                typeof(CustomVertex.PositionColored),   // type of the buffer
                3,                                      // holding 3 vertices
                device,                                 // for our device
                Usage.WriteOnly,                        // never going to read it
                CustomVertex.PositionColored.Format,    // format required
                Pool.SystemMemory);                     // mobile 3D requires this

            // Set the data in the vertex buffer to our triangles
            vertBuffer.SetData(vertices, 0, LockFlags.None);
        }

        // Do not want the system to paint the window background on refresh
        protected override void OnPaintBackground(PaintEventArgs e)
        {
        }

        // Called when the screen is redrawn
        protected override void OnPaint(PaintEventArgs e)
        {
            Render();
        }

        /// <summary>
        /// Our render method
        /// </summary>
        private void Render()
        {
            // Make the screen white to start with
            device.Clear(ClearFlags.Target, Color.White, 1.0f, 0);

            // Beginning of the draw process
            device.BeginScene();
            
            // Point the device at our vertex buffer

            device.SetStreamSource(0, vertBuffer, 0);

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

            // End of the draw process
            device.EndScene();

            // Now present the image on the screen
            device.Present();
        }
    }
}

⌨️ 快捷键说明

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