seniors 发表于 2013-5-29 10:00:44

第十讲之分解2-多色渐变画刷

第十讲之分解2-多色渐变画刷
1、_GDIPlus_LineBrushSetPresetBlend用于设置多色渐变画刷
多色渐变的第一个颜色位置必须是0,最后一个颜色位置必须是1,不然相当于没有设置
2、_GDIPlus_LineBrushSetLinearBlend用于设置最终颜色到nFocus位置结束,nFocus是%值;及设置最终颜色在渐变色中是nScale,nScale指从左开始的%值
3、_GDIPlus_LineBrushSetSigmaBlend作用和_GDIPlus_LineBrushSetLinearBlend相似,只是它的颜色变化是曲线变化,而_GDIPlus_LineBrushSetLinearBlend是线性突变

自己调节滑块测试吧

#include <GUIConstantsEx.au3>
#include <APIConstants.au3>
#include <WinAPIEx.au3>
#include <GDIPlus.au3>
#include <GDIPlusEx.au3>

Global $rectx = 5, $recty = 5, $rectw = 100, $recth = 100
Global $brushx = $rectx, $brushy = $recty, $brushw = 0, $brushh = 50
GUICreate("第十讲之分解2-多色渐变画刷", 500, 400)
$nCtrlId = GUICtrlCreatePic("", 0, 0, 500, 200)
$hPicWnd = GUICtrlGetHandle($nCtrlId)

Global $SigmaBlend, $LinearBlend, $check, $aIp
init()
GUIRegisterMsg($WM_HSCROLL, "onHSCROLL")
GUISetState()

update()

While 1
        $Msg = GUIGetMsg()
        Switch $Msg
                Case -3
                        ExitLoop
                Case $check, $check, $check
                        update()
        EndSwitch
WEnd

GUIDelete()
Exit

Func init()
        $check = GUICtrlCreateRadio("SigmaBlendnFocus", 5, 205)
        GUICtrlSetState(-1, $GUI_CHECKED)
        $SigmaBlend = GUICtrlCreateSlider(130, 205, 100)
        GUICtrlSetLimit(-1, 100, 0)
        GUICtrlSetData(-1, 0)
        $SigmaBlend = GUICtrlCreateLabel("", 235, 205, 40, 25)
        GUICtrlSetData(-1, StringFormat("%.2f", 0))
        GUICtrlCreateLabel("nScale", 295, 205)
        $SigmaBlend = GUICtrlCreateSlider(350, 205, 100)
        GUICtrlSetLimit(-1, 100, 0)
        GUICtrlSetData(-1, 100)
        $SigmaBlend = GUICtrlCreateLabel("", 455, 205, 40, 25)
        GUICtrlSetData(-1, StringFormat("%.2f", 1))

        $check = GUICtrlCreateRadio("LinearBlendnFocus", 5, 235)
        $LinearBlend = GUICtrlCreateSlider(130, 235, 100)
        GUICtrlSetLimit(-1, 100, 0)
        GUICtrlSetData(-1, 0)
        $LinearBlend = GUICtrlCreateLabel("", 235, 235, 40, 25)
        GUICtrlSetData(-1, StringFormat("%.2f", 0))
        GUICtrlCreateLabel("nScale", 295, 235)
        $LinearBlend = GUICtrlCreateSlider(350, 235, 100)
        GUICtrlSetLimit(-1, 100, 0)
        GUICtrlSetData(-1, 100)
        $LinearBlend = GUICtrlCreateLabel("", 455, 235, 40, 25)
        GUICtrlSetData(-1, StringFormat("%.2f", 1))
       
        $check = GUICtrlCreateRadio("多色渐变", 5, 265)
        GUICtrlCreateLabel("颜色1位置", 75, 295)
        $aIp = GUICtrlCreateSlider(130, 295, 100)
        GUICtrlSetLimit(-1, 100, 0)
        GUICtrlSetData(-1, 0)
        $aIp = GUICtrlCreateLabel("", 235, 295, 40, 25)
        GUICtrlSetData(-1, StringFormat("%.2f", 0))
       
        GUICtrlCreateLabel("颜色2位置", 295, 295)
        $aIp = GUICtrlCreateSlider(350, 295, 100)
        GUICtrlSetLimit(-1, 100, 0)
        GUICtrlSetData(-1, 30)
        $aIp = GUICtrlCreateLabel("", 455, 295, 40, 25)
        GUICtrlSetData(-1, StringFormat("%.2f", 0.3))
       
        GUICtrlCreateLabel("颜色3位置", 75, 325)
        $aIp = GUICtrlCreateSlider(130, 325, 100)
        GUICtrlSetLimit(-1, 100, 0)
        GUICtrlSetData(-1, 70)
        $aIp = GUICtrlCreateLabel("", 235, 325, 40, 25)
        GUICtrlSetData(-1, StringFormat("%.2f", 0.7))
       
        GUICtrlCreateLabel("颜色4位置", 295, 325)
        $aIp = GUICtrlCreateSlider(350, 325, 100)
        GUICtrlSetLimit(-1, 100, 0)
        GUICtrlSetData(-1, 100)
        $aIp = GUICtrlCreateLabel("", 455, 325, 40, 25)
        GUICtrlSetData(-1, StringFormat("%.2f", 1))
EndFunc   ;==>init

Func onHSCROLL($hWnd, $iMsg, $wParam, $lParam)
        Switch $lParam
                Case GUICtrlGetHandle($SigmaBlend)
                        $ls = GUICtrlRead($SigmaBlend)
                        GUICtrlSetData($SigmaBlend, StringFormat("%.2f", $ls / 100))
                Case GUICtrlGetHandle($SigmaBlend)
                        $ls = GUICtrlRead($SigmaBlend)
                        GUICtrlSetData($SigmaBlend, StringFormat("%.2f", $ls / 100))
                       
                Case GUICtrlGetHandle($LinearBlend)
                        $ls = GUICtrlRead($LinearBlend)
                        GUICtrlSetData($LinearBlend, StringFormat("%.2f", $ls / 100))
                Case GUICtrlGetHandle($LinearBlend)
                        $ls = GUICtrlRead($LinearBlend)
                        GUICtrlSetData($LinearBlend, StringFormat("%.2f", $ls / 100))
                       
                Case GUICtrlGetHandle($aIp)
                        $ls = GUICtrlRead($aIp)
                        GUICtrlSetData($aIp, StringFormat("%.2f", $ls / 100))
                Case GUICtrlGetHandle($aIp)
                        $ls = GUICtrlRead($aIp)
                        GUICtrlSetData($aIp, StringFormat("%.2f", $ls / 100))
                Case GUICtrlGetHandle($aIp)
                        $ls = GUICtrlRead($aIp)
                        GUICtrlSetData($aIp, StringFormat("%.2f", $ls / 100))
                Case GUICtrlGetHandle($aIp)
                        $ls = GUICtrlRead($aIp)
                        GUICtrlSetData($aIp, StringFormat("%.2f", $ls / 100))
        EndSwitch
        update()
EndFunc   ;==>onHSCROLL

Func update()
        Local $HWND_CX = _WinAPI_GetWindowWidth($hPicWnd)
        Local $HWND_CY = _WinAPI_GetWindowHeight($hPicWnd)
        _GDIPlus_Startup()
        $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hPicWnd)
        $hBitmap = _GDIPlus_BitmapCreateFromGraphics($HWND_CX, $HWND_CY, $hGraphics)
        $hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
        _GDIPlus_GraphicsClear($hBackbuffer, 0xFFECE9D8)
        _GDIPlus_GraphicsSetSmoothingMode($hBackbuffer, 2);光滑模式,2为8*8抗距齿
        lineBrushComp($hBackbuffer)
        _GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, $HWND_CX, $HWND_CY)
        _GDIPlus_BitmapDispose($hBitmap)
        _GDIPlus_GraphicsDispose($hBackbuffer)
        _GDIPlus_GraphicsDispose($hGraphics)
        _GDIPlus_Shutdown()
        _WinAPI_EmptyWorkingSet()
EndFunc   ;==>update

Func lineBrushComp($hGraphics)
        Local $hPen = _GDIPlus_PenCreate(0xFFFFFFFF, 2)
        Local $hPath = _GDIPlus_PathCreate()
        ;$rectx, $recty, $rectw, $recth分别是矩形路径的起点XY坐标,及宽高
        _GDIPlus_PathAddRectangle($hPath, $rectx, $recty, $rectw, $recth)
        Local $hBrush
        ;下面是4种填充方式的对比,0是不翻转,1是水平翻转,2 是垂直翻转,3是水平垂直均翻转,其中4是不铺设,所以不演示了
        ;水平翻转就是到超过画刷高到第二行时,水平翻转
        ;垂直翻转就是到超过画刷宽到第二列时,垂直翻转
        ;对于线性渐变画刷,只有翻转或者不翻转,所以0,2是翻转,1,3是翻转
        ;$brushx, $brushy, $brushw, $brushh分别是我设置的画刷的起点XY及宽高
        ;_GDIPlus_LineBrushCreate函数的参数是起点XY及终点XY,所以终点的XY用了起点+宽高
        Local $endx = $brushw + $brushx, $endy = $brushh + $brushy
        ;============================================================
        ;说明:当画刷起点和填充的起点一至时,填充效果最容易控制,自己在界面上拉动体会一下
        ;============================================================
        ;开始设置多色渐变,多色渐变的第一个颜色位置必须是0,最后一个颜色位置必须是1,不然相当于没有设置
        If GUICtrlRead($check) = $GUI_CHECKED Then
                For $i = 0 To 3
                        $hBrush = _GDIPlus_LineBrushCreate($brushx, $brushy, $endx, $endy, 0xFF00FF00, 0xFFFF0000, $i)
                        ; 定义渐变刷颜色和位置
                        Local $aInterpolations
                        $aInterpolations = 4; 使用4个要素和位置
                        $aInterpolations = 0xFFFF0000; 红色
                        $aInterpolations = GUICtrlRead($aIp); 从左侧0%
                        $aInterpolations = 0xFFFFFF00; 黄色
                        $aInterpolations = GUICtrlRead($aIp); 从左侧30%
                        $aInterpolations = 0xFF00FFFF; 青色
                        $aInterpolations = GUICtrlRead($aIp); 从左侧70%
                        $aInterpolations = 0xFFFF00FF; 紫色
                        $aInterpolations = GUICtrlRead($aIp); 从左侧100%
                        _GDIPlus_LineBrushSetPresetBlend($hBrush, $aInterpolations)
                        _GraphicsFillPath($hGraphics, $hPath, $hBrush)
                        _GraphicsDrawPath($hGraphics, $hPath, $hPen)
                        _GDIPlus_BrushDispose($hBrush)
                        _GraphicsDrawString($hGraphics, "渐变" & $i, $rectx, $recty + $recth + 10)
                        _GDIPlus_GraphicsTranslateTransform($hGraphics, $rectx + $rectw + 10, 0)
                Next
                _GDIPlus_PathDispose($hPath)
                _GDIPlus_PenDispose($hPen)
                ;================================在左下角绘制原画刷样子开始===========================
                ;复位画布
                _GDIPlus_GraphicsResetTransform($hGraphics)
                _GDIPlus_GraphicsTranslateTransform($hGraphics, 4, $recty + $recth + 20)
                $hPath = _GDIPlus_PathCreate()
                _GDIPlus_PathAddRectangle($hPath, $brushx, $brushy, 49, 49)
                $hBrush = _GDIPlus_LineBrushCreate($brushx, $brushy, $endx, $endy, 0xFF00FF00, 0xFFFF0000)
                _GDIPlus_LineBrushSetPresetBlend($hBrush, $aInterpolations)
                _GraphicsFillPath($hGraphics, $hPath, $hBrush)
                _GDIPlus_BrushDispose($hBrush)
                ;================================绘制原画刷样子结束===========================
        Else
                ;_GDIPlus_LineBrushSetSigmaBlend($hBrush, 0.7, 0.5)说明终止色在渐变画刷到0.7位置时结束,后面0.7-1基于贝尔曲线平滑变化
                ;0.5说明终止色是渐变画刷中颜色在中间
                ;_GDIPlus_LineBrushSetLinearBlend与_GDIPlus_LineBrushSetSigmaBlend相似,只是颜色是突变的,不是按曲线平滑变化
                For $i = 0 To 3
                        $hBrush = _GDIPlus_LineBrushCreate($brushx, $brushy, $endx, $endy, 0xFF00FF00, 0xFFFF0000, $i)
                        If GUICtrlRead($check) = $GUI_CHECKED Then
                                _GDIPlus_LineBrushSetSigmaBlend($hBrush, GUICtrlRead($SigmaBlend), GUICtrlRead($SigmaBlend))
                        EndIf
                        If GUICtrlRead($check) = $GUI_CHECKED Then
                                _GDIPlus_LineBrushSetLinearBlend($hBrush, GUICtrlRead($LinearBlend), GUICtrlRead($LinearBlend))
                        EndIf
                        _GraphicsFillPath($hGraphics, $hPath, $hBrush)
                        _GraphicsDrawPath($hGraphics, $hPath, $hPen)
                        _GDIPlus_BrushDispose($hBrush)
                        _GraphicsDrawString($hGraphics, "渐变" & $i, $rectx, $recty + $recth + 10)
                        _GDIPlus_GraphicsTranslateTransform($hGraphics, $rectx + $rectw + 10, 0)
                Next
                _GDIPlus_PathDispose($hPath)
                _GDIPlus_PenDispose($hPen)
                ;================================在左下角绘制原画刷样子开始===========================
                ;复位画布
                _GDIPlus_GraphicsResetTransform($hGraphics)
                _GDIPlus_GraphicsTranslateTransform($hGraphics, 4, $recty + $recth + 20)
                $hPath = _GDIPlus_PathCreate()
                _GDIPlus_PathAddRectangle($hPath, $brushx, $brushy, 49, 49)
                $hBrush = _GDIPlus_LineBrushCreate($brushx, $brushy, $endx, $endy, 0xFF00FF00, 0xFFFF0000)
                _GraphicsFillPath($hGraphics, $hPath, $hBrush)
                _GDIPlus_BrushDispose($hBrush)
                _GDIPlus_PathDispose($hPath)
                ;================================绘制原画刷样子结束===========================
        EndIf
EndFunc   ;==>lineBrushComp

;_GDIPlus_GraphicsDrawString这个函数,我认为他没有设置$hBrush,所以我改成这样就可以用不同的画刷了
Func _GraphicsDrawString($hGraphics, $sString, $nX, $nY, $hBrush = 0, $sFont = "Arial", $nSize = 10, $iFormat = 0)
        Local $hFormat = _GDIPlus_StringFormatCreate($iFormat)
        Local $hFamily = _GDIPlus_FontFamilyCreate($sFont)
        Local $hFont = _GDIPlus_FontCreate($hFamily, $nSize)
        Local $tLayout = _GDIPlus_RectFCreate($nX, $nY, 0, 0)
        Local $aInfo = _GDIPlus_GraphicsMeasureString($hGraphics, $sString, $hFont, $tLayout, $hFormat)
        __GDIPlus_BrushDefCreate($hBrush)
        Local $aResult = _GDIPlus_GraphicsDrawStringEx($hGraphics, $sString, $hFont, $aInfo, $hFormat, $hBrush)
        Local $iError = @error
        __GDIPlus_BrushDefDispose()
        _GDIPlus_FontDispose($hFont)
        _GDIPlus_FontFamilyDispose($hFamily)
        _GDIPlus_StringFormatDispose($hFormat)
        Return SetError($iError, 0, $aResult)
EndFunc   ;==>_GraphicsDrawString

;下面这两个描路径和填充路径,在3.3.9.5中已经更正了,我用的是3.3.7.15画笔和画刷设置不对,可以改成这样的就行了
Func _GraphicsDrawPath($hGraphics, $hPath, $hPen = 0)
        Local $iTmpErr, $iTmpExt, $aResult
        __GDIPlus_PenDefCreate($hPen)
        $aResult = DllCall($ghGDIPDll, "uint", "GdipDrawPath", "hwnd", $hGraphics, "hwnd", $hPen, "hwnd", $hPath)
        $iTmpErr = @error
        $iTmpExt = @extended
        __GDIPlus_PenDefDispose()
        If $iTmpErr Then Return SetError($iTmpErr, $iTmpExt, False)
        $GDIP_STATUS = $aResult
        Return $aResult = 0
EndFunc   ;==>_GraphicsDrawPath

Func _GraphicsFillPath($hGraphics, $hPath, $hBrush = 0)
        Local $iTmpErr, $iTmpExt, $aResult
        __GDIPlus_BrushDefCreate($hBrush)
        $aResult = DllCall($ghGDIPDll, "uint", "GdipFillPath", "hwnd", $hGraphics, "hwnd", $hBrush, "hwnd", $hPath)
        $iTmpErr = @error
        $iTmpExt = @extended
        __GDIPlus_BrushDefDispose()
        If $iTmpErr Then Return SetError($iTmpErr, $iTmpExt, False)
        $GDIP_STATUS = $aResult
        Return $aResult = 0
EndFunc   ;==>_GraphicsFillPath

sniperone 发表于 2013-5-29 16:01:14

占个沙发~ 收藏学习~

jirunyang 发表于 2013-5-29 19:38:10

s大写的很细致啦。真费心了。好人啊。
页: [1]
查看完整版本: 第十讲之分解2-多色渐变画刷