Results 1 to 4 of 4

Thread: Double buffering with Memory DC

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    Gloucestershire, England
    Posts
    301

    Thumbs down

    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:

    Code:
    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


  2. #2
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Try this function to load an empty DC:

    Code:
    '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 )

  3. #3
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553
    I always use this kind of code:

    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

  4. #4
    Member
    Join Date
    Jun 2000
    Location
    UK
    Posts
    49
    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.
    Dave.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width