Click to See Complete Forum and Search --> : Double buffering with Memory DC
Rick H
Oct 9th, 2000, 04:36 AM
I am writing a graph program that plots data in real time. At the moment I am using two picture boxes, I draw the data block (50ms worth of data) onto the background picturebox and then use BitBlt to copy this block onto the front picbox. This works great. However I am trying to replace to background picturebox with a memory dc.
This is the code I am using:
dim dc As Long ' Memory DC for double buffering
dim mempic As PictureBox ' Memory picture box used for double buffering
dc = CreateCompatibleDC(UserControl.hdc)
If dc = vbNull Then StatusBar1.SimpleText = "Failed to create memory DC"
Set mempic = PicMain ' copy picmain attributes
SelectObject dc, mempic
picmain is my front picturebox. I cant get this to work though. If I run run the usercontrol it comes up blank. Any ideas?
Cheers
Fox
Oct 9th, 2000, 12:17 PM
Try this function to load an empty DC:
'Types
Type tRect
'Coordinates
x As Long
y As Long
X2 As Long
Y2 As Long
End Type
'Declares
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 SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Public Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Public Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As tRect, ByVal hBrush As Long) As Long
'Create a DC
Function CreateDC(iW As Long, iH As Long) As Long
Dim Temp As Long
Dim Rect As tRect
'Create
CreateDC = CreateCompatibleDC(frmMain.hdc)
Temp = CreateCompatibleBitmap(frmMain.hdc, iW, iH)
'Get the DC
SelectObject CreateDC, Temp
DeleteObject Temp
'Clear
Rect.X2 = iW
Rect.Y2 = iH
FillRect CreateDC, Rect, 0
End Function
And keep up the good work! (use DCs instead of pics ;))
Mad Compie
Oct 9th, 2000, 02:15 PM
I always use this kind of code:
Option Explicit
Dim memDC As Long
Dim memBMP As Long
Sub Form_Load()
memDC = CreateCompatibleDC(0)
memBMP = CreateCompatibleBitmap(Me.hdc, ScrWidth, ScrHeight)
SelectObject memDC, memBMP
'... Do some actions
'i.e. this Blt copies the desktop into the memory DC
BitBlt memDC, 0, 0, ScrWidth, ScrHeight, GetDC(0), 0, 0, vbSrcCopy
'...
End Sub
Private Sub Form_Unload(Cancel As Integer)
DeleteDC memDC
DeleteObject memBMP
End Sub
dangerousdave
Oct 9th, 2000, 05:18 PM
Hmm,
Check Autoredraw properties are correct.
Also I noticed you didnt actually blit anything to the new picturebox.
I cant really put anything else that the other 2 havnt already put. Try using their methods. From my reckoning that should work.... I aint used BitBlt and the like in a while though so I could be rusty.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.