找回密码
 加入
搜索
查看: 16000|回复: 10

[交流] 第十二讲 GDI+路径

[复制链接]
发表于 2013-6-4 20:55:55 | 显示全部楼层 |阅读模式
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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×

评分

参与人数 6威望 +5 金钱 +220 贡献 +43 收起 理由
xyhqqaa + 10 学习学习
楼上风云 + 30 先谢过了。
xms77 + 50 + 11
lpxx + 50 + 17
afan + 5 +

查看全部评分

发表于 2013-6-4 21:11:49 | 显示全部楼层
seniors兄辛苦了~
 楼主| 发表于 2013-6-4 21:22:06 | 显示全部楼层
回复 2# pusofalse
对自己来说也是学习,没事的
只是我自己不喜欢写注释,特别是讲过的,除非复制过来的,一般没有注释
发表于 2013-6-4 22:06:32 | 显示全部楼层
感谢分享,学习了
发表于 2013-6-4 22:34:49 | 显示全部楼层
感谢S大无私的奉献!
发表于 2013-6-5 07:19:45 | 显示全部楼层
seniors哥们研究的够深的呀
发表于 2013-6-5 08:50:03 | 显示全部楼层
回复 1# seniors

seniors兄辛苦了。
先收藏了,这几天比较忙,稍后按您的指点作个尝试。先谢过了。
发表于 2013-6-5 09:29:41 | 显示全部楼层
搬个板凳,占个座,收藏慢慢学习~
发表于 2013-6-6 13:21:52 | 显示全部楼层
慢慢看慢慢学
发表于 2014-11-5 15:01:12 | 显示全部楼层
佩服,佩服!











广州市顺捷风机有限公司网站:www.020sjfj.com  电话:13922754875 周生
发表于 2017-8-28 21:47:50 | 显示全部楼层
感谢楼主,学习学习
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-3-29 03:59 , Processed in 0.085602 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表