网页功能: 加入收藏 设为首页 网站搜索  
用VB编写DDraw程序初步
发表日期:2005-11-09作者:[转贴] 出处:applevb.com  

DirectX7.0终于出现了,同前面DirectX6相同,版本7也带了一个庞大(129M)的SDK开发库,同DirectX6 SDK库相比,DirectX7的SDK库提供了以下新的功能
l 对于Visual Basic的支持。用户可以使用类库在Visual Basic环境下开发基于DirectX的程序。
l 提供更多DirectX3D立即模式(Immediate Mode)下API函数,以支持DirectX7中新的3D特效,包括立体环境映射、顶点混合等。
l DirectMusic支持DownLoadable Sound Level 2标准。
l DirectInput支持8按键的游戏杆设备,同时支持Microsoft的力反馈摇杆。SDK库提供了读取力反馈效果文件的方法。同时提供了Force Editor程序来建立效果。
对于VB爱好者来说,新的SDK库终于提供了完整的对VB的支持,现在终于可以使用Visual Basic来编写DirectX的程序了。
一、DirectX SDK库的安装
微软提供的SDK库是一个“重”达129M的dx7sdk.exe自解压缩文件,你可以上网下载或者从配套光盘上获得这个文件。双击文件就会弹出Winzip自解压缩对话框。在弹出的WinZip Self-Extract DK7SDK.EXE窗口中输入解压缩文件的路径,然后点击“Unzip”按钮解压缩SDK文件,解压缩界面如图1-1所示:

要注意的是,dx7sdk.exe解压缩之后的体积有220M,硬盘比较紧张的读者在解压缩之前首先看看你的硬盘的容量是否足够。
解压缩完毕之后,进入解压缩的目录中,双击Setup.exe文件就可以安装DirectX7.0 SDK文件了。安装是采用标准的InstallShield界面,玩Windows的读者应该对这种安装界面驾轻就熟,只要跟着安装提示一步一步的走就可以了。安装完毕之后,安装程序会在开始菜单中添加一个Microsoft DirectX 7 SDK的菜单,其中包括了DirectX 7设置工具、VB范例和SDK Help等菜单项。
现在开始进入VB,开始我们的DirectX的VB编程。在这里我们使用的是VB6企业版(英文)。Windows98中文版。
打开VB,点击菜单中的 Project | References 项,在Object Library 列表中会有一项:DirectX 7.0 For Visual Basic Type Library 列表项,这个就是DirectX7.0 VB类库,选中该项,再选“ok”按钮,就可以将库加入工程文件中。

二、DirectX编程初步
1 DirectX7对象
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


运行程序,分别点击不同的按钮,在列表框中就会出现相应的设备驱动名和描述。
2 DirectDraw7对象
DirectDraw是一个与Windows 图形系统接口(GDI)相兼容的直接操作显示设备的软件接口。DirectDraw提供与硬件无关性的同时允许直接操作显存。程序只要使用一些基本的标准硬件约定如:RGB及YUV色彩格式及解析度。你无须调用特殊的过程来使用显存块移动(Blitter)及调色板。使用DirectDraw,你可简单操作显存,完全使用各种硬件特性而不必理会各种不同硬件之间的差异。
2.1 建立DirectDraw对象
DirectDraw7对象是DirectX7中的DirectDraw对象,你需要首先建立一个DirectX7对象,然后使用该对象的DirectDrawCreate方法来建立DirectDraw7对象。例如:

 

 Dim DX As New DirectX7
 Dim DDraw As DirectDraw7
 Set DDraw = DX.DirectDrawCreate("")


2.2 建立协作层
当建立了一个DirectDraw对象之后,首先要设定DirectDraw的协作层。实现的方法是调用DirectDraw对象的SetCooperativeLevel函数。该函数的定义是:
object.SetCooperativeLevel( hdl As Long, flags As CONST_DDSCLFLAGS)
其中参数hdl指定程序的窗口句柄,参数flag决定程序运行的方式,函数调用
DDraw.SetCooperativeLevel Me.hWnd, DDSCL_NORMAL
将使程序运行于普通的协作层既窗口模式之下。在这种协作层你无法改变主平面调色板或进行页交换,因为程序可以使用多窗口。而函数调用
DDraw.SetCooperativeLevel MainForm.hWnd, DDSCL_EXCLUSIVE Or _
DDSCL_FULLSCREEN
将使程序运行于全屏幕模式之下。在全屏幕协作模式之下你可以完全使用硬件的一切。在这个模式之下,你可以设置使用定义及动态调色板,改变显示分辨率及进行页交换。
2.3 设置显示模式
设置显示模式是使用SetDispalyMode函数实现的,函数的定义如下:

 

 object.SetDisplayMode( _
     w As Long, _
     h As Long, _
     bpp As Long, _
     ref As Long, _
     mode As CONST_DDSDMFLAGS


其中参数w、h分别指定屏幕的宽度和高度,bpp指定屏幕显示的颜色位数,参数ref指定屏幕的刷新频率,设置为0使用显示驱动的缺省刷新频率,mode指定附加的参数。要获得系统支持的显示模式,可以使用DirectDraw对象的GetDisplayModesEnum函数来遍历所有支持的显示模式。
2.4 建立平面对象
一个平面或者说DirectDrawSurface对象是DirectDraw中图形显示和绘制对象。用户可以在DirectDrawSurface上贴位图、绘制图形,还可以直接操作DirectDrawSurface对象使用的显存里的内容。利用DirectDraw对象的CreateSurface方法可以建立一个DirectDrawSurface7对象。例如:


 Public DDSFrontDesc As DDSURFACEDESC2

 With DDSFrontDesc
        .lFlags = DDSD_CAPS
        .ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE '
    End With
Set DDSFront = DDraw.CreateSurface(DDSFrontDesc)


也可以利用DirectDraw对象的CreateSurfaceFromFile函数或者CreateSurfaceFromResource函数建立一个DirectDrawSurface7对象,同时将图象文件或者资源文件中的图象装入建立的DirectDrawSurface中。如果上面的函数调用成功,函数将返回一个DirectDrawSurface对象。如果在设定DirectDraw对象的协作层时将其设置为全屏幕模式的话,为了改善图象性能,可以设立一个主平面和若干个屏下缓冲平面,首先在屏下平面中生成图象,然后将图象翻转到主平面上,这样可以有效的避免图象闪烁。
下面通过一个具体的范例来对DirectDraw进行说明:这个范例建立一个全屏幕的DirectDraw对象,通过操作主显示平面的显示内存在屏幕上显示火焰字的特效,然后按Enter键可以将DirectDraw平面中的图形保存起来。程序的具体实现如下:
建立一个新的工程文件,点击菜单中的 Project | Reference 选项,打开Object Library 列表窗口,将DirectX 7.0 For Visual Basic Type Library 加入工程文件。将Form1的Name属性改变为MainForm,在MainForm中加入一个PictureBox控件,将其的Visible属性设置为False。然后在MainForm的代码窗口中加入以下代码:

 

Private Sub Form_KeyPress(KeyAscii As Integer)
    Dim sRect As RECT
    Dim hdcSrc As Long
   
    If KeyAscii = 27 Then
        ExitLoop = True
        'End
    ElseIf KeyAscii = vbKeyReturn Then
        DDSFront.BltToDC Picture1.hDC, sRect, sRect
        With Picture1
            '获得与主显示平面兼容的图形设备句柄
            hdcSrc = DDSFront.GetDC
            '保存图象
            Set .Picture = SaveTohBmp(hdcSrc, 0, 0, 640, 480)
            '释放图形句柄
            DDSFront.ReleaseDC hdcSrc
            SavePicture Picture1, "c:\a.bmp"
        End With
    End If
End Sub

Public Sub Form_Paint()
    BlitRect.Right = DDSBackDesc.lWidth
    BlitRect.Bottom = DDSBackDesc.lHeight
    DDSFront.Blt BlitRect, DDSBack, BlitRect, DDBLT_WAIT
End Sub
 在工程文件中加入一个Module文件,这个文件中将对DirectDraw操作做出定义,中加入以下代码:
Option Explicit
Public DX As New DirectX7
Public DDraw As DirectDraw7
Public DDSFront As DirectDrawSurface7
Public DDSFrontDesc As DDSURFACEDESC2
Public DDSBack As DirectDrawSurface7
Public DDSBackDesc As DDSURFACEDESC2
Public Clipper As DirectDrawClipper
Dim Pict() As Byte
Dim AlphaRect As RECT
Dim X As Long, Y As Long
Dim Temp As Long
Dim Index As Long
Dim Index2 As Long
Dim Pos As Long
Dim PosPlus1 As Long
Dim PosPlus2 As Long
Dim PosPlus3 As Long
Public Pal(255) As PALETTEENTRY
Public Palette As DirectDrawPalette
Public BlitRect As RECT
Public FullSize As Boolean
Public ExitLoop As Boolean
Dim Accum As Long
Dim Msg(9) As String
Dim Counter As Long
Dim MsgIndex As Long
Dim bDrawText As Boolean
Dim lastTime As Long
Dim XPos As Long, YPos As Long
Dim wait As Long
Dim Angle As Single
Dim Flag As Boolean
Dim Count As Long
Dim CurModeActiveStatus As Boolean
Dim bRestore As Boolean
Dim Mode As Boolean
Private Sub Main()
   
    InitializeDX

    '初始化Picture1以获得DirectDraw界面图象
    With MainForm.Picture1
        .Width = 640 * Screen.TwipsPerPixelX
        .Height = 480 * Screen.TwipsPerPixelY
    End With
   
    DDSBack.SetForeColor RGB(255, 255, 255)
    MainForm.Font.Name = "宋体"
    DDSBack.SetFont MainForm.Font
   
    Msg(0) = "一个显示火焰字的演示"
    Msg(1) = "演示"
    Msg(2) = "利用VB阵列"
    Msg(3) = "对显示内存"
    Msg(4) = "进行直接存取"
    Msg(5) = "{Esc}键推出"

    '设置8位的调色板
    For Index = 0 To 84
        Pal(Index + 1).red = Index * 3 + 3
        Pal(Index + 1).green = 0
        Pal(Index + 1).blue = 0
   
        Pal(Index + 86).red = 255
        Pal(Index + 86).green = Index * 3 + 3
        Pal(Index + 86).blue = 0
   
        Pal(Index + 171).red = 255
        Pal(Index + 171).green = 255
        Pal(Index + 171).blue = Index * 3 + 3
    Next
   
    Set Palette = DDraw.CreatePalette(DDPCAPS_8BIT _
        Or DDPCAPS_ALLOW256, Pal())
   
    DDSFront.SetPalette Palette
   
   
    AlphaRect.Right = DDSBackDesc.lWidth - 1
    AlphaRect.Bottom = DDSBackDesc.lHeight - 1
   
    DDSBack.Lock AlphaRect, DDSBackDesc, DDLOCK_WAIT, 0
   
    DDSBack.GetLockedArray Pict()
   
   
    For X = 0 To 639
        For Y = 0 To 479
            Pict(X, Y) = 0
        Next
    Next
   
    'Corresponding unlock
    DDSBack.Unlock AlphaRect
   
    While Not ExitLoop
       
        Mode = ExModeActive
       
        bRestore = False
        Do Until ExModeActive
            DoEvents
            bRestore = True
        Loop
        DoEvents
       
        If bRestore Then
            bRestore = False
            DDraw.RestoreAllSurfaces
        End If
       
        DDSBack.Lock AlphaRect, DDSBackDesc, DDLOCK_WAIT, 0
       
        DDSBack.GetLockedArray Pict()
   
        For Y = 0 To 479
            Pict(0, Y) = 0
            Pict(639, Y) = 0
        Next
        For X = 0 To 639
            Pict(X, 477) = Rnd * 220 + 35
            Pict(X, 478) = Rnd * 220 + 35
            Pict(X, 479) = Rnd * 220 + 35
        Next
       
        Accum = 0
        For X = 1 To 638
            For Y = 0 To 477
                Accum = (Accum + Pict(X, Y + 1) _
                    + Pict(X, Y + 2) _
                    + Pict(X + 1, Y + 1) _
                    + Pict(X - 1, Y + 1)) \ 5
                    If Accum < 0 Then
                        Accum = 0
                    ElseIf Accum > 255 Then
                        Accum = 255
                    End If
                Pict(X, Y) = Accum
            Next
        Next
       
        For X = 0 To 639
            Pict(X, 0) = 0
            Pict(X, 1) = 0
        Next
        X = Rnd * 639
        For Y = 50 To 439
        Next
        'Unlock
        DDSBack.Unlock AlphaRect
       
        If DX.TickCount() - lastTime > wait Then
            If Counter = 0 Then
                bDrawText = True
                Counter = 1
                XPos = Rnd * 200
                YPos = 300 + Rnd * 140
                wait = 400
            ElseIf Counter = 1 Then
                MsgIndex = MsgIndex + 1
                If MsgIndex > 5 Then MsgIndex = 0
                bDrawText = False
                Counter = 0
                wait = 2000
            End If
            lastTime = DX.TickCount
        End If
       
        'Draw Text to the backbuffer
        If bDrawText Then
            On Error Resume Next
            DDSBack.DrawText XPos, YPos, Msg(MsgIndex), False
            On Error GoTo 0
        End If
       
        MainForm.Form_Paint
       
    Wend
   
    TerminateDX
    End
End Sub
Function ExModeActive() As Boolean
    Dim TestCoopRes As Long
    TestCoopRes = DDraw.TestCooperativeLevel
    Select Case TestCoopRes
        Case DDERR_NOEXCLUSIVEMODE
            ExModeActive = False
        Case DD_OK
            ExModeActive = True
    End Select
End Function
Public Sub InitializeDX()
    
    MainForm.Left = 0
    MainForm.Top = 0
    MainForm.Height = 640 * Screen.TwipsPerPixelY
    MainForm.Width = 480 * Screen.TwipsPerPixelX
    MainForm.Show
   
    '建立DirectDraw对象
    Set DDraw = DX.DirectDrawCreate("")
    '设定DirectDraw对象的协作层
    DDraw.SetCooperativeLevel MainForm.hWnd, DDSCL_EXCLUSIVE Or DDSCL_FULLSCREEN ' DDSCL_NORMAL
   
    '设定显示模式位640x480x8位颜色
    DDraw.SetDisplayMode 640, 480, 8, 0, DDSDM_DEFAULT
   
    '设定DDSFrontDesc为主平面
    With DDSFrontDesc
        .lFlags = DDSD_CAPS
        .ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE 'Or DDSCAPS_SYSTEMMEMORY
    End With
    '设定DDSBackDesc为后台缓冲平面
    With DDSBackDesc
        .ddsCaps.lCaps = DDSCAPS_SYSTEMMEMORY
        .lFlags = DDSD_CAPS Or DDSD_WIDTH Or DDSD_HEIGHT
        .lWidth = 640
        .lHeight = 480
    End With
    '建立平面
    Set DDSFront = DDraw.CreateSurface(DDSFrontDesc)
    Set DDSBack = DDraw.CreateSurface(DDSBackDesc)
   
    Set Clipper = DDraw.CreateClipper(0)
    Clipper.SetHWnd MainForm.hWnd
    DDSFront.SetClipper Clipper
    DDSBack.SetClipper Clipper
   
    DoEvents
    Exit Sub
ERRoUT:
    If Not (DDraw Is Nothing) Then
        DDraw.RestoreDisplayMode
        DDraw.SetCooperativeLevel MainForm.hWnd, DDSCL_NORMAL
        DoEvents
    End If
    MsgBox "无法对DirectDraw进行初始化 " + Chr(13) + "也许你的显示卡不支持 640x480x8 显示模式 "
    End
End Sub
Public Sub TerminateDX()
    '子程序TerminateDX回复原来的显示模式并且释放所有的DirectDraw有关对象
    DDraw.RestoreDisplayMode
    DDraw.SetCooperativeLevel MainForm.hWnd, DDSCL_NORMAL
    DoEvents
    Set Clipper = Nothing
    Set DDSBack = Nothing
    Set DDSFront = Nothing
    Set DDraw = Nothing
    Set DX = Nothing
End Sub
 在工程文件中在加入一个Module,这个Module主要定义与图象保存相关的操作,加入以下代码:
 Option Explicit
Option Base 0

Private Type PALETTEENTRY
   peRed As Byte
   peGreen As Byte
   peBlue As Byte
   peFlags As Byte
End Type

Private Type LOGPALETTE
   palVersion As Integer
   palNumEntries As Integer
   palPalEntry(255) As PALETTEENTRY  ' Enough for 256 colors.
End Type

Private Type GUID
   Data1 As Long
   Data2 As Integer
   Data3 As Integer
   Data4(7) As Byte
End Type

Private Const RASTERCAPS As Long = 38
Private Const RC_PALETTE As Long = &H100
Private Const SIZEPALETTE As Long = 104

Private Declare Function CreateCompatibleDC Lib "GDI32" _
        (ByVal hDC As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "GDI32" _
        (ByVal hDC As Long, _
        ByVal nWidth As Long, _
        ByVal nHeight As Long) As Long
Private Declare Function GetDeviceCaps Lib "GDI32" _
        (ByVal hDC As Long, _
        ByVal iCapabilitiy As Long) As Long
Private Declare Function GetSystemPaletteEntries Lib "GDI32" _
        (ByVal hDC As Long, _
        ByVal wStartIndex As Long, _
        ByVal wNumEntries As Long, _
        lpPaletteEntries As PALETTEENTRY) As Long
Private Declare Function CreatePalette Lib "GDI32" _
        (lpLogPalette As LOGPALETTE) As Long
Private Declare Function SelectObject Lib "GDI32" _
        (ByVal hDC As Long, _
        ByVal hObject As Long) As Long
Private Declare Function BitBlt Lib "GDI32" _
        (ByVal hDCDest As Long, _
        ByVal XDest As Long, _
        ByVal YDest As Long, _
        ByVal nWidth As Long, _
        ByVal nHeight As Long, _
        ByVal hdcSrc As Long, _
        ByVal XSrc As Long, _
        ByVal YSrc As Long, _
        ByVal dwRop As Long) As Long
Private Declare Function DeleteDC Lib "GDI32" _
        (ByVal hDC As Long) As Long
Private Declare Function GetForegroundWindow Lib "USER32" () As Long
Private Declare Function SelectPalette Lib "GDI32" _
        (ByVal hDC As Long, _
        ByVal hPalette As Long, _
        ByVal bForceBackground As Long) As Long
Private Declare Function RealizePalette Lib "GDI32" _
        (ByVal hDC As Long) As Long
Private Declare Function GetWindowDC Lib "USER32" _
        (ByVal hWnd As Long) As Long
Private Declare Function GetDC Lib "USER32" _
        (ByVal hWnd As Long) As Long
Private Declare Function GetWindowRect Lib "USER32" _
        (ByVal hWnd As Long, _
        lpRect As RECT) As Long
Private Declare Function ReleaseDC Lib "USER32" _
        (ByVal hWnd As Long, _
        ByVal hDC As Long) As Long
Private Declare Function GetDesktopWindow Lib "USER32" () As Long


Private Type PicBmp
   Size As Long
   Type As Long
   hBmp As Long
   hPal As Long
   Reserved As Long
End Type

Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As _
 GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long

Public Function SaveTohBmp(ByVal hdcSrc As Long, ByVal LeftSrc As Long, _
    ByVal TopSrc As Long, ByVal WidthSrc As Long, ByVal HeightSrc As Long) As Picture

  Dim hDCMemory As Long
  Dim hBmp As Long
  Dim hBmpPrev As Long
  Dim r As Long
  Dim hPal As Long
  Dim hPalPrev As Long
  Dim RasterCapsScrn As Long
  Dim HasPaletteScrn As Long
  Dim PaletteSizeScrn As Long
  Dim LogPal As LOGPALETTE
 
  '

  
   '建立一个内存图形设备句柄
   hDCMemory = CreateCompatibleDC(hdcSrc)
   '建立一个bitmap并保存到hDCMemory中
   hBmp = CreateCompatibleBitmap(hdcSrc, WidthSrc, HeightSrc)
   hBmpPrev = SelectObject(hDCMemory, hBmp)

   ' Get screen properties.
   RasterCapsScrn = GetDeviceCaps(hdcSrc, RASTERCAPS) ' Raster
                                                      ' capabilities.
   HasPaletteScrn = RasterCapsScrn And RC_PALETTE       ' Palette
                                                        ' support.
   PaletteSizeScrn = GetDeviceCaps(hdcSrc, SIZEPALETTE) ' Size of
                                                        ' palette.

   If HasPaletteScrn And (PaletteSizeScrn = 256) Then
      '建立系统调色板的拷贝
      LogPal.palVersion = &H300
      LogPal.palNumEntries = 256
      r = GetSystemPaletteEntries(hdcSrc, 0, 256, LogPal.palPalEntry(0))
      hPal = CreatePalette(LogPal)
     
      hPalPrev = SelectPalette(hDCMemory, hPal, 0)
      r = RealizePalette(hDCMemory)
   End If

   '将屏幕图形拷贝到内存图形设备句柄中
   r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hdcSrc, LeftSrc, TopSrc, vbSrcCopy)

   hBmp = SelectObject(hDCMemory, hBmpPrev)

   If HasPaletteScrn And (PaletteSizeScrn = 256) Then
      hPal = SelectPalette(hDCMemory, hPalPrev, 0)
   End If

   '释放图形设备句柄
   r = DeleteDC(hDCMemory)
   Debug.Print r
  
   '调用CreateBitmapPicture函数从指定的bitmap对象和调色板中建立一个picture对象
   Set SaveTohBmp = CreateBitmapPicture(hBmp, hPal)
End Function
Public Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) As Picture
   Dim r As Long
   Dim Pic As PicBmp
   Dim IPic As IPicture
   Dim IID_IDispatch As GUID

   '填充IDispatch界面
   With IID_IDispatch
      .Data1 = &H20400
      .Data4(0) = &HC0
      .Data4(7) = &H46
   End With

   '填充Pic结构
   With Pic
      .Size = Len(Pic)          ' Length of structure.
      .Type = vbPicTypeBitmap   ' Type of Picture (bitmap).
      .hBmp = hBmp              ' Handle to bitmap.
      .hPal = hPal              ' Handle to palette (may be null).
   End With

   '建立Picture对象
   r = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)

   '返回Picture对象
   Set CreateBitmapPicture = IPic
End Function


运行程序,在屏幕上会出现一些火焰字的特效,按Enter键可以将屏幕保存到”c:\a.bmp”中,按Esc键退出程序回到Windows。
在上面的程序中,程序首先建立一个DirectDraw对象,然后设置该对象的协作层为全屏协作模式,接下来设置显示模式为640x480x8位颜色,建立一个前台DirectDrawSurface对象和一个后台缓冲DirectDrawSurface对象,建立和设置DirectDrawClipper对象。
在主程序段中,程序首先对前台绘图平面的调色板(DirectDrawPalette)对象进行操作以改变显示的文字的颜色,然后对后台缓冲绘图平面进行字节操作,以产生文字弥散的效果,然后在将后台缓冲绘图平面翻转到前台。当用户按下Enter键之后,程序获得与前台绘图平面相兼容的图形设备句柄,然后再调用Windows API函数将绘图平面内存中的内容保存到Windows位图文件中。
上面粗略的介绍了DirectX7 SDK的新特性以及初步的DirectDraw编程,希望对大家能有所帮助。以上的程序在Windows98、VB6.0下运行通过。

 

 

我来说两句】 【加入收藏】 【返加顶部】 【打印本页】 【关闭窗口
中搜索 用VB编写DDraw程序初步
本类热点文章
  VB编写DirectX简明教程
  真正删除数据库的记录
  用VB编写DirectX7.0游戏(上)
  用VB编写DDraw程序初步
  用VB编写DirectX7.0游戏(下)
最新分类信息我要发布 
最新招聘信息

关于我们 / 合作推广 / 给我留言 / 版权举报 / 意见建议 / 广告投放  
Copyright ©2003-2019 Lihuasoft.net webmaster(at)lihuasoft.net
网站编程QQ群   京ICP备05001064号 页面生成时间:0.00395