TextCtrl.cs
-------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Direct3D = Microsoft.DirectX.Direct3D;
namespace DxForm
{
/// <summary>
/// FontCtrl 的摘要说明。
/// </summary>
public class TextCtrl : System.Windows.Forms.Control
{
private Device device = null;
private Direct3D.Font font = null;
private Mesh mesh = null;
private Material meshMaterial;
private float angle = 0.0f;
/// <summary>
/// We will initialize our graphics device here
/// </summary>
public void InitializeGraphics()
{
// 设置显示参数
PresentParameters presentParams = new PresentParameters();
Draw2DText("If you type enough words in a single sentence, " +
"you may notice that the text begins to wrap. Try resizing " +
"the window to notice how the text changes as you size it.",
this.Width / 2 + this.Width / 4, this.Height / 4 , Color.Yellow);
// Draw our two spinning meshes
Draw3DText(new Vector3(1.0f, 1.0f, 0.0f),
new Vector3(-3.0f, 0.0f, 0.0f));
Draw3DText(new Vector3(0.0f, 1.0f, 1.0f),
new Vector3(0.0f, -1.0f, 1.0f));
device.EndScene();
device.Present();
this.Invalidate();
}
/// <summary>
/// Draw some 3D text rotating
/// </summary>
/// <param name="axis">The axis of rotation</param>
/// <param name="location">The location of the text</param>
private void Draw3DText(Vector3 axis, Vector3 location)
{
device.Transform.World = Matrix.RotationAxis(
axis, angle) *
Matrix.Translation(location);
device.Material = meshMaterial;
mesh.DrawSubset(0);
angle += 0.01f;
}
private void Draw2DText(string text, int x, int y, Color c)
{
font.DrawText(null, text, new Rectangle(x, y,
this.Width , this.Height ),
DrawTextFormat.NoClip | DrawTextFormat.ExpandTabs |
DrawTextFormat.WordBreak , c);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Text = "Form1";
}
#endregion
public BoxCtrl()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint|ControlStyles.Opaque,true);
}
private void CreateVertexBuffers()
{
CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[36];
// float len = 0.5f;
// 前表面
verts[0] = new CustomVertex.PositionColored(-1.0f, 1.0f, 1.0f, Color.Red.ToArgb());
verts[1] = new CustomVertex.PositionColored(-1.0f, -1.0f, 1.0f, Color.Red.ToArgb());
verts[2] = new CustomVertex.PositionColored(1.0f, 1.0f, 1.0f, Color.Red.ToArgb());
verts[3] = new CustomVertex.PositionColored(-1.0f, -1.0f, 1.0f, Color.Red.ToArgb());
verts[4] = new CustomVertex.PositionColored(1.0f, -1.0f, 1.0f, Color.Red.ToArgb());
verts[5] = new CustomVertex.PositionColored(1.0f, 1.0f, 1.0f, Color.Red.ToArgb());
// Back face (remember this is facing *away* from the camera, so vertices should be clockwise order)
verts[6] = new CustomVertex.PositionColored(-1.0f, 1.0f, -1.0f, Color.Blue.ToArgb());
verts[7] = new CustomVertex.PositionColored(1.0f, 1.0f, -1.0f, Color.Blue.ToArgb());
verts[8] = new CustomVertex.PositionColored(-1.0f, -1.0f, -1.0f, Color.Blue.ToArgb());
verts[9] = new CustomVertex.PositionColored(-1.0f, -1.0f, -1.0f, Color.Blue.ToArgb());
verts[10] = new CustomVertex.PositionColored(1.0f, 1.0f, -1.0f, Color.Blue.ToArgb());
verts[11] = new CustomVertex.PositionColored(1.0f, -1.0f, -1.0f, Color.Blue.ToArgb());
// Top face
verts[12] = new CustomVertex.PositionColored(-1.0f, 1.0f, 1.0f, Color.Yellow.ToArgb());
verts[13] = new CustomVertex.PositionColored(1.0f, 1.0f, -1.0f, Color.Yellow.ToArgb());
verts[14] = new CustomVertex.PositionColored(-1.0f, 1.0f, -1.0f, Color.Yellow.ToArgb());
verts[15] = new CustomVertex.PositionColored(-1.0f, 1.0f, 1.0f, Color.Yellow.ToArgb());
verts[16] = new CustomVertex.PositionColored(1.0f, 1.0f, 1.0f, Color.Yellow.ToArgb());
verts[17] = new CustomVertex.PositionColored(1.0f, 1.0f, -1.0f, Color.Yellow.ToArgb());
// Bottom face (remember this is facing *away* from the camera, so vertices should be clockwise order)
verts[18] = new CustomVertex.PositionColored(-1.0f, -1.0f, 1.0f, Color.Black.ToArgb());
verts[19] = new CustomVertex.PositionColored(-1.0f, -1.0f, -1.0f, Color.Black.ToArgb());
verts[20] = new CustomVertex.PositionColored(1.0f, -1.0f, -1.0f, Color.Black.ToArgb());
verts[21] = new CustomVertex.PositionColored(-1.0f, -1.0f, 1.0f, Color.Black.ToArgb());
verts[22] = new CustomVertex.PositionColored(1.0f, -1.0f, -1.0f, Color.Black.ToArgb());
verts[23] = new CustomVertex.PositionColored(1.0f, -1.0f, 1.0f, Color.Black.ToArgb());
// Left face
verts[24] = new CustomVertex.PositionColored(-1.0f, 1.0f, 1.0f, Color.Gray.ToArgb());
verts[25] = new CustomVertex.PositionColored(-1.0f, -1.0f, -1.0f, Color.Gray.ToArgb());
verts[26] = new CustomVertex.PositionColored(-1.0f, -1.0f, 1.0f, Color.Gray.ToArgb());
verts[27] = new CustomVertex.PositionColored(-1.0f, 1.0f, -1.0f, Color.Gray.ToArgb());
verts[28] = new CustomVertex.PositionColored(-1.0f, -1.0f, -1.0f, Color.Gray.ToArgb());
verts[29] = new CustomVertex.PositionColored(-1.0f, 1.0f, 1.0f, Color.Gray.ToArgb());
// Right face (remember this is facing *away* from the camera, so vertices should be clockwise order)
verts[30] = new CustomVertex.PositionColored(1.0f, 1.0f, 1.0f, Color.Green.ToArgb());
verts[31] = new CustomVertex.PositionColored(1.0f, -1.0f, 1.0f, Color.Green.ToArgb());
verts[32] = new CustomVertex.PositionColored(1.0f, -1.0f, -1.0f, Color.Green.ToArgb());
verts[33] = new CustomVertex.PositionColored(1.0f, 1.0f, -1.0f, Color.Green.ToArgb());
verts[34] = new CustomVertex.PositionColored(1.0f, 1.0f, 1.0f, Color.Green.ToArgb());
verts[35] = new CustomVertex.PositionColored(1.0f, -1.0f, -1.0f, Color.Green.ToArgb());