Results 1 to 6 of 6

Thread: [Resolved]hDC Picturebox Autoredraw

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    [Resolved]hDC Picturebox Autoredraw

    This maybe just noobness, but i'm confused.

    I thought that when the Autoredraw was set to true that if the picture box was moved off the screen, had something moved over it, or any thing such as that. The Autoredraw will allow the picture to stay intact.

    VB Code:
    1. Picture_.AutoRedraw = False
    2. r = StretchBlt(Picture_.hDC, 0, 0, w, h, hdcScreen, 0, 0, wScreen, hScreen, vbSrcCopy)
    3. Picture_.AutoRedraw = True

    This is how the image is being drawn onto the picturebox.

    The picture box automatically resizes to fit the image (such as the screen width and height). The Problem is that when i move the picturebox around the form (so that bits of it leave the form) it goes to the default background color (white in my case).

    How does this happen? I have autoredraw on (Debug.Print Picture_.Autoredraw). If i cause a msgbox to come up over the image, the image stays intact until i move the msgbox (Pressing OK wil keep it intact even though it was over the top of the image).

    Can anyone tell me a solution to this problem? This is the image that is going to be saved to a file. So the form won't come up all the time to the user, however the image will still be stored there and if they want to see a preview , only then will the image form show up.

    So in other words, the picturebox and the form it's located on will be hidden for most of the time.

    Does the hDC stop the autoredraw from working?
    Last edited by Slyke; Aug 17th, 2006 at 03:23 AM.

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: hDC Picturebox Autoredraw

    You must keep AutoRedraw = True at all times. AutoRedraw creates a new buffer that will automatically keep all drawn data in memory. This consumes memory and is slower than living without it, but in the other hand you don't need to constantly redraw picture in the Picture1_Paint event (which is where you probably would like to have your drawing code, but where you can't have it if you use AutoRedraw).


    So: you either keep AutoRedraw = True all the time or not at all.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: hDC Picturebox Autoredraw

    If i keep it on all the time and the user takes another screen shot, it comes up with an error saying that it can't draw onto an image that has autoredraw set to true, or somthign like that.

    "Can't Create Autoredraw image"

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: hDC Picturebox Autoredraw

    It's been so long time since I've been using AutoRedraw that I can't remember what caused that problem. But the other option that I could suggest would be to use API to create a new Device Context and store the picture there and then draw that to the picturebox only on request. You probably get started off by searching for CreateCompatibleDC, which makes it easier to create a new DC. I don't have VB on this computer so you're probably better off searching for that

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: hDC Picturebox Autoredraw

    Oh Damm...

    Well i'll do my best. www.allapi.com is a good help too!

    If any one else knows how to do this or another way to fix this please let me know.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: hDC Picturebox Autoredraw

    VB Code:
    1. Const RC_PALETTE As Long = &H100
    2. Const SIZEPALETTE As Long = 104
    3. Const RASTERCAPS As Long = 38
    4. Private Type PALETTEENTRY
    5.     peRed As Byte
    6.     peGreen As Byte
    7.     peBlue As Byte
    8.     peFlags As Byte
    9. End Type
    10. Private Type LOGPALETTE
    11.     palVersion As Integer
    12.     palNumEntries As Integer
    13.     palPalEntry(255) As PALETTEENTRY ' Enough for 256 colors
    14. End Type
    15. Private Type GUID
    16.     Data1 As Long
    17.     Data2 As Integer
    18.     Data3 As Integer
    19.     Data4(7) As Byte
    20. End Type
    21. Private Type PicBmp
    22.     Size As Long
    23.     Type As Long
    24.     hBmp As Long
    25.     hPal As Long
    26.     Reserved As Long
    27. End Type
    28. Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
    29. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
    30. Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
    31. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    32. Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal iCapabilitiy As Long) As Long
    33. Private Declare Function GetSystemPaletteEntries Lib "gdi32" (ByVal hdc As Long, ByVal wStartIndex As Long, ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) As Long
    34. Private Declare Function CreatePalette Lib "gdi32" (lpLogPalette As LOGPALETTE) As Long
    35. Private Declare Function SelectPalette Lib "gdi32" (ByVal hdc As Long, ByVal hPalette As Long, ByVal bForceBackground As Long) As Long
    36. Private Declare Function RealizePalette Lib "gdi32" (ByVal hdc As Long) As Long
    37. 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
    38. Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
    39. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    40. Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) As Picture
    41.     Dim R As Long, Pic As PicBmp, IPic As IPicture, IID_IDispatch As GUID
    42.  
    43.     'Fill GUID info
    44.     With IID_IDispatch
    45.         .Data1 = &H20400
    46.         .Data4(0) = &HC0
    47.         .Data4(7) = &H46
    48.     End With
    49.  
    50.     'Fill picture info
    51.     With Pic
    52.         .Size = Len(Pic) ' Length of structure
    53.         .Type = vbPicTypeBitmap ' Type of Picture (bitmap)
    54.         .hBmp = hBmp ' Handle to bitmap
    55.         .hPal = hPal ' Handle to palette (may be null)
    56.     End With
    57.  
    58.     'Create the picture
    59.     R = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)
    60.  
    61.     'Return the new picture
    62.     Set CreateBitmapPicture = IPic
    63. End Function
    64. Function hDCToPicture(ByVal hDCSrc As Long, ByVal LeftSrc As Long, ByVal TopSrc As Long, ByVal WidthSrc As Long, ByVal HeightSrc As Long) As Picture
    65.     Dim hDCMemory As Long, hBmp As Long, hBmpPrev As Long, R As Long
    66.     Dim hPal As Long, hPalPrev As Long, RasterCapsScrn As Long, HasPaletteScrn As Long
    67.     Dim PaletteSizeScrn As Long, LogPal As LOGPALETTE
    68.  
    69.     'Create a compatible device context
    70.     hDCMemory = CreateCompatibleDC(hDCSrc)
    71.     'Create a compatible bitmap
    72.     hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
    73.     'Select the compatible bitmap into our compatible device context
    74.     hBmpPrev = SelectObject(hDCMemory, hBmp)
    75.  
    76.     'Raster capabilities?
    77.     RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS) ' Raster
    78.     'Does our picture use a palette?
    79.     HasPaletteScrn = RasterCapsScrn And RC_PALETTE ' Palette
    80.     'What's the size of that palette?
    81.     PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE) ' Size of
    82.  
    83.     If HasPaletteScrn And (PaletteSizeScrn = 256) Then
    84.         'Set the palette version
    85.         LogPal.palVersion = &H300
    86.         'Number of palette entries
    87.         LogPal.palNumEntries = 256
    88.         'Retrieve the system palette entries
    89.         R = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry(0))
    90.         'Create the palette
    91.         hPal = CreatePalette(LogPal)
    92.         'Select the palette
    93.         hPalPrev = SelectPalette(hDCMemory, hPal, 0)
    94.         'Realize the palette
    95.         R = RealizePalette(hDCMemory)
    96.     End If
    97.  
    98.     'Copy the source image to our compatible device context
    99.     R = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, LeftSrc, TopSrc, vbSrcCopy)
    100.  
    101.     'Restore the old bitmap
    102.     hBmp = SelectObject(hDCMemory, hBmpPrev)
    103.  
    104.     If HasPaletteScrn And (PaletteSizeScrn = 256) Then
    105.         'Select the palette
    106.         hPal = SelectPalette(hDCMemory, hPalPrev, 0)
    107.     End If
    108.  
    109.     'Delete our memory DC
    110.     R = DeleteDC(hDCMemory)
    111.  
    112.     Set hDCToPicture = CreateBitmapPicture(hBmp, hPal)
    113. End Function
    114.  
    115.  
    116. Public Sub Screen_Shot_Event(Picture_ As PictureBox, Optional Active_Window As Boolean = False, Optional Draw_Cursor As Boolean = False)
    117. Dim wScreen As Long
    118. Dim hScreen As Long
    119. Dim hdcScreen As Long
    120. Dim w As Long
    121. Dim h As Long
    122.  
    123. Dim TmpPic As PictureBox
    124. Set TmpPic = Picture_
    125. TmpPic.AutoRedraw = False
    126. Dim lWnd As Long
    127. Dim R
    128. lWnd = GetForegroundWindow()
    129.  
    130. Picture_.Cls
    131. wScreen = Screen.Width \ Screen.TwipsPerPixelX
    132. hScreen = Screen.Height \ Screen.TwipsPerPixelY
    133. Picture_.ScaleMode = vbPixels
    134. w = Screen.Width / Screen.TwipsPerPixelX
    135. h = Screen.Height / Screen.TwipsPerPixelY
    136. hdcScreen = GetDC(0)
    137.  
    138. If Draw_Cursor = True Then DrawCursor hdcScreen
    139.  
    140. If Active_Window = False Then
    141. Picture_.Width = (Screen.Width * Screen.TwipsPerPixelX)
    142. Picture_.Height = (Screen.Height * Screen.TwipsPerPixelY)
    143. 'Picture_.AutoRedraw = False
    144. R = StretchBlt(TmpPic.hdc, 0, 0, w, h, hdcScreen, 0, 0, wScreen, hScreen, vbSrcCopy)
    145. 'Picture_.AutoRedraw = True
    146. Else
    147. If Active_Window = True Then
    148. Dim rct As RECT
    149. GetWindowRect lWnd, rct
    150. 'Picture_.AutoRedraw = False
    151. R = StretchBlt(TmpPic.hdc, rct.Left * -1, rct.Top * -1, rct.Right, rct.Bottom, hdcScreen, 0, 0, rct.Right, rct.Bottom, vbSrcCopy)
    152. Picture_.Width = (rct.Right * Screen.TwipsPerPixelX) - (rct.Left * Screen.TwipsPerPixelX) + 70
    153. Picture_.Height = (rct.Bottom * Screen.TwipsPerPixelY) - (rct.Top * Screen.TwipsPerPixelY) + 70
    154. 'Picture_.AutoRedraw = True
    155. End If
    156. End If
    157.  
    158. Set Picture_.Picture = hDCToPicture(TmpPic.hdc, 0, 0, Screen.Width / Screen.TwipsPerPixelX, Screen.Height / Screen.TwipsPerPixelY)
    159.  
    160. End Sub

    This code works, thanks for your help!

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