C#中如何使用DirectX?

flashpig 2004-09-24 10:20:47
在C#中如何使用DirectX呢?有没有例子发给我flashpig8014@163.com
开发环境如何配置呢?(我已经安装了.NET2.0,DirectX9.0)
...全文
598 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
andyou2000 2004-09-30
  • 打赏
  • 举报
回复
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DxVBLib;

public class Sample1Form : Form
{
private Container components = null;
private Panel Panel1 = null;

private DirectX7 DirectX = null;
private DirectDraw7 DirectDraw = null;
private DirectDrawSurface7 Surface = null;
private DirectDrawSurface7 PrimarySurface = null;
private DDSURFACEDESC2 Surface1;
private DDSURFACEDESC2 Surface2;
private DirectDrawClipper Clipper = null;

private Boolean bInit;

public Sample1Form()
{
InitializeComponent();
DirectX = new DirectX7();
InitializeDirectX();
}

private void InitializeComponent()
{
this.components = new Container();
this.Panel1 = new Panel();

Panel1.Location = new Point(0, 0);
Panel1.Size = new Size(255, 255);
Panel1.TabStop = false;

this.Controls.Add(Panel1);
this.Text = "Interoperability with DirectX";
this.ClientSize = new Size(255, 255);

// Event handlers
this.Resize += new EventHandler(this.Form_Resize);
Panel1.Paint += new PaintEventHandler(this.Panel1_Paint);
}

private void Form_Resize (System.Object sender, System.EventArgs e)
{
Panel1.Width = this.ClientSize.Width;
Panel1.Height = this.ClientSize.Height;
Blt();
}

//
// Called during run-time when the form is moved or resized.
//
public void Panel1_Paint(System.Object sender, PaintEventArgs e)
{
DirectDraw.RestoreAllSurfaces();
Blt();
}

private void Blt()
{
// Has DirectX been initialized? If not, exit.
if (bInit == false)
return;

DxVBLib.RECT r1 = new DxVBLib.RECT();
DxVBLib.RECT r2 = new DxVBLib.RECT();

// Gets the bounding rectangle for the entire window handle and stores it in r1
DirectX.GetWindowRect(Panel1.Handle.ToInt32(), ref r1);

r2.Bottom = Surface2.lHeight;
r2.Right = Surface2.lWidth;

PrimarySurface.Blt(ref r1, Surface, ref r2, CONST_DDBLTFLAGS.DDBLT_WAIT);
}

private void InitializeDirectX()
{
// The empty string parameter means to use the active display driver
DirectDraw = DirectX.DirectDrawCreate("");

// Indicate that this will be a normal windowed application
// with the same display depth as the current display
DirectDraw.SetCooperativeLevel(this.Handle.ToInt32(), CONST_DDSCLFLAGS.DDSCL_NORMAL);

// Indicate that the ddsCaps member is valid
Surface1.lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS;

// This surface is the primary surface (the one visible to the user)
Surface1.ddsCaps.lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_PRIMARYSURFACE;

// Create the primary surface with the surface description we just set
PrimarySurface = DirectDraw.CreateSurface(ref Surface1);

// Set the second surface description
Surface2.lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS;

// This is going to be a plain off-screen surface
Surface2.ddsCaps.lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_OFFSCREENPLAIN;

//Create the off-screen surface

try
{
Surface = DirectDraw.CreateSurfaceFromFile("background.bmp", ref Surface2);
}
catch(System.Runtime.InteropServices.COMException e)
{
// File Not Found
if ( (uint)e.ErrorCode == 0x800A0035)
{
MessageBox.Show("Could not find the file 'background.bmp'. This must be placed in the current directory.", "Picture Not Found");
}
else
{
MessageBox.Show("Unexpected exception: " + e.ToString(), "Unexpected Exception");
}
Application.Exit();
Application.DoEvents();
}

Clipper = DirectDraw.CreateClipper(0);
Clipper.SetHWnd(Panel1.Handle.ToInt32());
PrimarySurface.SetClipper(Clipper);

// We've finished initialization
bInit = true;
Blt();
}

[STAThread()]
public static void Main()
{
Application.Run(new Sample1Form());
}

}
flashpig 2004-09-27
  • 打赏
  • 举报
回复
怎么引用呢??? 有没有明确的例子呢??? 我要看例子!!!
LiloZhu 2004-09-26
  • 打赏
  • 举报
回复
沒做過類似的程序,幫你項...
andershong 2004-09-26
  • 打赏
  • 举报
回复
用命令行编译也行。
要记得在参数加入引用的DirectX DLL就可以了。
felixwoo 2004-09-24
  • 打赏
  • 举报
回复
为什么要用命令行编译呢?用vs.net来写多方便。DirectX SDK的例子很多,如果还觉得不够的花可以去http://www.codeproject.com/cs/media/#DirectX看看,一定会有收获的。
wuyeniao 2004-09-24
  • 打赏
  • 举报
回复
up
flashpig 2004-09-24
  • 打赏
  • 举报
回复
看见例子了, 可是没办法用命令行编译。能解释一下怎么用命令行来编译C#写的DirectX程序。
andershong 2004-09-24
  • 打赏
  • 举报
回复
你可能只是装了RUNTIEM,所以没看到例子。
andershong 2004-09-24
  • 打赏
  • 举报
回复
SDK里面自带了很多例子。
flashpig 2004-09-24
  • 打赏
  • 举报
回复
还是不太明白,有没有例子呢?发给我flashpig8014@163.com 谢谢
速马 2004-09-24
  • 打赏
  • 举报
回复
1、下载安装DirectX 9.0SDK,安装时记着选择最后一个选项(默认不选的)
2、在安装后的目录下执行: Redist\DirectX9\DXSetup.exe /InstallManagedDX
3、在项目中就可以添加那些dll的引用,和Microsoft.NET一个目录下

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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