WinForm og terminering
Antag følgende kode:using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using D3D = Microsoft.DirectX.Direct3D;
public class D3DTestForm : System.Windows.Forms.Form {
private Device device;
private System.ComponentModel.Container components = null;
private bool wireframe = false;
private float angle = 0f;
private CustomVertex.PositionColored[] vertices;
public D3DTestForm() {
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(500, 500);
this.Text = "DirectX Test";
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
Thread thread = new Thread(new ThreadStart(this.run));
thread.Start();
}
public void InitializeDevice() {
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
}
private void CameraPositioning() {
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1f, 50f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, -30), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
device.RenderState.Lighting = false;
device.RenderState.CullMode = Cull.None;
}
private void VertexDeclaration() {
vertices = new CustomVertex.PositionColored[3];
vertices[0].Position = new Vector3(0f, 0f, 0f);
vertices[0].Color = Color.Red.ToArgb();
vertices[1].Position = new Vector3(10f, 0f, 0f);
vertices[1].Color = Color.Green.ToArgb();
vertices[2].Position = new Vector3(5f, 10f, 0f);
vertices[2].Color = Color.Yellow.ToArgb();
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) {
device.Clear(ClearFlags.Target, Color.DarkSlateBlue, 1.0f, 0);
device.RenderState.FillMode = wireframe ? D3D.FillMode.WireFrame : D3D.FillMode.Solid;
device.BeginScene();
device.VertexFormat = CustomVertex.PositionColored.Format;
device.Transform.World = Matrix.Translation(-5, -10 * 1 / 3, 0) * Matrix.RotationAxis(new Vector3(angle * 4, angle * 2, angle * 3), angle);
device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertices);
device.EndScene();
device.Present();
this.Invalidate();
}
protected override void Dispose(bool disposing) {
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
private void run() {
while (true) {
angle += 0.05f;
Thread.Sleep(16);//average: 1000/16 = 62.5 FPS
}
}
protected override void OnKeyDown(KeyEventArgs e) {
switch (e.KeyCode) {
case Keys.Escape:
Application.Exit();
break;
case Keys.W:
wireframe = !wireframe;
break;
default:
base.OnKeyDown(e);
break;
}
}
static void Main() {
using (D3DTestForm form = new D3DTestForm()) {
form.InitializeDevice();
form.CameraPositioning();
form.VertexDeclaration();
Application.Run(form);
}
}
}
Når jeg terminerer formen (f.eks. tryk på "kryds" eller ESC) så forsvinder formen men programmet kører fortsat videre - kan ses i Windows Task Manager.
Hvordan får jeg afsluttet programmet helt når formen lukkes.
