Jump to content

Recommended Posts

Posted (edited)

Hello everybody :)

@UEZ explained in track ticket 4098 why plenty of graphics context disappear in the examples of the help file, he also gave a solution to prevent this. Here are a couple of tests done on some help file examples :

1) Topic _GDIPlus_LineBrushCreate (original code)
The graphics context disappear if you minimize the GUI OR drag the GUI outside the screen bounds OR cover the GUI with another window :

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>

Example()

Func Example()
    _GDIPlus_Startup() ;initialize GDI+
    Local Const $iWidth = 600, $iHeight = 600, $iBgColor = 0x303030 ;$iBgColor format RRGGBB

    Local $hGUI = GUICreate("GDI+ Example (" & @ScriptName & ")", $iWidth, $iHeight) ;create a test GUI
    GUISetBkColor($iBgColor, $hGUI) ;set GUI background color
    GUISetState(@SW_SHOW)

    Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI) ;create a graphics object from a window handle

    _GDIPlus_GraphicsSetSmoothingMode($hGraphics, $GDIP_SMOOTHINGMODE_HIGHQUALITY) ;sets the graphics object rendering quality (antialiasing)
    Local $hBrush = _GDIPlus_LineBrushCreate(0, 0, 280, 500, 0xFFFF0000, 0xFF4020FF, 1) ;create linear gradient brush
    _GDIPlus_GraphicsFillEllipse($hGraphics, 100, 50, 380, 500, $hBrush) ;draw the egg

    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ;cleanup GDI+ resources
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_Shutdown()
    GUIDelete($hGUI)
EndFunc   ;==>Example

2) Same topic _GDIPlus_LineBrushCreate (reworked code)
7 lines added in 2 groups (3+4 lines)
* 1st group of 3 lines just before original Local $hGraphics = ... (this line to be commented out or deleted)
* 2nd group of 4 lines just before the main loop

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>

Example()

Func Example()
    _GDIPlus_Startup() ;initialize GDI+
    Local Const $iWidth = 600, $iHeight = 600, $iBgColor = 0x303030 ;$iBgColor format RRGGBB

    Local $hGUI = GUICreate("GDI+ Example (" & @ScriptName & ")", $iWidth, $iHeight) ;create a test GUI
    GUISetBkColor($iBgColor, $hGUI) ;set GUI background color
    GUISetState(@SW_SHOW)

    ;============ 1st part to keep graphics context showing (3 lines added) ============
    Local $idPic = GUICtrlCreatePic("", 0, 0, $iWidth, $iHeight)
    Local $hImageScan0 = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImageScan0)
    ;===================================================================================

    ;;;;; Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI) ;create a graphics object from a window handle ; <=== LINE REMOVED

    _GDIPlus_GraphicsSetSmoothingMode($hGraphics, $GDIP_SMOOTHINGMODE_HIGHQUALITY) ;sets the graphics object rendering quality (antialiasing)
    Local $hBrush = _GDIPlus_LineBrushCreate(0, 0, 280, 500, 0xFFFF0000, 0xFF4020FF, 1) ;create linear gradient brush
    _GDIPlus_GraphicsFillEllipse($hGraphics, 100, 50, 380, 500, $hBrush) ;draw the egg

    ;============ 2nd part to keep graphics context showing (4 lines added) ============
    Local $hBitmap_GDI = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImageScan0)
    _WinAPI_DeleteObject(GUICtrlSendMsg($idPic, 0x0172, 0, $hBitmap_GDI)) ; $STM_SETIMAGE = 0x0172, $IMAGE_BITMAP = 0
    _WinAPI_DeleteObject($hBitmap_GDI)
    _GDIPlus_ImageDispose($hImageScan0)
    ;===================================================================================

    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ;cleanup GDI+ resources
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_Shutdown()
    GUIDelete($hGUI)
EndFunc   ;==>Example

Now the graphics context never disappear !
I immediately tried this on another example of the help file :

3) Topic _GDIPlus_LineBrushSetSigmaBlend (reworked code)
Exactly the same 7 lines added in 2 groups (3+4 lines) and the original Local $hGraphics = ... commented out (or deleted)

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>

Example()

Func Example()
    _GDIPlus_Startup() ;initialize GDI+
    Local Const $iWidth = 600, $iHeight = 300, $iBgColor = 0x303030 ;$iBgColor format RRGGBB

    Local $hGUI = GUICreate("GDI+ Example (" & @ScriptName & ")", $iWidth, $iHeight) ;create a test GUI
    GUISetBkColor($iBgColor, $hGUI) ;set GUI background color
    GUISetState(@SW_SHOW)

    ;============ 1st part to keep graphics context showing (3 lines added) ============
    Local $idPic = GUICtrlCreatePic("", 0, 0, $iWidth, $iHeight)
    Local $hImageScan0 = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImageScan0)
    ;===================================================================================

    ;;;;; Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI) ;create a graphics object from a window handle ; <=== LINE REMOVED

    _GDIPlus_GraphicsSetSmoothingMode($hGraphics, $GDIP_SMOOTHINGMODE_HIGHQUALITY) ;sets the graphics object rendering quality (antialiasing)
    Local $hBrush = _GDIPlus_LineBrushCreate(0, 0, 0, 280, 0xFFFFFF00, 0xFF0000FF, 1) ;create line brush
    _GDIPlus_GraphicsFillRect($hGraphics, 10, 10, 280, 280, $hBrush) ;draw the filled rectangle
    _GDIPlus_LineBrushSetSigmaBlend($hBrush, 0.333, .85)
    _GDIPlus_GraphicsFillRect($hGraphics, 310, 10, 280, 280, $hBrush) ;draw the filled rectangle
    _GDIPlus_GraphicsDrawString($hGraphics, "w/o sigma blend", 14, 14)
    _GDIPlus_GraphicsDrawString($hGraphics, "w/ sigma blend", 314, 14)

    ;============ 2nd part to keep graphics context showing (4 lines added) ============
    Local $hBitmap_GDI = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImageScan0)
    _WinAPI_DeleteObject(GUICtrlSendMsg($idPic, 0x0172, 0, $hBitmap_GDI)) ; $STM_SETIMAGE = 0x0172, $IMAGE_BITMAP = 0
    _WinAPI_DeleteObject($hBitmap_GDI)
    _GDIPlus_ImageDispose($hImageScan0)
    ;===================================================================================

    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ;cleanup GDI+ resources
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_Shutdown()
    GUIDelete($hGUI)
EndFunc   ;==>Example

This should help users who need their graphics context to be always shown.
All credits go to UEZ.

Edited by pixelsearch
typo

"I think you are searching a bug where there is no bug... don't listen to bad advice."

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
×
×
  • Create New...