我想用directDraw做一个实时显示图像的程序。刚在网上看了一些思路,大体这样:
创建一个主页面和一个缓冲页面。
然后将数据写在缓冲页面上,
然后将缓冲页面换到主页面上显示。但是创建主页面是可以的,创建缓冲页面就有错误,没弄过direct。 private DxVBLib.DirectX7 dx;
private DxVBLib.DirectDraw7 dDraw; private DxVBLib.DirectDrawSurface7 dDSurfacePrimary;//主表面
private DxVBLib.DirectDrawSurface7 dDSurfaceBuffer;//缓冲表面
//表面的区域
private DxVBLib.DDSURFACEDESC2 dDPrimaryDesc;
private DxVBLib.DDSURFACEDESC2 dDBufferDesc;
public bool initDX()
{
bool res = true;
try
{
dx = new DxVBLib.DirectX7();
dDraw = dx.DirectDrawCreate(""); //设置dDraw的显示模式
dDraw.SetCooperativeLevel(handle.ToInt32(), DxVBLib.CONST_DDSCLFLAGS.DDSCL_NORMAL);
//创建主表面
dDPrimaryDesc = new DxVBLib.DDSURFACEDESC2();
dDPrimaryDesc.lFlags = DxVBLib.CONST_DDSURFACEDESCFLAGS.DDSD_CAPS;
dDPrimaryDesc.ddsCaps.lCaps = DxVBLib.CONST_DDSURFACECAPSFLAGS.DDSCAPS_PRIMARYSURFACE;
dDSurfacePrimary = dDraw.CreateSurface(ref dDPrimaryDesc); //创建缓冲表面
dDBufferDesc = new DxVBLib.DDSURFACEDESC2();
dDBufferDesc.lFlags = DxVBLib.CONST_DDSURFACEDESCFLAGS.DDSD_WIDTH|DxVBLib.CONST_DDSURFACEDESCFLAGS.DDSD_HEIGHT;
dDBufferDesc.ddsCaps.lCaps = DxVBLib.CONST_DDSURFACECAPSFLAGS.DDSCAPS_OFFSCREENPLAIN;
dDBufferDesc.lWidth = showWidth;
dDBufferDesc.lHeight = showHeight;
dDSurfaceBuffer = dDraw.CreateSurface(ref dDBufferDesc);
}
catch(Exception ee)
{
res = false;
}
return res;
}有谁做个啊?提供点资料吧?为什么第二个页面创建不了呢?
创建一个主页面和一个缓冲页面。
然后将数据写在缓冲页面上,
然后将缓冲页面换到主页面上显示。但是创建主页面是可以的,创建缓冲页面就有错误,没弄过direct。 private DxVBLib.DirectX7 dx;
private DxVBLib.DirectDraw7 dDraw; private DxVBLib.DirectDrawSurface7 dDSurfacePrimary;//主表面
private DxVBLib.DirectDrawSurface7 dDSurfaceBuffer;//缓冲表面
//表面的区域
private DxVBLib.DDSURFACEDESC2 dDPrimaryDesc;
private DxVBLib.DDSURFACEDESC2 dDBufferDesc;
public bool initDX()
{
bool res = true;
try
{
dx = new DxVBLib.DirectX7();
dDraw = dx.DirectDrawCreate(""); //设置dDraw的显示模式
dDraw.SetCooperativeLevel(handle.ToInt32(), DxVBLib.CONST_DDSCLFLAGS.DDSCL_NORMAL);
//创建主表面
dDPrimaryDesc = new DxVBLib.DDSURFACEDESC2();
dDPrimaryDesc.lFlags = DxVBLib.CONST_DDSURFACEDESCFLAGS.DDSD_CAPS;
dDPrimaryDesc.ddsCaps.lCaps = DxVBLib.CONST_DDSURFACECAPSFLAGS.DDSCAPS_PRIMARYSURFACE;
dDSurfacePrimary = dDraw.CreateSurface(ref dDPrimaryDesc); //创建缓冲表面
dDBufferDesc = new DxVBLib.DDSURFACEDESC2();
dDBufferDesc.lFlags = DxVBLib.CONST_DDSURFACEDESCFLAGS.DDSD_WIDTH|DxVBLib.CONST_DDSURFACEDESCFLAGS.DDSD_HEIGHT;
dDBufferDesc.ddsCaps.lCaps = DxVBLib.CONST_DDSURFACECAPSFLAGS.DDSCAPS_OFFSCREENPLAIN;
dDBufferDesc.lWidth = showWidth;
dDBufferDesc.lHeight = showHeight;
dDSurfaceBuffer = dDraw.CreateSurface(ref dDBufferDesc);
}
catch(Exception ee)
{
res = false;
}
return res;
}有谁做个啊?提供点资料吧?为什么第二个页面创建不了呢?
WINDOWS\Microsoft.NET\DirectX for Managed Code\1.0.2902.0\Microsoft.DirectX.DirectDraw.dll最近做的项目用到了DirectDraw。也在摸索中。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.DirectDraw;namespace DigitalClass
{
public partial class displaywindow : Form
{
Device dev = null;
SurfaceCaps surcaps = null;
SurfaceDescription surdes = null;
Surface sur = null; public displaywindow()
{
InitializeComponent();
this.Shown += new EventHandler(displaywindow_Shown);
} void displaywindow_Shown(object sender, EventArgs e)
{
//throw new Exception("The method or operation is not implemented.");
dev = new Device();
dev.SetCooperativeLevel(this, CooperativeLevelFlags.FullscreenExclusive); surcaps = new SurfaceCaps();
surcaps.BackBuffer = true;
surdes = new SurfaceDescription();
surdes.SurfaceCaps.PrimarySurface = true;
surdes.SurfaceCaps.Flip = true;
surdes.SurfaceCaps.Complex = true;
surdes.BackBufferCount = 2; sur = new Surface(surdes, dev); bool re = dev.TestCooperativeLevel();
} bool is2 = false;
private void button1_Click(object sender, EventArgs e)
{
sur.DrawBox(0,0, 100, 100);
sur.DrawText(0, 0, "HelloWorld", true); is2 = !is2; //获得back Surface
Surface backsuf = sur.GetAttachedSurface(surcaps); if (is2)
backsuf.ColorFill(234556);
else
backsuf.ColorFill(23); sur.Flip(backsuf, FlipFlags.DoNotWait);
backsuf.Dispose();
}
}
还有个问题,网上的资料大多是这样:创建一个离屏表面,直接从一幅图像创建,然后调用bitblt函数画到主页面上,中间的主要部分都没有啊。
关键是,如何望缓冲页面上写数据啊。如果我这样写:
1.创建主页面,
2.创建缓冲页面(使用这个函数GetAttachedSurface();
3.往缓冲页面上写数据。此时主页面应该有显示的吧?
4.如何往缓冲页面上写数据呢?我从来都没条通过,所以不知道运行是个什么样子。
另外还必须要设置显示模式吗?SetDisplayMode();
可以从其他Surface复制,用backsuf.DrawFast
或者backsuf.Lock,然后赋值。数据准备好了,用下面这句显示到前台
sur.Flip(backsuf, FlipFlags.DoNotWait);
SetDisplayMode();
设置分辨率,颜色数,刷新率。由需求决定了。对程序运行没影响。
五楼能不能贴代码Show一下。学习中
Surface sprite = new Surface("catfish.bmp", new SurfaceDescription(), dev);
//获得后台表面
Surface backsuf = sur.GetAttachedSurface(surcaps);
//绘制到后台表面
backsuf.DrawFast(0, 0, sprite, DrawFastFlags.DoNotWait);
//显示
sur.Flip(backsuf, FlipFlags.DoNotWait);