seniors 发表于 2013-6-4 20:55:55

第十二讲 GDI+路径

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
        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], ($iI - 1) * 2 + 1)
                DllStructSetData($tPoints, 1, $aPoints[$iI], ($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
        Return $aResult = 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, "Width"), $height = DllStructGetData($aInfo, "Height")
        $hPath = _GDIPlus_PathCreate()
        _GDIPlus_PathAddString($hPath, "平行四边形", $tLayout, $hFamily, 0, 30)
        Local $aPoints = [, _ ;4个端点
                        , _         ;x1, y1
                        [$width - 60, 0], _         ;x2, y2
                        , _         ;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, "Width")
        $height = DllStructGetData($aInfo, "Height")
        $hPath = _GDIPlus_PathCreate()
        _GDIPlus_PathAddString($hPath, "三角形变换", $tLayout, $hFamily, 0, 30)
        Local $bPoints = [, _ ;4个端点
                        , _         ;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);获取路径的点和类型的数组
        ;是点数, - 点1的X位置, - 点1的Y位置,是点1的类型
        If @error Then Return SetError(@error, @extended, 0)
        ;以下只是修改了点的Y坐标
        For $i = 1 To $PathData
                $x = $PathData[$i]
                $y = $PathData[$i]
                $y += Sin($x / 20) * 10
                $PathData[$i] = $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
                $x = $PathData[$i]
                $y = $PathData[$i]
                $yy = $y + (30 - $y) * ($x - 250) * ($x - 250) / 100000
                If $yy > 30 Then $yy = 30
                $PathData[$i] = $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, $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

pusofalse 发表于 2013-6-4 21:11:49

seniors兄辛苦了~

seniors 发表于 2013-6-4 21:22:06

回复 2# pusofalse
对自己来说也是学习,没事的
只是我自己不喜欢写注释,特别是讲过的,除非复制过来的,一般没有注释

lpxx 发表于 2013-6-4 22:06:32

感谢分享,学习了

xms77 发表于 2013-6-4 22:34:49

感谢S大无私的奉献!

netegg 发表于 2013-6-5 07:19:45

seniors哥们研究的够深的呀

楼上风云 发表于 2013-6-5 08:50:03

回复 1# seniors

seniors兄辛苦了。
先收藏了,这几天比较忙,稍后按您的指点作个尝试。先谢过了。

sniperone 发表于 2013-6-5 09:29:41

搬个板凳,占个座,收藏慢慢学习~

ndyndy 发表于 2013-6-6 13:21:52

慢慢看慢慢学

广州市顺捷风机 发表于 2014-11-5 15:01:12

佩服,佩服!











广州市顺捷风机有限公司网站:www.020sjfj.com电话:13922754875 周生

Rmuto 发表于 2017-8-28 21:47:50

感谢楼主,学习学习
页: [1]
查看完整版本: 第十二讲 GDI+路径