DX之“HelloWord”

目标:使用VS建立窗口程序,通过DX绘制窗口背景色。

 

1,安装DX(含有托管的版本)。

2,使用VS建立窗口程序。

3,引入托管DX环境。

image

4,编写基础代码。全部在MainForm代码文件中。

原始代码:

image

 

修改后代码

引入DX

image

public partial class MainForm : Form
{
   public MainForm()
   {
       InitializeComponent();
       this.InitialDX();
   }
 
   private Device device = null;
 
   private void InitialDX()
   {
       PresentParameters presentParams = new PresentParameters();
       presentParams.Windowed = true;
       presentParams.SwapEffect = SwapEffect.Discard;
 
       device = new Device(
           0,
           DeviceType.Hardware,
           this,
           CreateFlags.SoftwareVertexProcessing,
           presentParams);
   }
 
   protected override void OnPaint(PaintEventArgs e)
   {
       base.OnPaint(e);
       this.DrawDX();
   }
 
   private void DrawDX()
   {
       if (this.device == null)
           return;
 
       this.device.Clear(ClearFlags.Target,
           Color.AliceBlue,
           1f,
           0);
 
       this.device.BeginScene();
       //绘制DX图形
       this.device.EndScene();
 
       this.device.Present();//显示图形
   }
}

效果:

image

注:

1,所有的绘制在 BeginScene和EndScene之间完成。

2,窗体背景颜色设置主要使用了Device.Clear 方法

public void Clear(
	ClearFlags flags,
	Color color,
	float zdepth,
	int stencil
)
将视区或视区中的一组矩形清除为指定的 RGBA 颜色,清除深度缓冲区并清除模具缓冲区。
参数:
flags
类型:Microsoft.WindowsMobile.DirectX.Direct3D.ClearFlags

指示要清除的图面的标志。此参数可以是下列标志的任意组合,但必须至少使用一个标志:

  • Stencil:将模具缓冲区清除为 stencil 参数中的值。

  • Target:将呈现目标清除为 color 参数中的颜色。

  • ZBuffer:将深度缓冲区清除为 zdepth 参数中的值。

color
类型:System.Drawing.Color

一个 Color 对象,它表示将呈现目标图面清除为的颜色。

zdepth
类型:System.Single

此方法存储在深度缓冲区中的新 zdepth 值。此参数的范围可以在 0.0 到 1.0 之间(对于基于 z 或基于 w 的深度缓冲区)。如果值为 0.0,表示与查看器的最近距离;如果值为 1.0,则表示最远距离。

stencil
类型:System.Int32

要存储在每个模具缓冲区项中的整数值。此参数的范围可以在 0 到 2n-1 之间,其中 n 是模具缓冲区的位深度。

posted @ 2011-02-10 11:17  sharpfeng  阅读(278)  评论(0编辑  收藏  举报