VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)

VB爱好者乐园(VBGood)

 找回密码
 立即注册
搜索
查看: 2553|回复: 3

[讨论] VB 双缓冲 如何 避免闪烁?

[复制链接]
发表于 2012-7-27 14:11:56 | 显示全部楼层 |阅读模式
按理说,双缓冲绘图应该不闪烁才对的呀。为什么还是感觉闪烁很厉害?
  1. Option Explicit

  2. Private Type RECT
  3.     Left As Long
  4.     Top As Long
  5.     Right As Long
  6.     Bottom As Long
  7. End Type
  8. Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
  9. Private Declare Function FillRect Lib "user32" (ByVal hDC As Long, lpRect As RECT, ByVal hBrush As Long) As Long
  10. Private Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
  11. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
  12. Private Declare Function SetBkMode Lib "gdi32" (ByVal hDC As Long, ByVal nBkMode As Long) As Long
  13. Private Declare Function DrawTextW Lib "user32" (ByVal hDC As Long, ByVal lpStr As Long, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
  14. Private Const DT_CALCRECT = &H400
  15. Private Const DT_LEFT = &H0
  16. Private Const DT_SINGLELINE = &H20
  17. Private Const DT_NOPREFIX = &H800
  18. Private Const DT_CENTER = &H1
  19. Private Const DT_VCENTER = &H4
  20. Private Const DT_TOP = &H0
  21. Private Const DT_RIGHT = &H2
  22. Private Declare Function CreateCompatibleDC Lib "gdi32.dll" (ByVal hDC As Long) As Long
  23. Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
  24. Private Declare Function SelectObject Lib "gdi32.dll" (ByVal hDC As Long, ByVal hObject As Long) As Long
  25. Private Declare Function DeleteDC Lib "gdi32.dll" (ByVal hDC As Long) As Long
  26. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  27. Private Declare Function SetTextColor Lib "gdi32" (ByVal hDC As Long, ByVal crColor As Long) As Long

  28. Private hMyDC   As Long
  29. Private hBmp    As Long
  30. Private MyStr(100)  As String
  31. Private lTop    As Long

  32. Private Sub Command1_Click()
  33.     Timer1.Enabled = Not Timer1.Enabled
  34. End Sub

  35. Private Sub Form_Load()
  36.     Me.ScaleMode = 3
  37.    
  38.     hMyDC = CreateCompatibleDC(Form1.hDC) '根据FORM1创建内存DC
  39.     hBmp = CreateCompatibleBitmap(Form1.hDC, 300, 2000)
  40.     DeleteObject SelectObject(hMyDC, hBmp)
  41.    
  42.     Dim i       As Integer
  43.     Const Str = "测试文字测试文字测试文字测试文字测试文字测试文字测试文字测试文字"
  44.     For i = 1 To 100
  45.         MyStr(i) = Left(Str, GetRnd(5, 32))
  46.     Next
  47.    
  48.     Timer1.Interval = 100
  49. End Sub

  50. Private Sub DrawTextB(vTop As Long)
  51.     '清除原来的内容
  52.     Dim r   As RECT
  53.     SetRect r, 0, 0, 300, 2000
  54.     FillRect hMyDC, r, CreateSolidBrush(&HE0E0E0)           '设置背景色

  55.     SetTextColor hMyDC, vbBlue
  56.     SetBkMode hMyDC, 1 'TRANSPARENT
  57.    
  58.     Dim i       As Integer
  59.    
  60.     For i = 1 To 100
  61.         r.Bottom = r.Top + 15
  62.         Call DrawTextW(hMyDC, StrPtr(MyStr(i)), -1, r, DT_CENTER Or DT_SINGLELINE Or DT_NOPREFIX Or DT_VCENTER)
  63.         r.Top = r.Top + 15
  64.     Next
  65.     BitBlt Me.hDC, 0, vTop, 300, 2000, hMyDC, 0, 0, &HCC0020    'SRCCOPY 将画好的内存DC映射到FORM2上
  66. End Sub

  67. Private Sub Form_Paint()
  68.     BitBlt Me.hDC, 0, lTop, 300, 2000, hMyDC, 0, 0, &HCC0020    'SRCCOPY 将画好的内存DC映射到FORM2上
  69. End Sub

  70. Private Sub Form_Unload(Cancel As Integer)
  71.     Erase MyStr
  72.     DeleteObject hBmp
  73.     DeleteDC hMyDC
  74. End Sub

  75. Private Sub Timer1_Timer()
  76.     lTop = lTop - 1
  77.     If lTop < -1515 Then
  78.         lTop = Form1.ScaleHeight
  79.     End If
  80.     Call DrawTextB(lTop)
  81. End Sub

  82. Private Function GetRnd(lowerbound As Integer, upperbound As Integer)
  83.     GetRnd = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
  84. End Function
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

点评

Win7 自带的显卡驱动,Intel(R) G41 Express Chipset (Microsoft Corporation - WDDM 1.1)。大神们倒是试试,帮找下原因呀....  发表于 2012-7-30 12:45
这样当然闪了  发表于 2012-7-27 19:44
难道LZ显卡驱动问题  发表于 2012-7-27 16:16
不会吧....  发表于 2012-7-27 15:08
在我这一点也不闪……  发表于 2012-7-27 14:19
发表于 2012-7-28 22:42:23 | 显示全部楼层
Form_Paint()中是会闪的,你拦一下WM_ERASEBKGND消息,在这个消息中绘制,就不会闪了,我以前写控件时都是用这种方法的。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-7-30 12:43:15 | 显示全部楼层
jy4977596491 发表于 2012-7-28 22:42
Form_Paint()中是会闪的,你拦一下WM_ERASEBKGND消息,在这个消息中绘制,就不会闪了,我以前写控件时都是用 ...

程序中的绘图主要是在 Timer1_Timer 中完成的,不是 Form_Paint 中擦除背景引起的闪烁
回复 支持 反对

使用道具 举报

发表于 2012-7-30 16:25:20 | 显示全部楼层
你的程序试了,没有闪烁现象,代码也没问题
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

文字版|手机版|小黑屋|VBGood  

GMT+8, 2022-9-25 17:04

VB爱好者乐园(VBGood)
快速回复 返回顶部 返回列表