Jump to content

Help with BitBlt


 Share

Go to solution Solved by UEZ,

Recommended Posts

I'm coding a GUI that using a lot of GDI (not GDI+) output.  I've been manually redrawing things as needed via WM_PAINT.  It's awkward and slow, and it can flicker a lot.  Then I found out about _WinAPI_BitBlt.  I've looked all over the forums here, and all over the internet in general.  I think I understand the basics of it, but I can't get it to work.  I think what I want to do is manipulate $hDCSource and then BitBlt it onto $hDCDest.  Here's my reducer sample:

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Global Const $Width = 100, $Height = 100
Global $Left = 50, $Top = 50
Global $hwnd, $hDCDest, $hDCSource, $hPenLil, $hPenFat, $hBrush
Main()

Func Main()

    ; GUI & GDI handles
    $hwnd = GUICreate("BitBlt Test", 300, 300)
    $hDCDest = _WinAPI_GetDC($hwnd)
    $hDCSource = _WinAPI_CreateCompatibleDC($hDCDest)
    $hPenLil = _WinAPI_CreatePen($PS_SOLID, 1, 0xFFFFFF)
    $hPenFat = _WinAPI_CreatePen($PS_SOLID, 3, 0x00FF00)
    $hBrush = _WinAPI_CreateSolidBrush(0xFFFFFF)

    ; Fill white rectangle
    Local $tRect = DllStructCreate($tagRect)
        DllStructSetData($tRect, "Left", $Left)
        DllStructSetData($tRect, "Top", $Top)
        DllStructSetData($tRect, "Right", $Left + $Width)
        DllStructSetData($tRect, "Bottom", $Top + $Height)
    _WinAPI_FillRect($hDCSource, $tRect, $hBrush)

    ; Thick green outline
    _WinAPI_SelectObject($hDCSource, $hPenFat)
    _WinAPI_MoveTo($hDCSource, $Left, $Top)
    _WinAPI_LineTo($hDCSource, $Left + $Width, $Top)
    _WinAPI_LineTo($hDCSource, $Left + $Width, $Top + $Height)
    _WinAPI_LineTo($hDCSource, $Left, $Top + $Height)
    _WinAPI_LineTo($hDCSource, $Left, $Top)

    ; Thin black outline
    _WinAPI_SelectObject($hDCSource, $hPenLil)
    _WinAPI_MoveTo($hDCSource, $Left, $Top)
    _WinAPI_LineTo($hDCSource, $Left + $Width, $Top)
    _WinAPI_LineTo($hDCSource, $Left + $Width, $Top + $Height)
    _WinAPI_LineTo($hDCSource, $Left, $Top + $Height)
    _WinAPI_LineTo($hDCSource, $Left, $Top)

    GUIRegisterMsg($WM_PAINT, "WM_PAINT")
    GUISetState(@SW_SHOW, $hwnd)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Quit()
        EndSwitch
    WEnd
EndFunc

Func WM_PAINT()
;   _WinAPI_BitBlt($hDCDest, $Left, $Top, $Width, $Height, $hDCSource, $Left, $Top, $BLACKNESS) ; Works
    _WinAPI_BitBlt($hDCDest, $Left, $Top, $Width, $Height, $hDCSource, $Left, $Top, $SRCCOPY)   ; Doesn't work
EndFunc

Func Quit()
    _WinAPI_ReleaseDC($hwnd, $hDCDest)
    _WinAPI_DeleteDC($hDCSource)
    _WinAPI_DeleteObject($hBrush)
    _WinAPI_DeleteObject($hPenLil)
    _WinAPI_DeleteObject($hPenFat)
    GUIDelete($hwnd)
    Exit
EndFunc

What am I doing wrong?

Link to comment
Share on other sites

  • Solution

You forgot to create the bitmap where you draw your graphics.

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Global Const $Width = 100, $Height = 100
Global $Left = 50, $Top = 50
Global $hwnd, $hDCDest, $hDCSource, $hPenLil, $hPenFat, $hBrush, $hHBitmap
Main()

Func Main()

    ; GUI & GDI handles
    $hwnd = GUICreate("BitBlt Test", 300, 300)
    GUISetState(@SW_SHOW, $hwnd)

    $hDCDest = _WinAPI_GetDC($hwnd)
    $hDCSource = _WinAPI_CreateCompatibleDC($hDCDest)
    $hHBitmap = _WinAPI_CreateSolidBitmap($hwnd, 0, 300, 300)
    _WinAPI_SelectObject($hDCSource, $hHBitmap)

    $hPenLil = _WinAPI_CreatePen($PS_SOLID, 1, 0xFFFFFF)
    $hPenFat = _WinAPI_CreatePen($PS_SOLID, 3, 0x00FF00)
    $hBrush = _WinAPI_CreateSolidBrush(0xFFFFFF)

    ; Fill white rectangle
    Local $tRect = DllStructCreate($tagRect)
        DllStructSetData($tRect, "Left", $Left)
        DllStructSetData($tRect, "Top", $Top)
        DllStructSetData($tRect, "Right", $Left + $Width)
        DllStructSetData($tRect, "Bottom", $Top + $Height)

    _WinAPI_SelectObject($hDCSource, $hBrush)
    _WinAPI_FillRect($hDCSource, $tRect, $hBrush)

    ; Thick green outline
    _WinAPI_SelectObject($hDCSource, $hPenFat)
    _WinAPI_MoveTo($hDCSource, $Left, $Top)
    _WinAPI_LineTo($hDCSource, $Left + $Width, $Top)
    _WinAPI_LineTo($hDCSource, $Left + $Width, $Top + $Height)
    _WinAPI_LineTo($hDCSource, $Left, $Top + $Height)
    _WinAPI_LineTo($hDCSource, $Left, $Top)

    ; Thin black outline
    _WinAPI_SelectObject($hDCSource, $hPenLil)
    _WinAPI_MoveTo($hDCSource, $Left, $Top)
    _WinAPI_LineTo($hDCSource, $Left + $Width, $Top)
    _WinAPI_LineTo($hDCSource, $Left + $Width, $Top + $Height)
    _WinAPI_LineTo($hDCSource, $Left, $Top + $Height)
    _WinAPI_LineTo($hDCSource, $Left, $Top)

    GUIRegisterMsg($WM_PAINT, "WM_PAINT")
    _WinAPI_RedrawWindow($hwnd, 0, 0, $RDW_INVALIDATE + $RDW_UPDATENOW)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Quit()
            Case $GUI_EVENT_RESTORE
                _WinAPI_RedrawWindow($hwnd, 0, 0, $RDW_INVALIDATE + $RDW_UPDATENOW)
        EndSwitch
    WEnd
EndFunc

Func WM_PAINT()
;   _WinAPI_BitBlt($hDCDest, $Left, $Top, $Width, $Height, $hDCSource, $Left, $Top, $BLACKNESS) ; Works
    _WinAPI_BitBlt($hDCDest, $Left, $Top, $Width, $Height, $hDCSource, $Left, $Top, $SRCCOPY)   ; Doesn't work
EndFunc

Func Quit()
    _WinAPI_ReleaseDC($hwnd, $hDCDest)
    _WinAPI_DeleteDC($hDCSource)
    _WinAPI_DeleteObject($hHBitmap)
    _WinAPI_DeleteObject($hBrush)
    _WinAPI_DeleteObject($hPenLil)
    _WinAPI_DeleteObject($hPenFat)
    GUIDelete($hwnd)
    Exit
EndFunc

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...