GDI+路径
1、建立
_GDIPlus_PathCreate($iFillMode)
_GDIPlus_PathCreate2($aPtTypes, $iFillMode)
$iFillMode =0相当于是XOR模式, =1时相当于是OR模式
$aPtTypes是根据点来建立路径
2、路径可以变换
这里介绍一下_GDIPlus_PathWarp($hPath, $hMatrix, $aPoints, $nX, $nY, $nWidth, $nHeight, $iWarpMode = 0, $nFlatness = $FlatnessDefault)
它是指$nX, $nY, $nWidth, $nHeight矩形按 $aPoints规定的开关变形,如果有$hMatrix再按$hMatrix变化
$aPoints必须是3个点或者4个点
注意UDF中这个函数有一个地方错了,把这个If $iCount <> 3 Or $iCount <> 4 Then
改成If $iCount <> 3 And $iCount <> 4 Then
就行了
Func _GDIPlus_PathWarp($hPath, $hMatrix, $aPoints, $nX, $nY, $nWidth, $nHeight, $iWarpMode = 0, $nFlatness = $FlatnessDefault)
Local $iI, $iCount, $pPoints, $tPoints, $aResult
$iCount = $aPoints[0][0]
If $iCount <> 3 And $iCount <> 4 Then
$GDIP_ERROR = 1
Return False
EndIf
$tPoints = DllStructCreate("float[" & $iCount * 2 & "]")
$pPoints = DllStructGetPtr($tPoints)
For $iI = 1 To $iCount
DllStructSetData($tPoints, 1, $aPoints[$iI][0], ($iI - 1) * 2 + 1)
DllStructSetData($tPoints, 1, $aPoints[$iI][1], ($iI - 1) * 2 + 2)
Next
$aResult = DllCall($ghGDIPDll, "uint", "GdipWarpPath", "hwnd", $hPath, "hwnd", $hMatrix, "ptr", $pPoints, "int", $iCount, "float", $nX, "float", $nY, "float", $nWidth, "float", $nHeight, "int", $iWarpMode, "float", $nFlatness)
If @error Then Return SetError(@error, @extended, False)
$GDIP_STATUS = $aResult[0]
Return $aResult[0] = 0
EndFunc ;==>_GDIPlus_PathWarp
另外例子中还有路径点的读取及点坐标更改
楼上风云的写字笔顺问题,我想说下,文字的路径不是一笔一笔的,他是一块连通区域是一个路径,而且也不是按书写顺序的
如果你要自己研究的话,可以看下_GDIPlus_PathIterNextSubpathPath
#include <APIConstants.au3>
#include <WinAPIEx.au3>
#include <GDIPlus.au3>
#include <GDIPlusEx.au3>
GUICreate("第十二讲 GDI+路径", 500, 200)
$nCtrlId = GUICtrlCreatePic("", 0, 0, 500, 200)
$hPicWnd = GUICtrlGetHandle($nCtrlId)
GUISetState()
update()
While 1
$Msg = GUIGetMsg()
Switch $Msg
Case -3
ExitLoop
EndSwitch
WEnd
GUIDelete()
Exit
Func update()
Local $HWND_CX = _WinAPI_GetWindowWidth($hPicWnd)
Local $HWND_CY = _WinAPI_GetWindowHeight($hPicWnd)
_GDIPlus_Startup()
$hGraphics = _GDIPlus_GraphicsCreateFromHWND($hPicWnd)
_GDIPlus_GraphicsSetSmoothingMode($hGraphics, 2)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics($HWND_CX, $HWND_CY, $hGraphics)
$hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsClear($hBackbuffer, 0xFFECE9D8)
_GDIPlus_GraphicsSetSmoothingMode($hBackbuffer, 2);光滑模式,2为8*8抗距齿
fillmodeComp($hBackbuffer)
PathString($hBackbuffer)
_GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, $HWND_CX, $HWND_CY)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_GraphicsDispose($hBackbuffer)
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_Shutdown()
EndFunc ;==>update
Func PathString($hGraphics)
_GDIPlus_GraphicsResetTransform($hGraphics)
Local $hBrush = _GDIPlus_BrushCreateSolid(0xFFFF0000)
Local $hFamily = _GDIPlus_FontFamilyCreate("Arial")
Local $hFont = _GDIPlus_FontCreate($hFamily, 30, 0)
Local $tLayout = _GDIPlus_RectFCreate(0, 0)
Local $hPath = _GDIPlus_PathCreate()
_GDIPlus_PathAddString($hPath, "正常文字", $tLayout, $hFamily, 0, 30)
_GDIPlus_GraphicsTranslateTransform($hGraphics, 0, 60)
_GraphicsFillPath($hGraphics, $hPath, $hBrush)
_GDIPlus_PathDispose($hPath)
Local $aInfo = _GDIPlus_GraphicsMeasureString($hGraphics, "平行四边形", $hFont, $tLayout, 0)
Local $width = DllStructGetData($aInfo[0], "Width"), $height = DllStructGetData($aInfo[0], "Height")
$hPath = _GDIPlus_PathCreate()
_GDIPlus_PathAddString($hPath, "平行四边形", $tLayout, $hFamily, 0, 30)
Local $aPoints[5][2] = [[4, 0], _ ;4个端点
[30, 0], _ ;x1, y1
[$width - 60, 0], _ ;x2, y2
[0, $height], _ ;x3, y3
[$width, $height]] ;x4, y4
_GDIPlus_PathWarp($hPath, 0, $aPoints, 0, 0, $width, $height)
_GDIPlus_GraphicsTranslateTransform($hGraphics, 150, 0)
_GraphicsFillPath($hGraphics, $hPath, $hBrush)
_GDIPlus_PathDispose($hPath)
$aInfo = _GDIPlus_GraphicsMeasureString($hGraphics, "三角形变换", $hFont, $tLayout, 0)
$width = DllStructGetData($aInfo[0], "Width")
$height = DllStructGetData($aInfo[0], "Height")
$hPath = _GDIPlus_PathCreate()
_GDIPlus_PathAddString($hPath, "三角形变换", $tLayout, $hFamily, 0, 30)
Local $bPoints[4][2] = [[3, 0], _ ;4个端点
[0, 0], _ ;x1, y1
[$width, 0], _ ;x2, y2
[$width / 2, $height]] ;x3, y3
_GDIPlus_PathWarp($hPath, 0, $bPoints, 0, 0, $width, $height)
_GDIPlus_GraphicsTranslateTransform($hGraphics, 150, 0)
_GraphicsFillPath($hGraphics, $hPath, $hBrush)
_GDIPlus_PathDispose($hPath)
_GDIPlus_BrushDispose($hBrush)
$hBrush = _GDIPlus_LineBrushCreate(0, 0, 0, 30, 0xFFFF0000, 0xFFFF6633, 1)
_GDIPlus_GraphicsResetTransform($hGraphics)
$hPath = _GDIPlus_PathCreate()
_GDIPlus_PathAddString($hPath, "正弦函数曲线字符测试字符串", $tLayout, $hFamily, 0, 30)
$hPath = sinPath($hPath)
_GDIPlus_GraphicsTranslateTransform($hGraphics, 0, 100)
_GraphicsFillPath($hGraphics, $hPath, $hBrush)
_GDIPlus_PathDispose($hPath)
$hPath = _GDIPlus_PathCreate()
_GDIPlus_PathAddString($hPath, "抛物线函数曲线字符测试字符串", $tLayout, $hFamily, 0, 30)
$hPath = parabolaPath($hPath)
_GDIPlus_GraphicsTranslateTransform($hGraphics, 0, 40)
_GraphicsFillPath($hGraphics, $hPath, $hBrush)
_GDIPlus_PathDispose($hPath)
_GDIPlus_BrushDispose($hBrush)
_GDIPlus_FontDispose($hFont)
_GDIPlus_FontFamilyDispose($hFamily)
EndFunc ;==>PathString
;填充模式对比
Func fillmodeComp($hGraphics)
Local $hPen = _GDIPlus_PenCreate(0xFFFF0000, 1)
Local $hBrush = _GDIPlus_BrushCreateSolid(0xFFFF0000)
;填充模式0
Local $hPath = _GDIPlus_PathCreate()
_GDIPlus_PathAddEllipse($hPath, 5, 5, 50, 50)
_GDIPlus_PathAddEllipse($hPath, 30, 5, 50, 50)
_GDIPlus_PathAddEllipse($hPath, 55, 5, 50, 50)
_GDIPlus_PathAddEllipse($hPath, 80, 5, 50, 50)
_GraphicsDrawPath($hGraphics, $hPath, $hPen)
_GDIPlus_GraphicsTranslateTransform($hGraphics, 100, 0)
_GraphicsFillPath($hGraphics, $hPath, $hBrush)
_GDIPlus_PathDispose($hPath)
_GDIPlus_GraphicsTranslateTransform($hGraphics, 150, 0)
;填充模式1
$hPath = _GDIPlus_PathCreate(1)
_GDIPlus_PathAddEllipse($hPath, 5, 5, 50, 50)
_GDIPlus_PathAddEllipse($hPath, 30, 5, 50, 50)
_GDIPlus_PathAddEllipse($hPath, 55, 5, 50, 50)
_GDIPlus_PathAddEllipse($hPath, 80, 5, 50, 50)
_GraphicsDrawPath($hGraphics, $hPath, $hPen)
_GDIPlus_GraphicsTranslateTransform($hGraphics, 100, 0)
_GraphicsFillPath($hGraphics, $hPath, $hBrush)
_GDIPlus_PathDispose($hPath)
_GDIPlus_BrushDispose($hBrush)
_GDIPlus_PenDispose($hPen)
EndFunc ;==>fillmodeComp
Func sinPath($hPath)
Local $retPath, $x, $y
Local $PathData = _GDIPlus_PathGetData($hPath);获取路径的点和类型的数组
;[0][0]是点数,[1][0] - 点1的X位置, [1][1] - 点1的Y位置,[1][2]是点1的类型
If @error Then Return SetError(@error, @extended, 0)
;以下只是修改了点的Y坐标
For $i = 1 To $PathData[0][0]
$x = $PathData[$i][0]
$y = $PathData[$i][1]
$y += Sin($x / 20) * 10
$PathData[$i][1] = $y
Next
$retPath = _GDIPlus_PathCreate2($PathData);根据点建立路径
Return $retPath
EndFunc ;==>sinPath
Func parabolaPath($hPath)
Local $retPath, $x, $y
Local $PathData = _GDIPlus_PathGetData($hPath)
If @error Then Return SetError(@error, @extended, 0)
For $i = 1 To $PathData[0][0]
$x = $PathData[$i][0]
$y = $PathData[$i][1]
$yy = $y + (30 - $y) * ($x - 250) * ($x - 250) / 100000
If $yy > 30 Then $yy = 30
$PathData[$i][1] = $yy
Next
$retPath = _GDIPlus_PathCreate2($PathData)
Return $retPath
EndFunc ;==>parabolaPath
;_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
|