Results 1 to 5 of 5

Thread: [VB6 and API] - Convert a hdc to long variable

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    [VB6 and API] - Convert a hdc to long variable

    i build 1 function for convert hdc to long variable:
    Code:
    Public Function ConvertPicturetoLong(PicControl As PictureBox) As Long
        Dim mdc As Long
        Dim mBMP As Long
        mdc = CreateCompatibleDC(PicControl.hDC)
        mBMP = CreateCompatibleBitmap(PicControl.hDC, PicControl.ScaleWidth, PicControl.ScaleHeight)
        SelectObject mdc, mBMP
        ConvertPicturetoLong = mBMP
        DeleteObject mBMP
        DeleteDC mdc
    End Function
    but i belive that isn't work, because when i do:
    Code:
    dim s as long
     s = ConvertPicturetoLong(PicAnimation(PicAnimation.Count - 1))
            PicAnimation(PicAnimation.Count - 1).Picture = Nothing
            Debug.Print BitBlt(PicAnimation(PicAnimation.Count - 1), 0, 0, UserControl.Width, UserControl.Height, s, 0, 0, vbSrcCopy)
    because the bitblt() give me 0(zero). what isn't right?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [VB6 and API] - Convert a hdc to long variable

    In your function, you create a bitmap and select it into a dc, then delete the bitmap and the dc before the function exits. The bitmap is destroyed.
    Besides, the variable you are returning did reference a Bitmap not an hDC. And you are using the return value as the source hDC parameter in the BitBlt call. BitBlt requires hDC handles not bitmap handles.

    You are using your function to return an hDC for a BitBlt call, I think this is more correct
    Code:
    Public Function ConvertPicturetoLong(PicControl As PictureBox) As Long
          ConvertPicturetoLong = PicControl.hDC
    End Function
    But you don't need a function, you can reference the picturebox directly.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Re: [VB6 and API] - Convert a hdc to long variable

    Quote Originally Posted by LaVolpe View Post
    In your function, you create a bitmap and select it into a dc, then delete the bitmap and the dc before the function exits. The bitmap is destroyed.
    Besides, the variable you are returning did reference a Bitmap not an hDC. And you are using the return value as the source hDC parameter in the BitBlt call. BitBlt requires hDC handles not bitmap handles.

    You are using your function to return an hDC for a BitBlt call, I think this is more correct
    Code:
    Public Function ConvertPicturetoLong(PicControl As PictureBox) As Long
          ConvertPicturetoLong = PicControl.hDC
    End Function
    But you don't need a function, you can reference the picturebox directly.
    i lose the image before use the bitblt()
    Code:
    ......
    Dim s As Long
            picGraphicsEffects.Picture = UserControl.Image
            UserControl.Picture = Nothing
            Load PicAnimation(PicAnimation.Count)
            PrintWindow Extender.Container.hwnd, PicAnimation(PicAnimation.Count - 1).hDC, 0
            PicAnimation(PicAnimation.Count - 1).Refresh
            PicAnimation(PicAnimation.Count - 1).Picture = PicAnimation(PicAnimation.Count - 1).Image
            s = PicAnimation(PicAnimation.Count - 1).hDC
            PicAnimation(PicAnimation.Count - 1).Picture = Nothing
            BitBlt PicAnimation(PicAnimation.Count - 1).hDC, 0, 0, UserControl.Width, UserControl.Height, s, Extender.Left, Extender.Top, vbSrcCopy ' has you see i use the variable long, but without sucess:(
            PicAnimation(PicAnimation.Count - 1).Refresh
            PicAnimation(PicAnimation.Count - 1).Picture = PicAnimation(PicAnimation.Count - 1).Image
            TransparentAlphaBlendDIB picGraphicsEffects, PicAnimation(PicAnimation.Count - 1), CLng(m_Opacy), picGraphicsEffects.Point(0, 0)
            UserControl.Picture = PicAnimation(PicAnimation.Count - 1).Image
            Unload PicAnimation(PicAnimation.Count - 1)
    ................
    why?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [VB6 and API] - Convert a hdc to long variable

    If the picturebox does not have AutoRedraw=True and is hidden/not visible, you should get just black I would think.
    If the picturebox does have AutoRedraw=True and the size is smaller than what you are trying to Blt, you will get only the size of the picturebox, the rest should be black I would think

    Now, if PicAnimation(picAnimation.Count-1).AutoRedraw=False, then this is a problem...
    Code:
            PrintWindow Extender.Container.hwnd, PicAnimation(PicAnimation.Count - 1).hDC, 0
            PicAnimation(PicAnimation.Count - 1).Refresh ' << erased without AutoRedraw=True
    If the above isn't the problem, I'm sure this is part of it too
    Code:
            PicAnimation(PicAnimation.Count - 1).Picture = PicAnimation(PicAnimation.Count - 1).Image
            s = PicAnimation(PicAnimation.Count - 1).hDC
            PicAnimation(PicAnimation.Count - 1).Picture = Nothing ' << erased DC
            ' ^^ if the AutoRedraw = True, the hDC changed, therefore s points to a destroyed hDC
    It is never safe to cache a VB hDC since VB can change it whenever it wants. Try this example
    -- add a picturebox to a form & paste this code. Run project & click the picturebox.
    See the results. You may be surprised
    Code:
    Private Sub Picture1_Click()
        Picture1.AutoRedraw = True
        Picture1.PaintPicture Me.Icon, 0, 0
        Debug.Print "hDC before setting picture to image: "; Picture1.hDC
        Set Picture1.Picture = Picture1.Image
        Debug.Print "hDC after setting picture to image: "; Picture1.hDC
        Set Picture1.Picture = Nothing
        Debug.Print "hDC after setting picture1.picture=nothing: "; Picture1.hDC
    End Sub
    Last edited by LaVolpe; Sep 9th, 2010 at 05:13 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Re: [VB6 and API] - Convert a hdc to long variable

    Quote Originally Posted by LaVolpe View Post
    If the picturebox does not have AutoRedraw=True and is hidden/not visible, you should get just black I would think.
    If the picturebox does have AutoRedraw=True and the size is smaller than what you are trying to Blt, you will get only the size of the picturebox, the rest should be black I would think

    Now, if PicAnimation(picAnimation.Count-1).AutoRedraw=False, then this is a problem...
    Code:
            PrintWindow Extender.Container.hwnd, PicAnimation(PicAnimation.Count - 1).hDC, 0
            PicAnimation(PicAnimation.Count - 1).Refresh ' << erased without AutoRedraw=True
    If the above isn't the problem, I'm sure this is part of it too
    Code:
            PicAnimation(PicAnimation.Count - 1).Picture = PicAnimation(PicAnimation.Count - 1).Image
            s = PicAnimation(PicAnimation.Count - 1).hDC
            PicAnimation(PicAnimation.Count - 1).Picture = Nothing ' << erased DC
            ' ^^ if the AutoRedraw = True, the hDC changed, therefore s points to a destroyed hDC
    It is never safe to cache a VB hDC since VB can change it whenever it wants. Try this example
    -- add a picturebox to a form & paste this code. Run project & click the picturebox.
    See the results. You may be surprised
    Code:
    Private Sub Picture1_Click()
        Picture1.AutoRedraw = True
        Picture1.PaintPicture Me.Icon, 0, 0
        Debug.Print "hDC before setting picture to image: "; Picture1.hDC
        Set Picture1.Picture = Picture1.Image
        Debug.Print "hDC after setting picture to image: "; Picture1.hDC
        Set Picture1.Picture = Nothing
        Debug.Print "hDC after setting picture1.picture=nothing: "; Picture1.hDC
    End Sub
    yes... the 3 values are diffent...
    Can i put here my code by steps(because the real problem isn't with the post title)?
    and yes the autoredraw is true and the scalemode is in pixels. and they are hidden.
    Last edited by joaquim; Sep 9th, 2010 at 05:31 PM.
    VB6 2D Sprite control

    To live is difficult, but we do it.

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