菜鸟求教:我这个WinForm为啥不能同时显示两个DirectX编写的控件?

oceanskywang 2007-08-15 09:22:03
刚学DirectX托管编程(C#),采用Control派生了两个控件(分别用来绘制三维盒子和三维文字),将其加入到Form中。遇到的问题是窗体不能同时显示两个控件(只有第一个被加入到窗体中的控件能正常显示),但是当我们拖动窗体或调整窗体大小时两个控件都显示正常,这是什么问题呢。
大侠们帮忙看看我的代码哪里有问题,先谢过了哈.
以下给出三个代码文件,分别为Form1.cs,BoxCtrl.cs和TextCtrl.cs
...全文
236 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Leon8086 2007-09-03
  • 打赏
  • 举报
回复
C#不熟……

不过C++的经验不知道有没有帮助,请注意一个Instance只能有一个Device,多窗口要使用IDirect3DSwapChain9接口来支持,每个窗口一个对象。具体的可以参照DX的例子。
oceanskywang 2007-08-15
  • 打赏
  • 举报
回复
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>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public TextCtrl()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

this.SetStyle(ControlStyles.AllPaintingInWmPaint|ControlStyles.Opaque, true);
}

/// <summary>
/// We will initialize our graphics device here
/// </summary>
public void InitializeGraphics()
{
// 设置显示参数
PresentParameters presentParams = new PresentParameters();

presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.AutoDepthStencilFormat = DepthFormat.D16;
presentParams.EnableAutoDepthStencil = true;

// 创建显示设备
device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams);
device.DeviceReset += new System.EventHandler(this.OnDeviceReset);
OnDeviceReset(device, null);

// What font do we want to use?
System.Drawing.Font localFont = new System.Drawing.Font
("Arial", 14.0f, FontStyle.Italic);

// Create an extruded version of this font
mesh = Mesh.TextFromFont(device, localFont, "Managed DirectX",
0.001f, 0.1f);

// Create a material for our text mesh
meshMaterial = new Material();
meshMaterial.Diffuse = Color.Peru;

// Create a font we can draw with
font = new Direct3D.Font(device, localFont);
}

private void OnDeviceReset(object sender, EventArgs e)
{
Device dev = (Device)sender;
dev.Transform.Projection = Matrix.PerspectiveFovLH(
(float)Math.PI / 4, this.Width / this.Height, 1.0f, 100.0f);

dev.Transform.View = Matrix.LookAtLH(new Vector3(0,0, -9.0f),
new Vector3(), new Vector3(0,1,0));

dev.Lights[0].Type = LightType.Directional;
dev.Lights[0].Diffuse = Color.White;
dev.Lights[0].Direction = new Vector3(0, 0, 1);
dev.Lights[0].Update();
dev.Lights[0].Enabled = true;
}


protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
if(device==null)
{
this.InitializeGraphics();
}

device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);

device.BeginScene();

Draw2DText("Here's some text", 10, 10, Color.WhiteSmoke);
Draw2DText("Here's some text\r\nwith\r\nhard\r\nline breaks",
100, 80, Color.Violet);

Draw2DText("This\tis\tsome\ttext\twith\ttabs.",
this.Width / 2, this.Height - 80 , Color.RoyalBlue);

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);
}


protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#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

}
}
oceanskywang 2007-08-15
  • 打赏
  • 举报
回复
BoxCtrl.cs
-----------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace DxForm
{
/// <summary>
/// Cavas 的摘要说明。
/// </summary>
public class BoxCtrl:System.Windows.Forms.Control
{
private Microsoft.DirectX.Direct3D.Device _mDevice;
// private Microsoft.DirectX.Direct3D.Mesh _mMesh = null;
private Microsoft.DirectX.Direct3D.VertexBuffer _mVBuffer = null;

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

//创建顶点缓冲
this._mVBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), 36, _mDevice, Usage.Dynamic
| Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);
this._mVBuffer.SetData(verts,0,LockFlags.None);
}

protected void InitializeGraphics3D()
{
// 设置显示参数
PresentParameters presentParams = new PresentParameters();

presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
// presentParams.AutoDepthStencilFormat = DepthFormat.D16;
// presentParams.EnableAutoDepthStencil = true;

// 创建显示设备
_mDevice = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
_mDevice.DeviceReset += new EventHandler(onDeviceReset);
onDeviceReset(_mDevice, null);
}


float angle = 0.0f;
private void SetupCamera()
{
_mDevice.Transform.World = Matrix.RotationYawPitchRoll(angle / (float)Math.PI, angle / (float)Math.PI * 2.0f, angle / (float)Math.PI / 4.0f);
angle += 0.1f;

_mDevice.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 100.0f);
_mDevice.Transform.View = Matrix.LookAtLH(new Vector3(0,0, 5.0f), new Vector3(), new Vector3(0,1,0));
_mDevice.RenderState.Lighting = false;
}

protected void onDeviceReset(object sender, EventArgs e)
{
this.SetupCamera();
}


protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
if(_mDevice==null)
{
this.InitializeGraphics3D();
}

if(this._mVBuffer==null)
this.CreateVertexBuffers();


Device dc = _mDevice;
dc.Clear(ClearFlags.Target ,System.Drawing.Color.Black,1.0f,0,null);

SetupCamera();
dc.BeginScene();
dc.VertexFormat = CustomVertex.PositionColored.Format;
dc.SetStreamSource(0,this._mVBuffer,0);
dc.DrawPrimitives(PrimitiveType.TriangleList,0,12);
dc.EndScene();
dc.Present();

this.Invalidate();
}



}
}
oceanskywang 2007-08-15
  • 打赏
  • 举报
回复
Form1.cs
---------------------------------

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DxForm
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
BoxCtrl _mBox;
TextCtrl _mText;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this._mText = new TextCtrl();
this._mBox = new BoxCtrl();
this.SuspendLayout();
//
// _mCanvas
//
this._mBox.Location = new System.Drawing.Point(0, 100);
this._mBox.Size = new System.Drawing.Size(300, 260);
//
//
//
this._mText.Location = new Point(400,100);
this._mText.Size = new Size(300,260);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(800, 600);
this.Controls.Add(this._mBox);
this.Controls.Add(this._mText);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}

8,303

社区成员

发帖
与我相关
我的任务
社区描述
游戏开发相关内容讨论专区
社区管理员
  • 游戏开发
  • 呆呆敲代码的小Y
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧