使用 BitBlt 帮助双缓冲 VB6
编辑:我更新了版本 2。现在它是单色的。 我尝试通过确保使用窗口的 DC 而不是 memdc (如所写)调用 CreateCOmpatibleBitmap 来修复它,但它仍然是错误的:(
下面是我编写的 3 个不同的简化版本的函数。 Scalemode 设置为 vbPixels 有什么问题?
版本
Private Sub Form_Paint()
Me.Cls
DrawStuff Me.hDc
End Sub
3 用黑色填充整个表单。版本2 的
Private Sub Form_Paint()
Me.Cls
If m_HDCmem = 0 then
m_HDC = GetDC(hwnd)
m_HDCmem = CreateCompatibleDC(m_HDC)
m_HBitmap = CreateCompatibleBitmap(m_HDC, Me.ScaleWidth, Me.ScaleHeight)
ReleaseDC Null, m_HDC
SelectObject m_HDCmem, m_HBitmap
End If
DrawStuff m_HDCmem
Debug.Print BitBlt(Me.hDc, 0, 0, Me.ScaleWidth, Me.ScaleHeight, m_HDCmem, 0, 0, SRCCOPY) 'During testing, this printed "1"
Me.Refresh
End Sub
1 工作正常(但显然有闪烁),版本 2 没有任何作用,版本 3:
Private Sub Form_Paint()
Me.Cls
If m_HDC = 0 Then m_HDC = CreateCompatibleDC(Me.hDc)
DrawStuff m_HDC
BitBlt(Me.hDc, 0, 0, Me.ScaleWidth, Me.ScaleHeight, m_HDC, 0, 0, BLACKNESS) 'During testing, this printed "1"
Me.Refresh
End Sub
注意:我在调用 Paint 之前将下面的代码粘贴到了调整大小函数中,这没有帮助,但我很确定我应该将其留在那里:
If m_HDC <> 0 Then DeleteDC m_HDC
m_HDC = 0
Edit: I updated version 2. Now It's monochrome. I tried to fix it by making sure to call CreateCOmpatibleBitmap
with the window's DC rather than the memdc (as written), but it is still wrong :(
Below are 3 different simplified versions of functions I have written. Version 1 works perfectly (but has flicker, obviously), version 2 does nothing, and version 3 fills the entire form with black. What is wrong with version 2? Scalemode is set to vbPixels.
Version 1:
Private Sub Form_Paint()
Me.Cls
DrawStuff Me.hDc
End Sub
Version 2 (new):
Private Sub Form_Paint()
Me.Cls
If m_HDCmem = 0 then
m_HDC = GetDC(hwnd)
m_HDCmem = CreateCompatibleDC(m_HDC)
m_HBitmap = CreateCompatibleBitmap(m_HDC, Me.ScaleWidth, Me.ScaleHeight)
ReleaseDC Null, m_HDC
SelectObject m_HDCmem, m_HBitmap
End If
DrawStuff m_HDCmem
Debug.Print BitBlt(Me.hDc, 0, 0, Me.ScaleWidth, Me.ScaleHeight, m_HDCmem, 0, 0, SRCCOPY) 'During testing, this printed "1"
Me.Refresh
End Sub
Version 3:
Private Sub Form_Paint()
Me.Cls
If m_HDC = 0 Then m_HDC = CreateCompatibleDC(Me.hDc)
DrawStuff m_HDC
BitBlt(Me.hDc, 0, 0, Me.ScaleWidth, Me.ScaleHeight, m_HDC, 0, 0, BLACKNESS) 'During testing, this printed "1"
Me.Refresh
End Sub
Note: I stuck the code below in my resize function immediately before the call to paint. It did not help, but I'm pretty sure I should leave it there:
If m_HDC <> 0 Then DeleteDC m_HDC
m_HDC = 0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在版本 2 和 3 您对 CreateCompatibleDC() 的调用构建了一个 1 像素 x 1 像素的单色绘图表面。 您需要在其中的某个位置调用 CreateCompatibleBitmap() 。
请参阅此处
in Version 2 & 3 your call to CreateCompatibleDC() builds a monochrome drawing surface that is 1 pixel by 1 pixel. You need to call CreateCompatibleBitmap() somewhere in there.
see here