Results 1 to 2 of 2

Thread: [RESOLVED] GDI: how draw a cursor\ani to a Memory DC?

Hybrid View

  1. #1

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

    Resolved [RESOLVED] GDI: how draw a cursor\ani to a Memory DC?

    see how i create a Memory DC:
    Code:
    Public Sub NewImage(width As Long, height As Long, Optional backcolor As Long = White)
        If (ImageBitmap) Then
            SelectObject ImageHDC, OldImageBitmap
            DeleteObject ImageBitmap
            DeleteDC ImageHDC
        End If
        If (ImageGraphics) Then GdipDeleteGraphics ImageGraphics
        ImageHDC = CreateCompatibleDC(GetDC(0))
        If (ImageHDC = 0) Then Debug.Print "error"
        ImageBitmap = CreateCompatibleBitmap(GetDC(0), width, height)
        If (ImageBitmap = 0) Then Debug.Print "error on ImageBitmap"
        OldImageBitmap = SelectObject(ImageHDC, ImageBitmap)
        Dim hBrush As Long
        hBrush = CreateSolidBrush(backcolor)
        If (hBrush = 0) Then Debug.Print "error on brush"
        Dim rect As rect
        rect.Left = 0
        rect.Top = 0
        rect.Bottom = height
        rect.Right = width
        If (FillRect(ImageHDC, rect, hBrush) = 0) Then Debug.Print "error on FillRect"
        ImageBackColor = backcolor
        lngwidth = width
        lngheight = height
        DeleteObject hBrush
    End Sub
    now see how i read the file and draw the cursor to it:
    Code:
    Public Sub FromFile(FileName As String)
        If (hBitmap) Then GdipDisposeImage (hBitmap)
        If (CurAni) Then DeleteObject (CurAni)
        
        imgType = GetImageType(FileName)
        If (imgType = ANI Or imgType = CUR) Then
            Debug.Print "cursor" 'yes it's printed
            CurAni = LoadImage(App.hInstance, FileName, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE)
                  
            NewImage 100, 100
            'If (DrawIconEx(GetWindowDC(GetForegroundWindow()), 0, 0, CurAni, 0, 0, 0, 1, DI_NORMAL Or DI_IMAGE) = 0) Then Debug.Print "error on draw cursor: " & GetLastError
            If (DrawIconEx(ImageHDC, 0, 0, CurAni, 0, 0, 0, 1, DI_NORMAL Or DI_IMAGE) = 0) Then Debug.Print "error on draw cursor: " & GetLastError
            
        Else
            
            Call GdipLoadImageFromFile(StrPtr(FileName), hBitmap)
            NewImage width, height
            GdipCreateFromHDC ImageHDC, hGraphics
            GdipDrawImage hGraphics, hBitmap, 0, 0
            GdipDeleteGraphics hGraphics
        End If
    End Sub
    heres how i draw on form:
    Code:
    Public Sub Draw(DestinationHDC As Long, Optional Transparent As Boolean = True)
        RaiseEvent BeforeDrawImage(intSelectedFrame)
        If (ImageHDC = 0) Then Debug.Print "no hDC"
        If (Transparent = True) Then
            Dim BF As Long
            Const USE_BITMAP_ALPHA = &H1000000 'AC_SRC_ALPHA scaled up to the 4th byte of a long
            BF = 128 * &H10000  'semi transparent ignoring bitmaps alpha channel
            BF = 255 * &H10000 Or USE_BITMAP_ALPHA 'fully opaque using bitmaps alpha channel
            Dim btMap As BITMAP
            If (GetObject(ImageBitmap, Len(btMap), btMap) = 0) Then Debug.Print "error"
            Dim s As BITMAPINFO
            Dim BytesPerScanLine As Long
            s.bmiHeader.biSize = 40
            s.bmiHeader.biPlanes = 1
            s.bmiHeader.biBitCount = 24
            s.bmiHeader.biHeight = height
            s.bmiHeader.biWidth = width
            s.bmiHeader.biPlanes = btMap.bmPlanes
            s.bmiHeader.biCompression = 0
            BytesPerScanLine = ((s.bmiHeader.biWidth * 3) + 3) And &HFFFFFFFC
            s.bmiHeader.biSizeImage = BytesPerScanLine * s.bmiHeader.biHeight
            Dim ImageData() As Byte
            ReDim ImageData(3, s.bmiHeader.biWidth, s.bmiHeader.biHeight)
            GetDIBits ImageHDC, ImageBitmap, 0, s.bmiHeader.biHeight, ImageData(0, 0, 0), s, 0
            
            If (ImageData(0, 0, 0) = 0) Then 'testing if have a alpha value
                AlphaBlend DestinationHDC, 0, 0, width, height, ImageHDC, 0, 0, width, height, BF 
            Else
                TransparentBlt DestinationHDC, 0, 0, width, height, ImageHDC, 0, 0, width, height, GetPixel(ImageHDC, 0, 0)
            End If
        Else
            BitBlt DestinationHDC, 0, 0, width, height, ImageHDC, 0, 0, vbSrcCopy
        End If
        
        'DrawIconEx DestinationHDC, 0, 0, CurAni, 0, 0, 0, 1, DI_NORMAL Or DI_IMAGE 'works
    End Sub
    the DrawIconEx() don't give me any error. so why the cursor isn't drawed on Memory DC?
    (on form is drawed, i had tested)
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2

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

    Re: [RESOLVED] GDI: how draw a cursor\ani to a Memory DC?

    the error was on NewImage():
    Code:
    lngwidth = width
        lngheight = height
    because the property width and height was wrong. after i fixed them, works like a sharm.
    and i fixed on FromFile() too:
    Code:
    GdipGetImageHeight HBITMAP, lngheight
            GdipGetImageWidth HBITMAP, lngwidth
            NewImage lngwidth, lngheight
    thanks for all
    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