第十讲之分解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[4], $LinearBlend[4], $check[3], $aIp[8]
init()
GUIRegisterMsg($WM_HSCROLL, "onHSCROLL")
GUISetState()
update()
While 1
$Msg = GUIGetMsg()
Switch $Msg
Case -3
ExitLoop
Case $check[0], $check[1], $check[2]
update()
EndSwitch
WEnd
GUIDelete()
Exit
Func init()
$check[0] = GUICtrlCreateRadio("SigmaBlend nFocus", 5, 205)
GUICtrlSetState(-1, $GUI_CHECKED)
$SigmaBlend[0] = GUICtrlCreateSlider(130, 205, 100)
GUICtrlSetLimit(-1, 100, 0)
GUICtrlSetData(-1, 0)
$SigmaBlend[1] = GUICtrlCreateLabel("", 235, 205, 40, 25)
GUICtrlSetData(-1, StringFormat("%.2f", 0))
GUICtrlCreateLabel("nScale", 295, 205)
$SigmaBlend[2] = GUICtrlCreateSlider(350, 205, 100)
GUICtrlSetLimit(-1, 100, 0)
GUICtrlSetData(-1, 100)
$SigmaBlend[3] = GUICtrlCreateLabel("", 455, 205, 40, 25)
GUICtrlSetData(-1, StringFormat("%.2f", 1))
$check[1] = GUICtrlCreateRadio("LinearBlend nFocus", 5, 235)
$LinearBlend[0] = GUICtrlCreateSlider(130, 235, 100)
GUICtrlSetLimit(-1, 100, 0)
GUICtrlSetData(-1, 0)
$LinearBlend[1] = GUICtrlCreateLabel("", 235, 235, 40, 25)
GUICtrlSetData(-1, StringFormat("%.2f", 0))
GUICtrlCreateLabel("nScale", 295, 235)
$LinearBlend[2] = GUICtrlCreateSlider(350, 235, 100)
GUICtrlSetLimit(-1, 100, 0)
GUICtrlSetData(-1, 100)
$LinearBlend[3] = GUICtrlCreateLabel("", 455, 235, 40, 25)
GUICtrlSetData(-1, StringFormat("%.2f", 1))
$check[2] = GUICtrlCreateRadio("多色渐变", 5, 265)
GUICtrlCreateLabel("颜色1位置", 75, 295)
$aIp[0] = GUICtrlCreateSlider(130, 295, 100)
GUICtrlSetLimit(-1, 100, 0)
GUICtrlSetData(-1, 0)
$aIp[1] = GUICtrlCreateLabel("", 235, 295, 40, 25)
GUICtrlSetData(-1, StringFormat("%.2f", 0))
GUICtrlCreateLabel("颜色2位置", 295, 295)
$aIp[2] = GUICtrlCreateSlider(350, 295, 100)
GUICtrlSetLimit(-1, 100, 0)
GUICtrlSetData(-1, 30)
$aIp[3] = GUICtrlCreateLabel("", 455, 295, 40, 25)
GUICtrlSetData(-1, StringFormat("%.2f", 0.3))
GUICtrlCreateLabel("颜色3位置", 75, 325)
$aIp[4] = GUICtrlCreateSlider(130, 325, 100)
GUICtrlSetLimit(-1, 100, 0)
GUICtrlSetData(-1, 70)
$aIp[5] = GUICtrlCreateLabel("", 235, 325, 40, 25)
GUICtrlSetData(-1, StringFormat("%.2f", 0.7))
GUICtrlCreateLabel("颜色4位置", 295, 325)
$aIp[6] = GUICtrlCreateSlider(350, 325, 100)
GUICtrlSetLimit(-1, 100, 0)
GUICtrlSetData(-1, 100)
$aIp[7] = GUICtrlCreateLabel("", 455, 325, 40, 25)
GUICtrlSetData(-1, StringFormat("%.2f", 1))
EndFunc ;==>init
Func onHSCROLL($hWnd, $iMsg, $wParam, $lParam)
Switch $lParam
Case GUICtrlGetHandle($SigmaBlend[0])
$ls = GUICtrlRead($SigmaBlend[0])
GUICtrlSetData($SigmaBlend[1], StringFormat("%.2f", $ls / 100))
Case GUICtrlGetHandle($SigmaBlend[2])
$ls = GUICtrlRead($SigmaBlend[2])
GUICtrlSetData($SigmaBlend[3], StringFormat("%.2f", $ls / 100))
Case GUICtrlGetHandle($LinearBlend[0])
$ls = GUICtrlRead($LinearBlend[0])
GUICtrlSetData($LinearBlend[1], StringFormat("%.2f", $ls / 100))
Case GUICtrlGetHandle($LinearBlend[2])
$ls = GUICtrlRead($LinearBlend[2])
GUICtrlSetData($LinearBlend[3], StringFormat("%.2f", $ls / 100))
Case GUICtrlGetHandle($aIp[0])
$ls = GUICtrlRead($aIp[0])
GUICtrlSetData($aIp[1], StringFormat("%.2f", $ls / 100))
Case GUICtrlGetHandle($aIp[2])
$ls = GUICtrlRead($aIp[2])
GUICtrlSetData($aIp[3], StringFormat("%.2f", $ls / 100))
Case GUICtrlGetHandle($aIp[4])
$ls = GUICtrlRead($aIp[4])
GUICtrlSetData($aIp[5], StringFormat("%.2f", $ls / 100))
Case GUICtrlGetHandle($aIp[6])
$ls = GUICtrlRead($aIp[6])
GUICtrlSetData($aIp[7], 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[2]) = $GUI_CHECKED Then
For $i = 0 To 3
$hBrush = _GDIPlus_LineBrushCreate($brushx, $brushy, $endx, $endy, 0xFF00FF00, 0xFFFF0000, $i)
; 定义渐变刷颜色和位置
Local $aInterpolations[5][2]
$aInterpolations[0][0] = 4; 使用4个要素和位置
$aInterpolations[1][0] = 0xFFFF0000; 红色
$aInterpolations[1][1] = GUICtrlRead($aIp[1]); 从左侧0%
$aInterpolations[2][0] = 0xFFFFFF00; 黄色
$aInterpolations[2][1] = GUICtrlRead($aIp[3]); 从左侧30%
$aInterpolations[3][0] = 0xFF00FFFF; 青色
$aInterpolations[3][1] = GUICtrlRead($aIp[5]); 从左侧70%
$aInterpolations[4][0] = 0xFFFF00FF; 紫色
$aInterpolations[4][1] = GUICtrlRead($aIp[7]); 从左侧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[0]) = $GUI_CHECKED Then
_GDIPlus_LineBrushSetSigmaBlend($hBrush, GUICtrlRead($SigmaBlend[1]), GUICtrlRead($SigmaBlend[3]))
EndIf
If GUICtrlRead($check[1]) = $GUI_CHECKED Then
_GDIPlus_LineBrushSetLinearBlend($hBrush, GUICtrlRead($LinearBlend[1]), GUICtrlRead($LinearBlend[3]))
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[0], $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[0]
Return $aResult[0] = 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[0]
Return $aResult[0] = 0
EndFunc ;==>_GraphicsFillPath
|