Jump to content

[SOLVED] _GDIPlus_GraphicsDrawImageRect


Recommended Posts

hello,

i would like to know if it's possible to draw with

_GDIPlus_GraphicsDrawImageRect

for example half-transparent? so that the background i draw over is still half visible after drawing?

Edited by andygo
Link to comment
Share on other sites

hello,

i would like to know if it's possible to draw with

_GDIPlus_GraphicsDrawImageRect

for example half-transparent? so that the background i draw over is still half visible after drawing?

If you want to have something transparent over the bacground, whether it is something you draw or some control, I think the easiest way could be like this example.

Click the "fade foreground" and move the slider to see the effect. The example is unnecessarrily involved but it is a mod of something powerofos wrote that I already had.

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

Const $STM_SETIMAGE = 0x0172
Const $IMAGE_BITMAP = 0

$g = GUICreate("", 500, 500, -1, -1, BitOR($WS_SYSMENU, $WS_POPUP), $WS_EX_TOOLWINDOW)
;GUISetBkColor(0x111111)
$hExit = GUICtrlCreateIcon("shell32.dll", -132, 478, 6, 16, 16, BitOR($SS_NOTIFY, $WS_GROUP, $WS_TABSTOP))
GUICtrlSetCursor(-1, 0)
GUICtrlSetTip(-1, "Exit")
_RoundCorners($g, 0.9)
GUICtrlCreatePic(@ProgramFilesDir & "\AutoIt3\Examples\GUI\msoobe.jpg", 0,0,500,500)
GUICtrlCreatePic("", 0, 0, 500, 500, BitOR($GUI_SS_DEFAULT_PIC, $SS_CENTERIMAGE), $GUI_WS_EX_PARENTDRAG)
GUISetState()
$c = GUICreate("", 496, 300, 4, 198, $WS_POPUP, $WS_EX_MDICHILD, $g)
$Pic = GUICtrlCreatePic("", 50, 100, 400, 12)

GUICtrlSetState(-1, $GUI_DISABLE)

#region make slider
$hPic = GUICtrlGetHandle($Pic)
_GDIPlus_Startup()

$wGraph = _GDIPlus_GraphicsCreateFromHWND($c)
;_GDIPlus_GraphicsDispose($SheetWindowGraph)
$MainImage = _GDIPlus_BitmapCreateFromGraphics(400, 12, $wGraph)
;_GDIPlus_BitmapDispose($MainImage)
$MainGraphic = _GDIPlus_ImageGetGraphicsContext($MainImage)
_GDIPlus_GraphicsClear($MainGraphic, 0xFFF00FFF)
;_GDIPlus_GraphicsSetSmoothingMode($MainGraphic, 0); 2)
For $n = 0 To 400
    $hPen = _GDIPlus_PenCreate(0xff0000FF + Int($n * 255 / 400) * 2 ^ 8 + Int($n * 255 / 400) * 2 ^ 16, 1);+ $n*2^16/400  + $n*255/400;// Bitshift(Int($n*255/400),1) +
    _GDIPlus_GraphicsDrawLine($MainGraphic, $n, 0, $n, 12, $hPen)
    _GDIPlus_PenDispose($hPen)
Next

$hbmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($MainImage)
$aBmp = DllCall("user32.dll", "hwnd", "SendMessage", "hwnd", $hPic, "int", $STM_SETIMAGE, "int", $IMAGE_BITMAP, "int", $hbmp)
If $aBmp[0] <> 0 Then _WinAPI_DeleteObject($aBmp[0])
_WinAPI_DeleteObject($hbmp)
_GDIPlus_Shutdown()


$B = GUICtrlCreateButton("", 50, 95, 30, 22)
#endregion make slider

$chk1 = GUICtrlCreateCheckbox("fade background", 100, 140)
$chk2 = GUICtrlCreateCheckbox("fade foreground", 100, 170)

#region make shaped cutout
$aM_Mask = DllCall("gdi32.dll", "long", "CreateRectRgn", "long", 0, "long", 0, "long", 496, "long", 300)

$rct = DllStructCreate("int;int;int;inr", $aM_Mask[0])

$Dia = 200
For $j = 0 To $Dia / 3;the arch height
    $startx = 0
    $starty = $j
    $endx = Round(0.4 + $Dia / 2 - Sqrt($Dia * $Dia / 4 - ($Dia / 2 - $j) ^ 2), 0)

    $endy = $starty

    addRegion( $aM_Mask, $startx, $starty, $endx, $endy + 1)

    $startx = $Dia - $endx
    $endx = $Dia + 1
    addRegion( $aM_Mask, $startx, $starty, $endx, $endy + 1)
Next
DllCall("user32.dll", "long", "SetWindowRgn", "hwnd", $c, "long", $aM_Mask[0], "int", 1)
#endregion make shaped cutout

GUISetState()
GUISetBkColor(0xff0000, $c)

$lastpos = 0
$newpos = 0

While 1
    Sleep(10)
    $ci = GUIGetCursorInfo($c)
    ConsoleWrite($ci[4] & @CRLF)
    If $ci[2] And $ci[4] = $B Then
        $startx = $ci[0]
        $startb = ControlGetPos($c, "", $B)
        While $ci[2]
            $ci = GUIGetCursorInfo($c)
            $newpos = $startb[0] + $ci[0] - $startx
            If $newpos < 60 Then $newpos = 50
            If $newpos > 420 Then $newpos = 420
            If $newpos <> $lastpos Then
                ControlMove($c, "", $B, $newpos, 95)
                If GUICtrlRead($chk1) = $GUI_CHECKED Then WinSetTrans($g, "", 255 - ($newpos - 50) * 254 / 400)
                If GUICtrlRead($chk2) = $GUI_CHECKED Then WinSetTrans($c, "", 255 - ($newpos - 50) * 254 / 400)
                $lastpos = $newpos
            EndIf
        WEnd
    EndIf

    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $hExit
            Exit
    EndSwitch
WEnd

Func addRegion($MMask, $sx, $sy, $ex, $ey)
    Local $aMask = DllCall("gdi32.dll", "long", "CreateRectRgn", "long", $sx, "long", $sy, "long", $ex, "long", $ey)
    DllCall("gdi32.dll", "long", "CombineRgn", "long", $MMask[0], "long", $aMask[0], "long", $MMask[0], "int", 3)
EndFunc   ;==>addRegion


Func _RoundCorners($hWnd, $iR = 1)
    Local $x, $pos
    $pos = WinGetPos($hWnd)
    If $pos[2] > $pos[3] Then
        $x = 10 * $pos[2] / $pos[3]
    Else
        $x = 10 * $pos[3] / $pos[2]
    EndIf
    Local $aResult = DllCall("gdi32.dll", "handle", "CreateRoundRectRgn", "int", 0, "int", 0, "int", $pos[2], "int", $pos[3], _
            "int", $iR * $x, "int", $iR * $x)
    Local $aRet = DllCall("user32.dll", "int", "SetWindowRgn", "hwnd", $hWnd, "handle", $aResult[0], "bool", True)
    If @error Then Return SetError(@error, @extended, False)
    Return $aRet[0]
EndFunc   ;==>_RoundCorners
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

update: after many many tests and endless googlesearch, it still dont work like i want it.

example:

$hwnd = GUICreate("GDI+ Sample!", 500, 500)
$test = GUICtrlCreatePic("test.jpg", 0, 0, 500, 500)
$test2 = GUICtrlCreatePic("test.jpg", 200, 200, 100, 100)

result:

Posted Image

ok, now i want to draw another image over the background, but with free adjustable alpha.

example is with 30% alpha. the result should be:

Posted Image

is this possible with autoit?

Link to comment
Share on other sites

Try this:

#include <GDIP.au3>

Opt("GUIOnEventMode", 1)
_GDIPlus_Startup()
$hImage = _GDIPlus_ImageLoadFromFile(@ProgramFilesDir & "\AutoIt3\Examples\GUI\msoobe.jpg")
$iW = _GDIPlus_ImageGetWidth($hImage)
$iH = _GDIPlus_ImageGetHeight($hImage)

$W = 800
$H = 600
$hGUI = GUICreate("GDI+ Test by UEZ 2011", $W, $H)
GUISetState()

$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics($iW, $iH, $hGraphic)
$hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap)

$hAttribute_Alpha = _GDIPlus_ImageAttributesCreate()
$tColorMatrix = _GDIPlus_ColorMatrixCreateTranslate(0, 0, 0, -0.25) ;set alpha channel value
$pColorMatrix = DllStructGetPtr($tColorMatrix)
_GDIPlus_ImageAttributesSetColorMatrix($hAttribute_Alpha, 0, True, $pColorMatrix)

$d = 1
GUISetOnEvent(-3, "_Exit")
AdlibRegister("Draw", 30)

While Sleep(2^16)
WEnd

Func Draw()
    Local Static $i = 0
    _GDIPlus_GraphicsDrawImage($hContext, $hImage, 0, 0)
    _GDIPlus_GraphicsDrawString($hContext, "GDI+ TEST", 200, 420, "Arial", 50, 0)
    _GDIPlus_GraphicsDrawImageRectRectIA($hContext, $hImage, 0, 0, $iW, $iH, $i, 400, $iW / 8, $iH / 8, $hAttribute_Alpha)
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $iW, $iH)
    $i += 2 * $d
    If $i < 0 Or $i > 800 - $iW / 8 Then $d *= -1
EndFunc

Func _Exit()
    AdlibUnRegister("Draw")
    _GDIPlus_ImageAttributesDispose($hAttribute_Alpha)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_GraphicsDispose($hContext)
    _GDIPlus_BitmapDispose ($hBitmap)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
    Exit
EndFunc

You will need to run it properly!

Br,

UEZ

Edited by 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

Just to clarify and simply what I showed earlier, this is what I meant which would let you add controls to the transparent section as well.

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

Const $STM_SETIMAGE = 0x0172
Const $IMAGE_BITMAP = 0

$g = GUICreate("", 500, 500, -1, -1, BitOR($WS_SYSMENU, $WS_POPUP), $WS_EX_TOOLWINDOW)

GUICtrlCreatePic(@ProgramFilesDir & "\AutoIt3\Examples\GUI\msoobe.jpg", 0,0,500,500)
GUISetState()
$c = GUICreate("", 150, 150, 0, 200, $WS_POPUP, $WS_EX_MDICHILD, $g)
$Pic = GUICtrlCreatePic(@ProgramFilesDir & "\AutoIt3\Examples\GUI\msoobe.jpg", 0,0,150,150)

GUICtrlSetState(-1, $GUI_DISABLE)

GUISetState()

$lastpos = 0
$way = 1
WinSetTrans($c,"",100)
adlibregister("shift",50)

While 1
    Sleep(1000)

WEnd


func shift()
    Local $pp = WinGetPos($g)
    $lastpos += 2* $way
    if $lastpos >= 500-150 then $way = -1
    if $lastpos <= 0 then $way = 1
    winmove($c,"",$pp[0] + $lastpos,$pp[1] + 200)
EndFunc
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Good morning,

thanks UEZ for the hint, after another few tests and modifications this was my solution! :mellow:

it takes only 11 additional lines in my script to do what i want.

i can now set a backgroundimage and a foregroundimage and before joining them to one layer

it is possible via two sliders set rotation (angle) and transparentvalue of the foreground.

it is for a little paint-tool which i wrote in autoit (constantly refine it...)

ok then, have a nice day

Link to comment
Share on other sites

here is a little stand-alone-script which should show how i am using it:

#include <GuiConstants.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <GDIP.au3>
$hGUI = GUICreate("temp", 10, 10)
global $cr = "0xffff0000", $cr2 = "0xff00ff00", $tt
Func SystemEvents()
    Switch @GUI_CtrlId
        Case $GUI_EVENT_PRIMARYDOWN
            DllCall("user32.dll","int","SendMessage","hWnd", $tt,"int",$WM_NCLBUTTONDOWN,"int", $HTCAPTION,"int", 0)
        Case $GUI_EVENT_SECONDARYDOWN
            exit
    EndSwitch
endfunc
_GDIPlus_Startup()
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hgui)
$hPen = _GDIPlus_PenCreate($cr, 20, 2)
$kPen = _GDIPlus_BrushCreateSolid($cr)
$cr22 = _GDIPlus_BrushCreateSolid($cr2)
$hFamily = _GDIPlus_FontFamilyCreate ("Arial")
$hFont = _GDIPlus_FontCreate ($hFamily, 20, 0)
$tLayout = _GDIPlus_RectFCreate (-1, -1, 0, 0)
$hFormat = _GDIPlus_StringFormatCreate ()
_GDIPlus_StringFormatSetAlign($hFormat, 0)
$aInfo = _GDIPlus_GraphicsMeasureString ($hGraphic, "Hello World", $hFont, $tLayout, $hFormat)
Opt("GuiOnEventMode", 1)
$matx = sqrt((DllStructGetData($aInfo[0],3)*DllStructGetData($aInfo[0],3))+(DllStructGetData($aInfo[0],4)*DllStructGetData($aInfo[0],4)))
$tt = GUICreate("temptext", $matx, $matx+40, -1, -1, BitOR($WS_POPUP,$WS_BORDER,$WS_CLIPCHILDREN), $WS_EX_LAYERED+$WS_EX_TOPMOST)
GUISetBkColor(0x010101)
$tmpsld = GUICtrlCreateSlider(0, $matx, $matx, 20)
GUICtrlSetLimit($tmpsld, 180, -180)
GUICtrlSetData($tmpsld, 0)
$tmpsld2 = GUICtrlCreateSlider(0, $matx+20, $matx, 20)
GUICtrlSetLimit($tmpsld2, 255, 40)
GUICtrlSetData($tmpsld2, 255)
GUISetState()
_WinAPI_SetLayeredWindowAttributes($tt, 0x010101, 255)
$tGraphic = _GDIPlus_GraphicsCreateFromHWND($tt)
$hMatrix = _GDIPlus_MatrixCreate()
_GDIPlus_MatrixTranslate($hMatrix, round($matx/2), round($matx/2))
_GDIPlus_MatrixRotate($hMatrix, 0)
_GDIPlus_GraphicsSetTransform($tGraphic, $hMatrix)
$ttLayout = _GDIPlus_RectFCreate (-round(DllStructGetData($aInfo[0],3)/2), -round(DllStructGetData($aInfo[0],4)/2), DllStructGetData($aInfo[0],3), DllStructGetData($aInfo[0],4))
_GDIPlus_GraphicsFillRect($tGraphic, -round(DllStructGetData($aInfo[0],3)/2), -round(DllStructGetData($aInfo[0],4)/2), DllStructGetData($aInfo[0],3), DllStructGetData($aInfo[0],4), $cr22)
_GDIPlus_GraphicsDrawStringEx ($tGraphic, "Hello World", $hFont, $ttLayout, $hFormat, $kPen)
GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "SystemEvents")
GUISetOnEvent($GUI_EVENT_secondARYDOWN, "SystemEvents")
$angle = 0
$temptrans = 255
do
    Sleep(20)
    if $angle <> GUICtrlRead($tmpsld) or $temptrans <> GUICtrlRead($tmpsld2) then
        $temptrans = GUICtrlRead($tmpsld2)
        _WinAPI_SetLayeredWindowAttributes($tt,0x010101,GUICtrlRead($tmpsld2))
        _GDIPlus_GraphicsClear($tGraphic, 0xFF010101)
        $hMatrix = _GDIPlus_MatrixCreate()
        _GDIPlus_MatrixTranslate($hMatrix, round($matx/2), round($matx/2))
        _GDIPlus_MatrixRotate($hMatrix, GUICtrlRead($tmpsld))
        _GDIPlus_GraphicsSetTransform($tGraphic, $hMatrix)
        _GDIPlus_GraphicsFillRect($tGraphic, -round(DllStructGetData($aInfo[0],3)/2), -round(DllStructGetData($aInfo[0],4)/2), DllStructGetData($aInfo[0],3), DllStructGetData($aInfo[0],4), $cr22)
        _GDIPlus_GraphicsDrawStringEx ($tGraphic, "Hello World", $hFont, $ttLayout, $hFormat, $kPen)
        $angle = GUICtrlRead($tmpsld)
    endif
    if $angle <> 0 then
        ToolTip($angle)
    Else
        tooltip("")
    endif
until 0

in the mainscript there is much more code. makes fun to paint with, tooks long enough to get it work :mellow:

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...