VB中使用DirectX库的简明教程
来源:互联网
作者:西部数码
时间:2008-04-09
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!
meshbox.GetFace(5).SetColor g_dx.CreateColorRGB(1, 1, 1)
'将立方体对象添加到场景中
framebox.AddVisual meshbox
End Sub
Private Sub Form_Load()
Dim rectMe As RECT
Dim rectEmpty As RECT
Me.Show
Me.ScaleMode = 3
InitS
InitDDraw
InitD3dRM
g_dx.GetWindowRect Me.hWnd, rectMe
While True
DoEvents
'清除视区内德内容
RMViewPort.Clear D3DRMCLEAR_ZBUFFER Or D3DRMCLEAR_TARGET
'重新渲染视区内的场景
RMViewPort.Render RMFrameScene
'更新三维设备
RMDevice.Update
'将后台绘图平面的内容翻转到前台来
Call MainBuffer.Blt(rectMe, BackBuffer, rectEmpty, DDBLT_WAIT)
Wend
End Sub
Private Sub Form_Unload(Cancel As Integer)
CleanUp
End
End Sub
运行程序,在窗口中就会出现一个立方体,可以看到,相对于使用立即模式,上面的保留模式就简单的多了。首先调用InitS函数建立场景、设置灯光以及相机,然后调用InitDDraw函数建立DirectDraw对象,最后调用InitD3dRM函数建立Direct3D对象以及设备并绘制三维立方体,设置立方体每面的颜色并添加到场景中(演员登场)。如果想让三维对象动起来,可以通过设置三维对象的位置、场景的角度以及相机的角度来分别实现。
三、 DirectInput
DirectInput是DirectX中的输入设备控件对象,它包括了对键盘、鼠标以及游戏杆的支持,使用DirectX的DirectInputCreate方法可以建立DirectInput对象,当建立一个DirectInput对象后,利用DirectInput对象的CreateDevice方法可以建立一个DirectInputDevice对象,使用DirectInputDevice对象可以获得对输入设备(键盘、鼠标、游戏杆)的控制。
下面通过一个简单的程序来了解DirectInput是如何工作的。首先建立一个新工程,加入Directx7说明库,在Form1中加入一个Timer控件和一个Label控件。然后在Form1中加入以下代码:
Option Explicit
Dim dx As New DirectX7
Dim di As DirectInput
Dim diDEV As DirectInputDevice
Dim diMouse As DirectInputDevice
Dim diState As DIKEYBOARDSTATE
Dim diSMouse As DIMOUSESTATE
Dim iKeyCounter As Integer
Dim ix, iy, iz
Private Sub Form_Load()
Set di = dx.DirectInputCreate()
If Err.Number <> 0 Then
MsgBox "Direct Input设置错误,请确认再你的系统中是否安装了DirectX", _
vbApplicationModal
End
End If
'建立一个键盘DirectInput对象
Set diDEV = di.CreateDevice("GUID_SysKeyboard")
Set diMouse = di.CreateDevice("GUID_SysMouse")
diDEV.SetCommonDataFormat DIFORMAT_KEYBOARD
diDEV.SetCooperativeLevel Me.hWnd, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE
diMouse.SetCommonDataFormat DIFORMAT_MOUSE
diMouse.SetCooperativeLevel Me.hWnd, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE
Me.Show
diDEV.Acquire
diMouse.Acquire
ix = diSMouse.x
iy = diSMouse.y
iz = diSMouse.z
Timer1.Interval = 10 '设置敲击键盘的灵敏度
Timer1.Enabled = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
'程序结束后释放DirectInput对象
diDEV.Unacquire
diMouse.Unacquire
End Sub
Private Sub Timer1_Timer()
'获得当前的击键值
diDEV.GetDeviceStateKeyboard diState
For iKeyCounter = 0 To 255
'判断是否敲击了某键
If diState.Key(iKeyCounter) <> 0 Then
Label1.Caption = iKeyCounter & " - 击键时间为: (" & Time & ")"
End If
Next
diMouse.GetDeviceStateMouse diSMouse
If diSMouse.x <> 0 Then
ix = diSMouse.x
End If
If diSMouse.y <> 0 Then
iy = diSMouse.y
End If
If diSMouse.z <> 0 Then
iz = diSMouse.z
End If
Form1.Caption = "X:" & ix & " Y:" & iy & " Z:" & iz
DoEvents
End Sub
运行程序,敲击键盘的任意键,可以看到键值显示在了Label1上,而移动鼠标,当前鼠标坐标会显示在Form1的标题条上(光标的坐标原点是程序运行时光标所在的位置,而如果你的鼠标有滚轮的话,则Z坐标由你的滚轮的滚动所决定)。而不论窗口处于前台或者后台,击键和鼠标动作都会倍记录下来,你可以利用上面的程序稍加修改建立自己的鼠标键盘Hook程序。
四、 DirectSound
DirectSound类对象是DirectX中播放、捕捉数字声音对象并且对声音在虚拟的三维控件内进行定位。
它主要包括以下的几个对象:
DirectSound
DirectSoundBuffer
DirectSound3DBuffer
Direct3DsoundListner
DirectSoundCapture
DirectSoundCaptureBuffer
DirectSound对象和DirectSoundCapture对象是DirectSound类对象的基本对象,DirectSound对象是音频播放基本对象,而DirectSoundCapture对象是音频捕捉基本对象。利用DirectX7对象的DirectSoundCreate方法和DirectSoundCaptureCreate方法可以分别建立DirectSound对象和DirectSoundCapture对象。
DirectSoundBuffer对象和DirectSound3Dbuffer是音频播放缓冲对象,顾名思义,前者是音频播放对象,而后者是三维音频播放对象。Direct3DSoundListner对象用于设置播放三维音频时收听者所处的位置。
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!



