转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
DirectX 7 编程初步         ★★★★

DirectX 7 编程初步

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2502 更新时间:2009/4/23 18:58:54
      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
   ''''设定显示模式位640×480×8位颜色
   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)+“也许你的显示卡不支持 640×480×8 显示模式 ”
    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主要定义与图像保存相关的操作,在建立的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 LOOGPALETTE

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

  '''' Get screen properties.
   RasterCapsScrn = GetDeviceCaps(hdcSrc, RASTERCAPS) ''''Raste capabilities.
   HasPaletteScrn = RasterCapsScrn And RC_PALtTTEic1 '''' 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对象,然后设置该对象的协作层为全屏协作模式,接下来设置显示模式为640×480×8位颜色,建立一个前台DirectDrawSurface对象和一个后台缓冲DirectDrawSurface对象,建立和设置DirectDrawClipper对象。
  在主程序段中,程序首先对前台绘图平面的调色板(DirectDrawPalette)对象进行操作以改变显示的文字的颜色,然后对后台缓冲绘图平面进行字节操作,以产生文字弥散的效果,然后再将后台缓冲绘图平面翻转到前台。当用户按下Enter键之后,程序获得与前台绘图平面相兼容的图形设备句柄,然后再调用Windows API函数将绘图平面内存中的内容保存到Windows位图文件中。

  上面粗略地介绍了DirectX7 SDK的新特性以及初步的DirectDraw编程,希望对大家能有所帮助。以上的程序在Windows98、VB6.0下运行通过。

上一页  [1] [2] 


[聊天工具]让IE 7也用上鼠标手势  [常用软件]IE 7出炉,Firefox 2当道.谁才是“王者”?
[常用软件]绕过WGA安装IE 7 Beta3 5450  [VB.NET程序]VB.NET实现DirectSound9 (7) 录音
[VB.NET程序]VB程序员眼中的C# 7  [Delphi程序]用DELPHI开发DirectX游戏
[Delphi程序]用Delphi + DirectX开发简单RPG游戏  [Delphi程序]DELPHI7 如何在编写可视组件中传递一个事件到组件…
[Delphi程序]Delphi7 的 WebService 与 数据库  [Delphi程序]Delphi7 从子线程中发送消息到主线程触发事件执行
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台