Sign in to follow this  
Followers 0
Artisan

Help with BitBlt

3 posts in this topic

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?

Share this post


Link to post
Share on other sites



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

1 person likes this

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Thanks again, UEZ!  The helpfile is rather sparse on this topic.  Solved!

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Similar Content

    • IAMK
      By IAMK
      Hello,
       
      I have a script which uses a GUI with two buttons. It gets shown and hidden., and pressing the buttons (including their hotkeys) highlights the button, making you able to press enter the next time.
      However, I don't want that.
       
      I've tried using WinActivate() to make enter do things on another program, but the enter is still tied to the AutoIT GUI (which still has the button highlighted).
       
      Here's the important part of my code:
      $MY_GUI = GUICreate("Check.exe", 400, 300, ((@DesktopWidth / 2) - 200), -500) WinSetOnTop($MY_GUI, "", 1) ;Keeps the window on the top. $Button1 = GUICtrlCreateButton("Pass (Ctrl+P)", 40, 250, 120, 40, $WS_GROUP) GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif") $Button2 = GUICtrlCreateButton("Fail (Ctrl+F)", 240, 250, 120, 40, $WS_GROUP) GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif") $GUIText = GUICtrlCreateLabel("", 20, 10, 360, 230) GUICtrlSetFont(-1, 13, 400, 0, "MS Sans Serif") GUICtrlSetData($GUIText, Execute($stepMessage[$step])) $showGUI = "True" GUISetState(@SW_SHOW) While($showGUI = "True") $buttonPressed = GUIGetMsg() Switch $buttonPressed Case $Button1 ;Pass $pass = "True" $buttonPressed = 0 $showGUI = "False" GUISetState(@SW_HIDE) Case $Button2 ;Fail $buttonPressed = 0 $showGUI = "False" GUISetState(@SW_HIDE) While($message = "") $message = InputBox("Check.exe", "Please enter the reason the step failed." & @CRLF, "", "", 300, 200, ((@DesktopWidth / 2) - 150), -400) WEnd Return $message EndSwitch WEnd Local $pass = "False" ;Whether "Pass" or "Fail" was pressed. Local $failLog = xxxfunc($pass) ;Returns a message to log if the step failed. ##THIS CALLS THE CODE SNIPPET ABOVE## FileWrite($file, _Now() & ": Step " & $step & getStatus($pass) & "and took " & Ceiling(TimerDiff($timer) / 1000) & " seconds." & @CRLF) If($failLog <> "") Then FileWrite($file, "Logged comment: " & $failLog & @CRLF) EndIf WinActivate("[TITLE:ProgramName]") ;Hotkeys: HotKeySet("^{p}", "pressPass") HotKeySet("^{f}", "pressFail") Func pressPass() $buttonPressed = $Button1 EndFunc Func pressFail() $buttonPressed = $Button2 EndFunc  
      The GUI's button/hotkey pressing works correctly.
      The same WinActivate code works in another script of mine.
      Together, WinActivate is not take "Active" away from the GUI's last-pressed button. How do I fix this?
       
      Thank you in advance.
    • fatpig
      By fatpig
      Dear AutoIT Community.
      I am currently working on a program that will display lots of images in a scroll down GUI.
      I create the GUI based on the number of images, which works fine.
      But all images I place using GuiCtrlCreatePic above a threshold of around 32000 pixels (cant say exactly) are placed at negative locations and finally will reach 0 again
      and will then populate on top of the images placed there before.
      For $i = 1 To $Images[0] ; Get picture dimensions $hImage = _GDIPlus_ImageLoadFromFile($ImgRep & $Images[$i]) $iX = _GDIPlus_ImageGetWidth($hImage) $iY = _GDIPlus_ImageGetHeight($hImage) _GDIPlus_ImageDispose($hImage) ; Resize & place images in GUI $Pic[$i] = GUICtrlCreatePic($ImgRep & $Images[$i], $Spacer, Round($yPos, 0), $MaxImgWidth, $MaxImgWidth * ($iY / $iX)) $yPos = $yPos + $MaxImgWidth * ($iY / $iX) + $Spacer $log = $log & "|" & $yPos $currentlut = $Images[1] Next I have found a limit at 32767 pixels for some GUI elements when googling around.
      Can someone tell me how to circumvent that?
      thanks!
      cheers
      Felix
    • Shirdish_chakravarthi
      By Shirdish_chakravarthi
      Hello Guys this is my gui and i need the numeric value typed in the highlighted  inputbox. if this value is greater than 1 i need to perform some operations
      \
       
      Here is the summary about that field which i got from wininfo tool

      i tried writing the code as shown below.. but the msgbox is popping up empty value..when the same code is pasted under some button action i am able to access the value present in the field..but when placed alone as written in the attached script i am unable to fetch it... 

      here i am using the valu present in the field

      can you guys tell me what's wrong??? or how can i get that value?
       
      My_GUI.au3

    • Larnil
      By Larnil
      This script generates Barnsleys Fractal Fern using script only.
      ; version 2017-10-03 ; Barnsley Fractal Fern ; by larnil #include <GUIConstants.au3> Dim $x, $y, $xn, $yn, $n, $r, $dc $WinSize = 800 ; window size ;Create graphics windows AutoItSetOption("GUIOnEventMode", 1) $GUI = GUICreate("Barnsley Fractal Fern", $WinSize, $WinSize, -1, -1) $Graphic = GuiCtrlCreateGraphic(0, 0, $WinSize, $WinSize) GUICtrlSetBkColor(-1, 0x000000) GUICtrlSetGraphic(-1,$GUI_GR_COLOR, 0x00ff00) GUISetState(@SW_SHOW) GUISetOnEvent($GUI_EVENT_CLOSE,"Bye") ;Main $start = TimerInit() _Fern(800) ; hight of fern - can be larger than window MsgBox(0,"Time taken:",Round(TimerDiff($start)/1000,3) &" seconds") While 1 Sleep(100) WEnd Func Bye() Exit EndFunc Func _Fern($height) $f = $height/10.6 ; scale factor. Complete fern is within 0 <= y <= 9.9983 (with no scale) $offset_x = $height/2 - $height/40 ; Side adjustment. Fern is within −2.1820 < x < 2.6558 (with no scale) For $n = 1 To $height*200 ; Number of iterations $r = Random(0, 99, 1) Select Case $r < 85 ; 0-84 = 85% of the time $xn = 0.85 * $x + 0.04 * $y $yn = -0.04 * $x + 0.85 * $y + 1.6 Case $r > 84 AND $r < 92 ; 85-91 = 7% of the time $xn = 0.2 * $x - 0.26 * $y $yn = 0.23 * $x + 0.22 * $y + 1.6 Case $r > 91 AND $r < 99 ; 92-98 = 7% of the time $xn = -0.15 * $x + 0.28 * $y $yn = 0.26 * $x + 0.24 * $y + 0.44 Case Else ; 99-99 = 1% of the time $xn = 0 $yn = 0.16 * $y EndSelect $x = $xn $y = $yn GUICtrlSetGraphic($Graphic, $GUI_GR_PIXEL, $offset_x + $x * $f, $height - $y * $f) Next GUICtrlSetGraphic($Graphic, $GUI_GR_REFRESH) EndFunc ;==> Fern  
      Here is another example where I have used GDI (my very first attempt at using GDI by the way). This script can generate much larger Ferns and save them to file (png). I have used this script to generate a 20000 x 20000 pixel @ 600 dpi image. Looks really good printed out in full size.
      #include <GDIPlus.au3> #include <GUIConstantsEx.au3> ; Param Local Const $iPxColor = 0xFF00FF00 ; Pixel color for fractal Alpha/R/G/B Local Const $iBgColor = 0xFFFFFFFF ; Background color for image Alpha/R/G/B Local Const $iSize = 1000 ; Hight of fern in pixels - image will have this hight and width too Local Const $iIter = $iSize*400 ; Number of iterations - $iSize * 200 is a good starting point ; Call function ;$start = TimerInit() _Fern($iSize,$iIter) ;MsgBox(0,"Time taken:",Round(TimerDiff($start)/1000,3) &" seconds") ; Function for generating Barnsley Fractal Fern Func _Fern($Size,$Iter) _GDIPlus_Startup() ; initialize GDI+ Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($Size, $Size) ; create an empty bitmap Local $hBmpCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap) ; get the graphics context of the image _GDIPlus_GraphicsClear($hBmpCtxt, $iBgColor) ; Set the background color for empty bitmap ; Here the magic happens $x=0 ; init $y=0 ; init $f = $Size/10.6 ; scale factor. Complete fern is within 0 <= y <= 9.9983 (with no scale) $offset_x = $Size/2 - $Size/40 ; Side adjustment. Fern is within −2.1820 < x < 2.6558 (with no scale) For $n = 1 To $Iter ; Number of iterations $r = Random(0, 99, 1) Select Case $r < 85 ; 0-84 = 85% of the time $xn = 0.85 * $x + 0.04 * $y $yn = -0.04 * $x + 0.85 * $y + 1.6 Case $r > 84 AND $r < 92 ; 85-91 = 7% of the time $xn = 0.2 * $x - 0.26 * $y $yn = 0.23 * $x + 0.22 * $y + 1.6 Case $r > 91 AND $r < 99 ; 92-98 = 7% of the time $xn = -0.15 * $x + 0.28 * $y $yn = 0.26 * $x + 0.24 * $y + 0.44 Case Else ; 99-99 = 1% of the time $xn = 0 $yn = 0.16 * $y EndSelect $x = $xn $y = $yn _GDIPlus_BitmapSetPixel($hBitmap, $offset_x + $x * $f, $Size - $y * $f, $iPxColor) ; Change pixel color for calculated X,Y Next ; ==> End of magic $File = "\Fractal_Fern_"&StringRight(Hex($iPxColor),6)&"-"&StringRight(Hex($iBgColor),6)&"-"&$iSize&".png" _GDIPlus_ImageSaveToFile($hBitmap, @MyDocumentsDir & $File) ;save bitmap to disk ShellExecute(@MyDocumentsDir & $File); Show it to the world in your default image viewer ; Cleanup GDI+ resources _GDIPlus_GraphicsDispose($hBmpCtxt) _GDIPlus_BitmapDispose($hBitmap) _GDIPlus_Shutdown() EndFunc ;==> _Fern  
       

    • Neonovaz
      By Neonovaz
      Hello
       
      Is there anyway to store word documents in Autoit GUI? For example I have a instruction sheet that I want to bundle up with the exe.

      So a user simply clicks the icon and the stored document will launch  (Something like how you can add objects like excel sheets in word documents )

      (I Know we can launch word files from script directory)