[高手救急]关于DirectDraw,这段代码就是运行不过
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX.DirectDraw;
namespace DirectDrawTest
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <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()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(432, 338);
this.Name = "Form1";
this.Text = "Form1";
this.Resize += new System.EventHandler(this.Form1_Resize);
this.Load += new System.EventHandler(this.Form1_Load);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
#endregion
// DirectDraw 设备
private Device dev = new Device(CreateFlags.Default);
// 两个表面
private SurfaceDescription sdPrimary = null;
private SurfaceDescription sdSecondary = null;
private Surface surPrimary = null;
private Surface surSecondary = null;
// 一个后台表面
private SurfaceDescription sdImage = null;
private Surface surImage = null;
// 表面的矩形区域
private Rectangle rPrimary, rSecondary, rImage;
// Image
private Bitmap bmp;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void InitDDraw()
{
// Set Cooperative Level
dev.SetCooperativeLevel(this, CooperativeLevelFlags.Normal);
// Primary Surface
sdPrimary = new SurfaceDescription();
sdPrimary.SurfaceCaps.PrimarySurface = true;
sdPrimary.SurfaceCaps.Flip = true;
sdPrimary.BackBufferCount = 1;
// !!!! 运行到这句就出错,提示是“应用程序错误”
surPrimary = new Surface(sdPrimary, dev);
// Secondary Surface
sdSecondary = new SurfaceDescription();
sdSecondary.SurfaceCaps.BackBuffer = true;
surSecondary = surPrimary.GetAttachedSurface(sdSecondary.SurfaceCaps);
surSecondary.ForeColor = Color.White;
surSecondary.FontTransparency = true;
// Image
bmp = (Bitmap)Image.FromFile(@"E:\image.jpg");
}
private void LoadSurface()
{
sdImage = new SurfaceDescription();
sdImage.SurfaceCaps.OffScreenPlain = true;
sdImage.Width = surSecondary.SurfaceDescription.Width;
sdImage.Height = surSecondary.SurfaceDescription.Height;
surImage = new Surface(bmp, sdImage, dev);
rPrimary.Width = surPrimary.SurfaceDescription.Width;
rPrimary.Height = surPrimary.SurfaceDescription.Height;
rSecondary.Width = surSecondary.SurfaceDescription.Width;
rSecondary.Height = surSecondary.SurfaceDescription.Height;
rImage.Width = surImage.SurfaceDescription.Width;
rImage.Height = surImage.SurfaceDescription.Height;
}
private void draw()
{
if (surSecondary == null)
{
return;
}
surSecondary.DrawFast(0, 0, surImage, rImage, DrawFastFlags.Wait);
surPrimary.Flip(null, FlipFlags.NoVSync);
}
private void Form1_Load(object sender, System.EventArgs e)
{
InitDDraw();
LoadSurface();
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
draw();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
draw();
}
}
}
为什么?