[教程] 第十一讲 GDI+画笔之补充 画笔的缩放和旋转
ACN论坛.讲师:seniors 转载请说明此文出处所在:<<简易工作室>>,谢谢!
今天又研究了这个画笔的缩放和旋转,百思不得其解,突然发现画笔最后一个参数是指宽度的单位,
会不会是单位不对,缩放不了结果,研究发现,画笔的单位一定要用0也就是通用单位,而非物理
单位,这样才能缩放和旋转放上代码共享之
#include <APIConstants.au3>
#include <WinAPIEx.au3>
#include <GDIPlus.au3>
#include <GDIPlusEx.au3>
#include <GuiSlider.au3>
Global $ScaleX = 5, $ScaleY = 1, $nAngle = 0
GUICreate("第十一讲 GDI+画笔补充缩放旋转", 300, 300)
$nCtrlId = GUICtrlCreatePic("", 0, 0, 300, 200)
$hPicWnd = GUICtrlGetHandle($nCtrlId)
Global $rect[3][2]
init()
GUIRegisterMsg($WM_HSCROLL, "onHSCROLL")
GUISetState()
update()
While 1
$Msg = GUIGetMsg()
Switch $Msg
Case -3
ExitLoop
EndSwitch
WEnd
GUIDelete()
Exit
Func init()
GUICtrlCreateLabel("水平缩放", 5, 205)
$rect[0][0] = GUICtrlCreateSlider(60, 205, 100)
GUICtrlSetLimit(-1, 10, 1)
GUICtrlSetData(-1, $ScaleX)
$rect[0][1] = GUICtrlCreateLabel("", 170, 205, 40, 25)
GUICtrlSetData(-1, $ScaleX)
GUICtrlCreateLabel("垂直绽放", 5, 235)
$rect[1][0] = GUICtrlCreateSlider(60, 235, 100)
GUICtrlSetLimit(-1, 10, 1)
GUICtrlSetData(-1, $ScaleY)
$rect[1][1] = GUICtrlCreateLabel("", 170, 235, 40, 25)
GUICtrlSetData(-1, $ScaleY)
GUICtrlCreateLabel("旋转角度", 5, 265)
$rect[2][0] = GUICtrlCreateSlider(60, 265, 100)
GUICtrlSetLimit(-1, 360, 0)
GUICtrlSetData(-1, $nAngle)
_GUICtrlSlider_SetTicFreq(-1, 60)
$rect[2][1] = GUICtrlCreateLabel("", 170, 265, 40, 25)
GUICtrlSetData(-1, $nAngle)
EndFunc ;==>init
Func onHSCROLL($hWnd, $iMsg, $wParam, $lParam)
Switch _WinAPI_GetDlgCtrlID($lParam)
Case $rect[0][0]
$ScaleX = GUICtrlRead($rect[0][0])
GUICtrlSetData($rect[0][1], $ScaleX)
Case $rect[1][0]
$ScaleY = GUICtrlRead($rect[1][0])
GUICtrlSetData($rect[1][1], $ScaleY)
Case $rect[2][0]
$nAngle = GUICtrlRead($rect[2][0])
GUICtrlSetData($rect[2][1], $nAngle)
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抗距齿
PenScale($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 PenScale($hGraphics)
Local $hPen = _GDIPlus_PenCreate(0xFFFF0000, 2, 0);建立画笔,最后一个参数0是指通用单位,只有通用单位才能缩放和旋转,不能用2像素
Local $hBrush = _GDIPlus_LineBrushCreate(0, 0, 20, 20, 0xFFFFFF00, 0xFFFF0000, 1)
Local $hPen2 = _GDIPlus_PenCreate2($hBrush, 2, 0);建立通用单位的渐变画笔
_GDIPlus_BrushDispose($hBrush)
;画笔缩放的旋转
_GDIPlus_PenScaleTransform($hPen, $ScaleX, $ScaleY, 1)
_GDIPlus_PenRotateTransform($hPen, $nAngle, 1)
_GDIPlus_PenScaleTransform($hPen2, $ScaleX, $ScaleY, 1)
_GDIPlus_PenRotateTransform($hPen2, $nAngle, 1)
;绘制矩形和椭圆看效果
_GDIPlus_GraphicsDrawRect($hGraphics, 10, 10, 100, 80, $hPen)
_GDIPlus_GraphicsTranslateTransform($hGraphics, 0, 100)
_GDIPlus_GraphicsDrawEllipse($hGraphics, 10, 10, 100, 80, $hPen)
_GDIPlus_GraphicsResetTransform($hGraphics)
_GDIPlus_GraphicsTranslateTransform($hGraphics, 150, 0)
_GDIPlus_GraphicsDrawRect($hGraphics, 10, 10, 100, 80, $hPen2)
_GDIPlus_GraphicsTranslateTransform($hGraphics, 0, 100)
_GDIPlus_GraphicsDrawEllipse($hGraphics, 10, 10, 100, 80, $hPen2)
;释放画笔
_GDIPlus_PenDispose($hPen)
_GDIPlus_PenDispose($hPen2)
EndFunc ;==>PenScale
```
本文固定链接: http://jianyiit.com/post-59.html
发表评论
木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!