簡介
DirectX7對象是DirectX VB對象中其他所有對象的服務和起始對象,這個對象包含了建立諸如DirectDraw、 Direct3D、 DirectSound、 DirectInput等對象的方法。同時該對象還包含了一系列的三維控制項頂點和距陣的操作函式以及一些DirectX系統函式。在VB中可以通過Dim...New來直接定義和初始化一個DirectX7對象,例如:
Dim DirectX As New DirectX7
當建立成功一個DirectX7對象之後,就可以使用該對象的DirectDrawCreate、Direct3DRMCreate等方法建立DirectDraw、Direct3D對象了。
示例
DirectX7對象範例1:獲得系統中的DirectDraw和DirectSound驅動
建立一個新的工程檔案,點擊選單中的 Project | References 項,在Object Library 列表中選中DirectX 7.0 For Visual Basic Type Library 項後按確定按鈕(以下的程式都需要這個步驟,後面將不再做說明)。然後在Form1中加入一個ListBox控制項和四個CommandButton控制項,在Form1的代碼視窗中加入以下代碼:
Option Explicit
Dim DirectX As New DirectX7
Dim DDEnum As DirectDrawEnum
Dim DDSound As DirectSoundEnum
Private Sub Command1_Click()
Dim Count, i As Integer
Set DDEnum = DirectX.GetDDEnum
Count = DDEnum.GetCount
List1.Clear
For i = 1 To Count
List1.AddItem DDEnum.GetDescription(i)
Next i
Set DDEnum = Nothing
End Sub
Private Sub Command2_Click()
Dim Count, i As Integer
Set DDEnum = DirectX.GetDDEnum
Count = DDEnum.GetCount
List1.Clear
For i = 1 To Count
List1.AddItem DDEnum.GetName(i)
Next i
Set DDEnum = Nothing
End Sub
Private Sub Command3_Click()
Dim Count, i As Integer
Set DDSound = DirectX.GetDSEnum
Count = DDSound.GetCount
List1.Clear
For i = 1 To Count
List1.AddItem DDSound.GetDescription(i)
Next i
End Sub
Private Sub Command4_Click()
Dim Count, i As Integer
Set DDSound = DirectX.GetDSEnum
Count = DDSound.GetCount
List1.Clear
For i = 1 To Count
List1.AddItem DDSound.GetName(i)
Next i
End Sub
Private Sub Form_Load()
Command1.Caption = “DirectDraw驅動描述”
Command2.Caption = “DirectDraw驅動名稱”
Command3.Caption = “DirectSound驅動描述”
Command4.Caption = “DirectSound驅動名稱”
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set DirectX = Nothing
End Sub
運行程式,分別點擊不同的按鈕,在列表框中就會出現相應的設備驅動名和描述。