Jump to content

Custom Backgrounds


picea892
 Share

Recommended Posts

Hi all

I recently have been experimenting with GDI backgrounds, and these examples are really a continuation of this Instead of experimenting with gradients, this example focuses on fading in a background with the intent of making borders, shadows and glow effect. Include the picture in the same directory as the script

Any improvements and suggestions are welcome.

#include <GUIConstantsEx.au3>
#include <GDIPlus.au3>
#include <WindowsConstants.au3>
#include <WINAPI.au3>
#include <Color.au3>
commonbck()
HotKeySet("{esc}","quiter")
Func commonbck()
    $mainwidth=560
    $mainheight=250
    $Gui = GUICreate("", $mainwidth,$mainheight,-1,-1, $WS_POPUP, $WS_EX_TOOLWINDOW)
    $Pic = GUICtrlCreatePic("", 0, 0, $mainwidth, $mainheight)
    GUICtrlSetState(-1, $GUI_DISABLE)
    _mainbackground(-1,$mainwidth,$mainheight,"metal_small.jpg")
    $bckpic= GUICtrlCreatePic("",25,12,150, 150)
    GuiCtrlSetState(-1,$GUI_DISABLE)
    background(-1,"0xBB000000","0x880000FF","6","10") ; blue
    $bckpic2= GUICtrlCreatePic("",25,170,280, 70)
    GuiCtrlSetState(-1,$GUI_DISABLE)
    background(-1,"0xFF660000","0x88220000","14","6") ;red
    $bckpic3= GUICtrlCreatePic("",180,15,120, 40)
    GuiCtrlSetState(-1,$GUI_DISABLE)
    background(-1,"0x8800AA00","0x88003300","4","16") ; green
    
    $bckshadow= GUICtrlCreatePic("",320,10,120, 70)
    GuiCtrlSetState(-1,$GUI_DISABLE)
    fadebackground(-1,"0x000033","0x000099","21","17","12")
    
    $bckshadow2= GUICtrlCreatePic("",320,90,120, 60)
    GuiCtrlSetState(-1,$GUI_DISABLE)
    fadebackground(-1,"0xCCCC00","0x555500","33","10","8")
        
    $bckshadow3= GUICtrlCreatePic("",320,160,120, 80)
    GuiCtrlSetState(-1,$GUI_DISABLE)
   fadebackground2(-1,"0xCCCC00","55","30","8")
    $bckshadow3= GUICtrlCreatePic("",440,165,100, 70)
    GuiCtrlSetState(-1,$GUI_DISABLE)
   fadebackground2(-1,"0x550000","66","30","0")
   
    $round= GUICtrlCreatePic("",500,12,50, 50)
    GuiCtrlSetState(-1,$GUI_DISABLE)
    background(-1,"0xDD000000","0x880000FF","10","16") ; blue
    GUISetState()

EndFunc

While 1
sleep(50)
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                Exit
        EndSelect
WEnd

func _mainbackground($guicontrol,$reqwidth,$reqheight,$picname="")
    _GDIPlus_Startup()
    Local $hImage1 = _GDIPlus_ImageLoadFromFile($picname)
    Local $iX1 = _GDIPlus_ImageGetWidth($hImage1)+15
    Local $hBitmap = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $reqwidth, "int", $reqheight, "int", 0, "int",  0x0026200A, "ptr", 0, "int*", 0)
    $hBitmap = $hBitmap[6]
    Local $hCtx = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    For $i = 0 To $reqwidth Step $iX1
        _GDIPlus_GraphicsDrawImage($hCtx, $hImage1, $i, 0)
    Next
    _GDIPlus_GraphicsDrawborder($hCtx, 0, 0, $reqwidth, $reqheight, 6,"AA",0x000011,10)
    Local $hBmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_GraphicsDispose($hCtx)
    Local Const $STM_SETIMAGE = 0x0172
    _WinAPI_DeleteObject(GUICtrlSendMsg($guicontrol, $STM_SETIMAGE, 0, $hBmp))
    _WinAPI_DeleteObject($hBmp)
    _GDIPlus_Shutdown ()
EndFunc

Func _GDIPlus_GraphicsDrawborder($hGraphics, $iX, $iY, $iWidth, $iHeight, $iRadius, $hPen, $colour, $thick)
    $transpen=_GDIPlus_PenCreate(BitOR("0x"&$hPen&"000000",$colour),1)
    for $r=0 to $thick
        _GDIPlus_GraphicsDrawLine($hGraphics, $iX+$thick+1, $iY+$r, $iX + $iWidth-$thick-1, $iY+$r,$transpen) ; top
        _GDIPlus_GraphicsdrawLine($hGraphics, $iX + $iWidth-$r, $iY, $iX + $iWidth-$r, $iY + $iHeight,$transpen) ;right
        _GDIPlus_GraphicsdrawLine($hGraphics, $iX + $iWidth-$thick-1, $iY + $iHeight-$r, $iX+$thick+1, $iY + $iHeight-$r,$transpen) ;bottom
        _GDIPlus_GraphicsdrawLine($hGraphics, $iX+$r, $iY + $iHeight, $iX+$r, $iY,$transpen) ;left
        _GDIPlus_PenDispose($hPen)
    Next
EndFunc 



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
func background($control,$col1,$col2,$deep,$cornered)
    _GDIPlus_Startup ()
    $hwd=GUICtrlGetHandle($control)
    ;$guiname=_WinAPI_GetParent($hwd)
    $width=_WinAPI_GetClientWidth($hwd)
    $height=_WinAPI_GetClientHeight($hwd)
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hwd)
    $hBitmap1 = _GDIPlus_BitmapCreateFromGraphics($width,$height, $hGraphic)
    $hBitmap1=_drawborder($hBitmap1,$col1,$col2,$deep,$cornered)
    $hImage1 = _GDIPlus_ImageGetGraphicsContext($hBitmap1)
    _GDIPlus_GraphicsSetSmoothingMode($hImage1, 2)
    $hBMP1 = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap1)
     _WinAPI_DeleteObject(GUICtrlSendMsg($control, 0x0172, 0, $hBMP1))
    _GDIPlus_ImageDispose ($hImage1)
    _WinAPI_DeleteObject($hBMP1)
    _WinAPI_DeleteObject($hBitmap1)
    _GDIPlus_GraphicsDispose ($hGraphic)
    _GDIPlus_Shutdown ()
EndFunc 

;handler, to draw background and createbutton3
Func _drawborder($hBitmap, $colour1, $colour2, $depth, $corner) ;this is for normal buttons
    $gwt = _GDIPlus_ImageGetWidth($hBitmap) - 1
    $ght = _GDIPlus_ImageGetHeight($hBitmap) - 1
    $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    Local $bGammaCorrection = False
    Global $hBrush1 = _GDIPlus_BrushCreateSolid($colour1)
    Global $hBrush = _GDIPlus_BrushCreateSolid($colour2)
    _GDIPlus_GraphicsSetSmoothingMode($hGraphics, 4)
        _GDIPlus_GraphicsDrawRoundRect($hGraphics, 0, 0, $gwt, $ght, $corner, $hBrush)
        _GDIPlus_GraphicsDrawRoundRect($hGraphics, $depth, $depth, $gwt - ($depth * 2), $ght - ($depth * 2), $corner - 2, $hBrush1)

    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_BrushDispose($hBrush1)

    _GDIPlus_GraphicsDispose($hGraphics)
    Return $hBitmap
EndFunc ;==>_drawborder


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
func fadebackground($control,$col1,$col2,$faded,$deep,$cornered)
    _GDIPlus_Startup ()
    $hwd=GUICtrlGetHandle($control)
    ;$guiname=_WinAPI_GetParent($hwd)
    $width=_WinAPI_GetClientWidth($hwd)
    $height=_WinAPI_GetClientHeight($hwd)
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hwd)
    $hBitmap1 = _GDIPlus_BitmapCreateFromGraphics($width,$height, $hGraphic)
    $hBitmap1=_fadeborder($hBitmap1,$col1,$col2, $faded, $deep,$cornered)
    $hImage1 = _GDIPlus_ImageGetGraphicsContext($hBitmap1)
    _GDIPlus_GraphicsSetSmoothingMode($hImage1, 2)
    $hBMP1 = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap1)
     _WinAPI_DeleteObject(GUICtrlSendMsg($control, 0x0172, 0, $hBMP1))
    _GDIPlus_ImageDispose ($hImage1)
    _WinAPI_DeleteObject($hBMP1)
    _WinAPI_DeleteObject($hBitmap1)
    _GDIPlus_GraphicsDispose ($hGraphic)
    _GDIPlus_Shutdown ()
EndFunc 

Func _fadeborder($hBitmap, $col1,$col2, $faded, $depth, $corner)
    $gwt = _GDIPlus_ImageGetWidth($hBitmap)
    $ght = _GDIPlus_ImageGetHeight($hBitmap)

    $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    Local $bGammaCorrection = False
    _GDIPlus_GraphicsSetSmoothingMode($hGraphics, 4)
    $f = _GUICtrlCreateGradient($col1,$col2,$depth)

For $i=0 To $depth
    $iC1 = StringReplace($f[$i], "0x", "0x"&$faded)
     Global $hBrush = _GDIPlus_BrushCreateSolid($iC1)
     GDIPlus_SetLineGammaCorrection($hBrush, $bGammaCorrection)
        _GDIPlus_GraphicsDrawRoundRect($hGraphics, $i, $i, $gwt-($i*2), $ght-($i*2), $corner, $hBrush)
    _GDIPlus_BrushDispose($hBrush)
Next
    _GDIPlus_GraphicsDispose($hGraphics)
    Return $hBitmap
EndFunc

Func _GUICtrlCreateGradient($col1,$col2,$depth2)
    Local $color1R = _ColorGetRed($col1)
    Local $color1G = _ColorGetGreen($col1)
    Local $color1B = _ColorGetBlue($col1)

    Local $nStepR = (_ColorGetRed($col2) - $color1R) / $depth2
    Local $nStepG = (_ColorGetGreen($col2) - $color1G) / $depth2
    Local $nStepB = (_ColorGetBlue($col2) - $color1B) / $depth2
dim $colours[$depth2+1]

    For $i = 0 To $depth2
        $colours[$i] = "0x" & StringFormat("%02X%02X%02X", $color1R+$nStepR*$i, $color1G+$nStepG*$i, $color1B+$nStepB*$i)
    Next
    Return $colours
EndFunc

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
func fadebackground2($control,$col1,$faded,$deep,$cornered)
    _GDIPlus_Startup ()
    $hwd=GUICtrlGetHandle($control)
    ;$guiname=_WinAPI_GetParent($hwd)
    $width=_WinAPI_GetClientWidth($hwd)
    $height=_WinAPI_GetClientHeight($hwd)
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hwd)
    $hBitmap1 = _GDIPlus_BitmapCreateFromGraphics($width,$height, $hGraphic)
    $hBitmap1=_fadeborder2($hBitmap1,$col1, $faded, $deep,$cornered)
    $hImage1 = _GDIPlus_ImageGetGraphicsContext($hBitmap1)
    _GDIPlus_GraphicsSetSmoothingMode($hImage1, 2)
    $hBMP1 = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap1)
     _WinAPI_DeleteObject(GUICtrlSendMsg($control, 0x0172, 0, $hBMP1))
    _GDIPlus_ImageDispose ($hImage1)
    _WinAPI_DeleteObject($hBMP1)
    _WinAPI_DeleteObject($hBitmap1)
    _GDIPlus_GraphicsDispose ($hGraphic)
    _GDIPlus_Shutdown ()
EndFunc 

Func _fadeborder2($hBitmap, $col1, $faded, $depth, $corner)
    $gwt = _GDIPlus_ImageGetWidth($hBitmap)
    $ght = _GDIPlus_ImageGetHeight($hBitmap)

    $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    Local $bGammaCorrection = False
    _GDIPlus_GraphicsSetSmoothingMode($hGraphics, 4)
$fadechange=int($faded/$depth)
For $i=0 To $depth
    ConsoleWrite($fadechange&@crlf)
    $iC1 = StringReplace($col1, "0x", "0x"&$fadechange*$i)
     Global $hBrush = _GDIPlus_BrushCreateSolid($iC1)
     GDIPlus_SetLineGammaCorrection($hBrush, $bGammaCorrection)
        _GDIPlus_GraphicsDrawRoundRect($hGraphics, $i, $i, $gwt-($i*2), $ght-($i*2), $corner, $hBrush)
    _GDIPlus_BrushDispose($hBrush)
Next
    _GDIPlus_GraphicsDispose($hGraphics)
    Return $hBitmap
EndFunc


While 1
    $MSG = GUIGetMsg()
    Switch $MSG
        Case -3
            Quiter()
    EndSwitch
WEnd

Func Quiter()
    Exit
EndFunc


Func _GDIPlus_GraphicsDrawRoundRect($hGraphics, $iX, $iY, $iWidth, $iHeight, $iRadius, $hBrush = 0, $hPen = 0)
    _GDIPlus_PenCreate($hPen)
    Local $hPath = _GDIPlus_GraphicsPathCreate()
    _GDIPlus_GraphicsPathAddLine($hPath, $iX + $iRadius, $iY, $iX + $iWidth - ($iRadius * 2), $iY)
    _GDIPlus_GraphicsPathAddArc($hPath, $iX + $iWidth - ($iRadius * 2), $iY, $iRadius * 2, $iRadius * 2, 270, 90)
    _GDIPlus_GraphicsPathAddLine($hPath, $iX + $iWidth, $iY + $iRadius, $iX + $iWidth, $iY + $iHeight - ($iRadius * 2))
    _GDIPlus_GraphicsPathAddArc($hPath, $iX + $iWidth - ($iRadius * 2), $iY + $iHeight - ($iRadius * 2), $iRadius * 2, $iRadius * 2, 0, 90)
    _GDIPlus_GraphicsPathAddLine($hPath, $iX + $iWidth - ($iRadius * 2), $iY + $iHeight, $iX + $iRadius, $iY + $iHeight)
    _GDIPlus_GraphicsPathAddArc($hPath, $iX, $iY + $iHeight - ($iRadius * 2), $iRadius * 2, $iRadius * 2, 90, 90)
    _GDIPlus_GraphicsPathAddLine($hPath, $iX, $iY + $iHeight - ($iRadius * 2), $iX, $iY + $iRadius)
    _GDIPlus_GraphicsPathAddArc($hPath, $iX, $iY, $iRadius * 2, $iRadius * 2, 180, 90)

    ;Draw the font onto the new bitmap
    _GDIPlus_GraphicsPathCloseFigure($hPath)
    If $hBrush <> 0 Then _GDIPlus_GraphicsFillPath($hGraphics, $hBrush, $hPath)
    _GDIPlus_GraphicsDrawPath($hGraphics, $hPen, $hPath)
    _GDIPlus_GraphicsPathDispose($hPath)
    _GDIPlus_PenDispose($hPen)
EndFunc ;==>_GDIPlus_GraphicsDrawRoundRect



;==== GDIPlus_CreateLineBrushFromRect === Malkey's function
Func _GDIPlus_CreateLineBrushFromRect($iX, $iY, $iWidth, $iHeight, $aFactors, $aPositions, _
        $iArgb1 = 0xFF0000FF, $iArgb2 = 0xFFFF0000, $LinearGradientMode = 0x00000001, $WrapMode = 0)

    Local $tRect, $pRect, $aRet, $tFactors, $pFactors, $tPositions, $pPositions, $iCount

    If $iArgb1 = Default Then $iArgb1 = 0xFF0000FF
    If $iArgb2 = Default Then $iArgb2 = 0xFFFF0000
    If $LinearGradientMode = -1 Or $LinearGradientMode = Default Then $LinearGradientMode = 0x00000001
    If $WrapMode = -1 Or $LinearGradientMode = Default Then $WrapMode = 1

    $tRect = DllStructCreate("float X;float Y;float Width;float Height")
    $pRect = DllStructGetPtr($tRect)
    DllStructSetData($tRect, "X", $iX)
    DllStructSetData($tRect, "Y", $iY)
    DllStructSetData($tRect, "Width", $iWidth)
    DllStructSetData($tRect, "Height", $iHeight)
    $aRet = DllCall($ghGDIPDll, "int", "GdipCreateLineBrushFromRect", "ptr", $pRect, "int", $iArgb1, _
            "int", $iArgb2, "int", $LinearGradientMode, "int", $WrapMode, "int*", 0)

    If IsArray($aFactors) = 0 Then Dim $aFactors[4] = [0.0, 0.4, 0.6, 1.0]
    If IsArray($aPositions) = 0 Then Dim $aPositions[4] = [0.0, 0.3, 0.7, 1.0]
    $iCount = UBound($aPositions)
    $tFactors = DllStructCreate("float[" & $iCount & "]")
    $pFactors = DllStructGetPtr($tFactors)
    For $iI = 0 To $iCount - 1
        DllStructSetData($tFactors, 1, $aFactors[$iI], $iI + 1)
    Next
    $tPositions = DllStructCreate("float[" & $iCount & "]")
    $pPositions = DllStructGetPtr($tPositions)
    For $iI = 0 To $iCount - 1
        DllStructSetData($tPositions, 1, $aPositions[$iI], $iI + 1)
    Next
    $hStatus = DllCall($ghGDIPDll, "int", "GdipSetLineBlend", "hwnd", $aRet[6], _
            "ptr", $pFactors, "ptr", $pPositions, "int", $iCount)
    Return $aRet[6] ; Handle of Line Brush
EndFunc ;==>_GDIPlus_CreateLineBrushFromRect

Func GDIPlus_SetLineGammaCorrection($hBrush, $useGammaCorrection = True)
    Local $aResult
    $aResult = DllCall($ghGDIPDll, "int", "GdipSetLineGammaCorrection", "hwnd", $hBrush, "int", $useGammaCorrection)
    Return $aResult[0]
EndFunc ;==>GDIPlus_SetLineGammaCorrection

Func _GDIPlus_GraphicsPathCreate($iFillMode = 0)
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipCreatePath", "int", $iFillMode, "int*", 0);
    If @error Then Return SetError(@error, @extended, 0)
    Return SetError($aResult[0], 0, $aResult[2])
EndFunc ;==>_GDIPlus_GraphicsPathCreate
Func _GDIPlus_GraphicsPathAddLine($hGraphicsPath, $iX1, $iY1, $iX2, $iY2)
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipAddPathLine", "hwnd", $hGraphicsPath, "float", $iX1, "float", $iY1, _
            "float", $iX2, "float", $iY2)
    If @error Then Return SetError(@error, @extended, 0)
    Return SetError($aResult[0], 0, 0)
EndFunc ;==>_GDIPlus_GraphicsPathAddLine
Func _GDIPlus_GraphicsPathAddArc($hGraphicsPath, $iX, $iY, $iWidth, $iHeight, $iStartAngle, $iSweepAngle)
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipAddPathArc", "hwnd", $hGraphicsPath, "float", $iX, "float", $iY, _
            "float", $iWidth, "float", $iHeight, "float", $iStartAngle, "float", $iSweepAngle)
    If @error Then Return SetError(@error, @extended, 0)
    Return SetError($aResult[0], 0, 0)
EndFunc ;==>_GDIPlus_GraphicsPathAddArc
Func _GDIPlus_GraphicsPathCloseFigure($hGraphicsPath)
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipClosePathFigure", "hwnd", $hGraphicsPath)
    If @error Then Return SetError(@error, @extended, 0)
    Return SetError($aResult[0], 0, 0)
EndFunc ;==>_GDIPlus_GraphicsPathCloseFigure
Func _GDIPlus_GraphicsPathDispose($hGraphicsPath)
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipDeletePath", "hwnd", $hGraphicsPath)
    If @error Then Return SetError(@error, @extended, 0)
    Return SetError($aResult[0], 0, 0)
EndFunc ;==>_GDIPlus_GraphicsPathDispose
Func _GDIPlus_GraphicsDrawPath($hGraphics, $hPen, $hGraphicsPath)
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipDrawPath", "hwnd", $hGraphics, "hwnd", $hPen, "hwnd", $hGraphicsPath)
    If @error Then Return SetError(@error, @extended, 0)
    Return SetError($aResult[0], 0, 0)
EndFunc ;==>_GDIPlus_GraphicsDrawPath
Func _GDIPlus_GraphicsFillPath($hGraphics, $hBrush, $hGraphicsPath)
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipFillPath", "hwnd", $hGraphics, "hwnd", $hBrush, "hwnd", $hGraphicsPath)
    If @error Then Return SetError(@error, @extended, 0)
    Return SetError($aResult[0], 0, 0)
EndFunc ;==>_GDIPlus_GraphicsFillPath

post-45064-0-84562800-1308788227_thumb.j

post-45064-0-05541600-1308789050_thumb.j

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