[教程] 第十一講 GDI+畫筆之補充 畫筆的縮放和旋轉
陕西11选5链接 www.kbcqhj.com.cn 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
本文固定鏈接: //www.kbcqhj.com.cn/post-59.html
發表評論
木有頭像就木JJ啦!還木有頭像嗎?點這里申請屬于你的個性Gravatar頭像吧!