📄 graphicsform.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 MoveTriangle
{
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;
CustomVertex.PositionColored[] vertices;
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;
// Add the camera to our world and point it at the triangle
device.Transform.View = Matrix.LookAtLH(
new Vector3(0.0f, 0.0f, -5.0f), // camera position
new Vector3(0.0f, 0.0f, 0.0f), // camera target
new Vector3(0.0f, 1.0f, 0.0f) // camera up vector
);
// Set up the projection for our world
device.Transform.Projection =
Matrix.PerspectiveFovLH(
(float)Math.PI / 4, // field of view
1.0f, // aspect ratio
1.0f, // near Z plane
100.0f // far Z plane
);
// three vertices for a single triangle
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();
}
private void exitProgram()
{
Application.Exit();
}
private void updateTriangle()
{
Text = "X:" + vertices[0].X + " Y:" + vertices[0].Y + " Z:" + vertices[0].Z;
vertBuffer.SetData(vertices, 0, LockFlags.None);
Invalidate();
}
private void resetTriangle()
{
vertices[0].X = 0.0f;
vertices[0].Y = 1.0f;
vertices[0].Z = 0.0f;
vertices[1].X = 1.0f;
vertices[1].Y = -1.0f;
vertices[1].Z = 0.0f;
vertices[2].X = -1.0f;
vertices[2].Y = -1.0f;
vertices[2].Z = 0.0f;
updateTriangle();
}
private float step = 0.2f;
private void moveLeft()
{
vertices[0].X -= step;
vertices[1].X -= step;
vertices[2].X -= step;
updateTriangle();
}
private void moveRight()
{
vertices[0].X += step;
vertices[1].X += step;
vertices[2].X += step;
updateTriangle();
}
private void moveUp()
{
vertices[0].Y += step;
vertices[1].Y += step;
vertices[2].Y += step;
updateTriangle();
}
private void moveDown()
{
vertices[0].Y -= step;
vertices[1].Y -= step;
vertices[2].Y -= step;
updateTriangle();
}
private void moveTowards()
{
vertices[0].Z -= step;
vertices[1].Z -= step;
vertices[2].Z -= step;
updateTriangle();
}
private void moveAway()
{
vertices[0].Z += step;
vertices[1].Z += step;
vertices[2].Z += step;
updateTriangle();
}
private void GraphicsForm_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == System.Windows.Forms.Keys.Up))
{
moveUp();
}
if ((e.KeyCode == System.Windows.Forms.Keys.Down))
{
moveDown();
}
if ((e.KeyCode == System.Windows.Forms.Keys.Left))
{
moveLeft();
}
if ((e.KeyCode == System.Windows.Forms.Keys.Right))
{
moveRight();
}
if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
{
resetTriangle();
}
}
private void resetMenuItem_Click(object sender, EventArgs e)
{
resetTriangle();
}
private void zMinusMenuItem_Click(object sender, EventArgs e)
{
moveTowards();
}
private void zPlusMenuItem_Click(object sender, EventArgs e)
{
moveAway();
}
private void yMinusMenuItem_Click(object sender, EventArgs e)
{
moveUp();
}
private void yPlusMenuItem_Click(object sender, EventArgs e)
{
moveDown();
}
private void xMinusMenuItem_Click(object sender, EventArgs e)
{
moveLeft();
}
private void xPulsMenuItem_Click(object sender, EventArgs e)
{
moveRight();
}
private void exitMenuItem_Click(object sender, EventArgs e)
{
exitProgram();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -