本帖最后由 seniors 于 2012-11-24 21:43 编辑
回复 27# afan
Afan版版,帮我看下为什么秒针有时会长出来,是不是我的电脑有问题
把尺寸改小看没问题了,看样子尺寸长了,计算量大,有时会出错。
#include <WindowsConstants.au3>
#include <WinAPIEx.au3>
#include <GDIPlusex.au3>
#include <APIConstants.au3>
OnAutoItExitRegister("ExitFunc")
Global $Timer_Second = 1
Global $SecondDelay = 50
Global $hGui = GUICreate("怀表", 201, 201, -1, -1, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))
Global $hSecond = GUICreate("", 201, 201, 0, 0, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_MDICHILD, $WS_EX_TRANSPARENT), $hGui)
_GDIPlus_Startup()
Draw_Watch()
Global $hCallback = DllCallbackRegister("My_winProc", "int", "hWnd;uint;wparam;lparam")
Global $tCallback = DllCallbackGetPtr($hCallback)
Global $CallProc = _WinAPI_SetWindowLong($hGui, -4, $tCallback)
GUISetState(@SW_SHOW, $hGui)
GUISetState(@SW_SHOW, $hSecond)
_WinAPI_SetTimer($hGui, $Timer_Second, $SecondDelay, 0)
Do
Until GUIGetMsg() = -3
Func ExitFunc()
_WinAPI_KillTimer($hGui, $Timer_Second)
_WinAPI_SetWindowLong($hGui, -4, $CallProc)
_GDIPlus_Shutdown()
GUIDelete($hGui)
EndFunc ;==>ExitFunc
Func second()
Local $hPath
Local $hBitmap = _WinAPI_CreateBitmap(201, 201, 1, 32);建立背景位图
Local $hCDC = _WinAPI_CreateCompatibleDC(0);建立绘图DC
Local $hOld = _WinAPI_SelectObject($hCDC, $hBitmap);调入图片到DC
Local $hGraphics = _GDIPlus_GraphicsCreateFromHDC($hCDC);从DC创建图形对象
_GDIPlus_GraphicsClear($hGraphics, 0x00000000);清空图形
_GDIPlus_GraphicsSetSmoothingMode($hGraphics, 2);绘制质量 2 - 使用 8 X 8 矩形过滤器
_GDIPlus_GraphicsSetInterpolationMode($hGraphics, 7);插值法 7=High-quality, bicubic interpolation.
DllCall($ghGDIPDll, "uint", "GdipSetTextRenderingHint", "hwnd", $hGraphics, "int", 4)
;画时针
Local $hPath = _GDIPlus_PathCreate()
_GDIPlus_PathAddLine($hPath, 0, 0, 50, 0)
_GDIPlus_PathAddLine($hPath, 0, 0, -5, 0)
Local $hMatrix = _GDIPlus_MatrixCreate()
Local $hourAng = (@HOUR + @MIN / 60) * 30 - 90
_GDIPlus_MatrixTranslate($hMatrix, 101, 101)
_GDIPlus_MatrixRotate($hMatrix, $hourAng)
_GDIPlus_PathTransform($hPath, $hMatrix)
Local $hPen = _GDIPlus_PenCreate(0xFF0072E3, 3)
_GDIPlus_GraphicsDrawPath($hGraphics, $hPath, $hPen);这个函数里已经注销了$hPen
_GDIPlus_PathDispose($hPath)
;画分针
$hPath = _GDIPlus_PathCreate()
_GDIPlus_PathAddLine($hPath, 0, 0, 60, 0)
_GDIPlus_PathAddLine($hPath, 0, 0, -6, 0)
$hMatrix = _GDIPlus_MatrixCreate()
Local $miniuteAng = (@MIN + @SEC / 60) * 6 - 90
_GDIPlus_MatrixTranslate($hMatrix, 101, 101)
_GDIPlus_MatrixRotate($hMatrix, $miniuteAng)
_GDIPlus_PathTransform($hPath, $hMatrix)
$hPen = _GDIPlus_PenCreate(0xFF0066CC, 2)
_GDIPlus_GraphicsDrawPath($hGraphics, $hPath, $hPen);这个函数里已经注销了$hPen
_GDIPlus_PathDispose($hPath)
;画秒针
$hPath = _GDIPlus_PathCreate()
_GDIPlus_PathAddLine($hPath, 0, 0, 80, 0)
_GDIPlus_PathAddLine($hPath, 0, 0, -8, 0)
$hMatrix = _GDIPlus_MatrixCreate()
Local $secondAng = (@SEC + @MSEC / 1000) * 6 - 90
_GDIPlus_MatrixTranslate($hMatrix, 101, 101)
_GDIPlus_MatrixRotate($hMatrix, $secondAng)
_GDIPlus_PathTransform($hPath, $hMatrix)
$hPen = _GDIPlus_PenCreate(0xFF005AB5, 1)
_GDIPlus_GraphicsDrawPath($hGraphics, $hPath, $hPen);这个函数里已经注销了$hPen
_GDIPlus_PathDispose($hPath)
_GDIPlus_GraphicsDispose($hGraphics)
_WinAPI_SelectObject($hCDC, $hOld)
_WinAPI_DeleteDC($hCDC)
_WinAPI_UpdateLayeredWindowEx($hSecond, -1, -1, $hBitmap, 255, 1)
_WinAPI_DeleteObject($hBitmap)
DllCall('psapi.dll', 'int', 'EmptyWorkingSet', 'hWnd', -1) ;释放内存
EndFunc ;==>second
Func Draw_Watch()
Local $hBitmap = _WinAPI_CreateBitmap(201, 201, 1, 32);建立背景位图
Local $hCDC = _WinAPI_CreateCompatibleDC(0);建立绘图DC
Local $hOld = _WinAPI_SelectObject($hCDC, $hBitmap);调入图片到DC
Local $hGraphics = _GDIPlus_GraphicsCreateFromHDC($hCDC);从DC创建图形对象
_GDIPlus_GraphicsClear($hGraphics, 0x00000000);清空图形
_GDIPlus_GraphicsSetSmoothingMode($hGraphics, 2);绘制质量 2 - 使用 8 X 8 矩形过滤器
_GDIPlus_GraphicsSetInterpolationMode($hGraphics, 7);插值法 7=High-quality, bicubic interpolation.
DllCall($ghGDIPDll, "uint", "GdipSetTextRenderingHint", "hwnd", $hGraphics, "int", 4)
Local $hPath = _GDIPlus_PathCreate()
_GDIPlus_PathAddEllipse($hPath, 0, 0, 200, 200)
Local $hBrush = _GDIPlus_BrushCreateSolid(0x99FFFFFF)
_GDIPlus_GraphicsFillPath($hGraphics, $hPath, $hBrush);这个函数里已经注销了$hBrush
_GDIPlus_PathDispose($hPath)
Local $hPen = _GDIPlus_PenCreate(0xFF005AB5)
$hPath = circlePath(200, 5)
_GDIPlus_GraphicsDrawPath($hGraphics, $hPath, $hPen);这个函数里已经注销了$hPen
_GDIPlus_PathDispose($hPath)
$hBrush = _GDIPlus_BrushCreateSolid(0xFF005AB5)
$hPath = WatchPath(200, 5, 0.5, 6)
_GDIPlus_GraphicsFillPath($hGraphics, $hPath, $hBrush);这个函数里已经注销了$hBrush
_GDIPlus_PathDispose($hPath)
$hBrush = _GDIPlus_BrushCreateSolid(0xFF005AB5)
$hPath = WatchPath(200, 8, 1, 30)
_GDIPlus_GraphicsFillPath($hGraphics, $hPath, $hBrush);这个函数里已经注销了$hBrush
_GDIPlus_PathDispose($hPath)
Local $ColorArray[2] = [0xFF005AB5, 0xFF004696], $ZiArray[2] = [0xFF004696, 0xFF005AB5]
Local $hImage
Local $pi = 3.14159265358979
For $i = 1 To 12
$hImage = _ImageString($i, $ColorArray, 24, 0x00000000)
$hBitmap2 = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
$tSIZE = _WinAPI_GetBitmapDimension($hBitmap2)
_WinAPI_DeleteObject($hBitmap2)
$SX = DllStructGetData($tSIZE, 'X') + 2
$SY = DllStructGetData($tSIZE, 'Y') + 2
$tSIZE = 0
$rad = $i * $pi / 6
$xx = 88 + Sin($rad) * 80
$yy = 86 - Cos($rad) * 80
If $i > 9 Then $xx -= 4
_GDIPlus_GraphicsDrawImageRect($hGraphics, $hImage, $xx, $yy, $SX, $SY)
_WinAPI_DeleteObject($hImage)
Next
$hImage = _ImageString("沙漏", $ZiArray, 16, 0xFFFFFFDF, "黑体")
$hBitmap2 = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
$tSIZE = _WinAPI_GetBitmapDimension($hBitmap2)
_WinAPI_DeleteObject($hBitmap2)
$SX = DllStructGetData($tSIZE, 'X') + 2
$SY = DllStructGetData($tSIZE, 'Y') + 2
$tSIZE = 0
_GDIPlus_GraphicsDrawImageRect($hGraphics, $hImage, 82, 50, $SX, $SY)
_WinAPI_DeleteObject($hImage)
_GDIPlus_GraphicsDispose($hGraphics)
_WinAPI_SelectObject($hCDC, $hOld)
_WinAPI_DeleteDC($hCDC)
_WinAPI_UpdateLayeredWindowEx($hGui, -1, -1, $hBitmap, 255, 1)
_WinAPI_DeleteObject($hBitmap)
DllCall('psapi.dll', 'int', 'EmptyWorkingSet', 'hWnd', -1) ;释放内存
EndFunc ;==>Draw_Watch
Func My_winProc($hWnd, $Msg, $wParam, $lParam)
Switch $Msg
Case 0x0084;WM_NCHITTEST
Return $HTCAPTION
Case 0x00A3;WM_NCLMOUSEDBCLK
Exit
Case 0x0113;$WM_TIMER
Switch $wParam
Case $Timer_Second
second()
EndSwitch
EndSwitch
Return _WinAPI_CallWindowProc($CallProc, $hWnd, $Msg, $wParam, $lParam)
EndFunc ;==>My_winProc
Func ScalePath($R, $Height, $nStartAngle, $nSweepAngle)
Local $hPath = _GDIPlus_PathCreate()
_GDIPlus_PathAddArc($hPath, 0, 0, $R, $R, $nStartAngle, $nSweepAngle)
_GDIPlus_PathAddArc($hPath, $Height, $Height, $R - $Height * 2, $R - $Height * 2, $nStartAngle + $nSweepAngle, -$nSweepAngle)
_GDIPlus_PathCloseFigures($hPath)
Return $hPath
EndFunc ;==>ScalePath
Func circlePath($R, $Height)
Local $hPath = _GDIPlus_PathCreate()
_GDIPlus_PathAddEllipse($hPath, 0, 0, $R, $R)
_GDIPlus_PathAddEllipse($hPath, $Height, $Height, $R - $Height * 2, $R - $Height * 2)
Return $hPath
EndFunc ;==>circlePath
Func WatchPath($R, $Height, $sweepAng, $step)
Local $hPath1 = _GDIPlus_PathCreate()
Local $startAng = -$sweepAng / 2
For $Ang = $startAng To 360 - $sweepAng Step $step
$hPath = ScalePath($R, $Height, $Ang, $sweepAng)
_GDIPlus_PathAddPath($hPath1, $hPath, False)
_GDIPlus_PathDispose($hPath)
Next
Return $hPath1
EndFunc ;==>WatchPath
Func _ImageString($str, $ColorArray, $size = 38, $ColorSide = 0xFF333333, $font = "Arial")
Local $hPath = _GDIPlus_PathCreate()
Local $hFormat = _GDIPlus_StringFormatCreate(0x0004)
_GDIPlus_StringFormatSetAlign($hFormat, 0);0为左对齐,1为居中显示
Local $hFamily = _GDIPlus_FontFamilyCreate($font)
Local $tLayout = _GDIPlus_RectFCreate(0, 0)
_GDIPlus_PathAddString($hPath, $str, $tLayout, $hFamily, 0, $size, $hFormat);读取字符串外形为路径;0是正常字
Local $strXY = _GDIPlus_PathGetWorldBounds($hPath);字符串的区域范围
Local $hdisplayDC = _WinAPI_CreateCompatibleDC(0)
Local $hGraphics = _GDIPlus_GraphicsCreateFromHDC($hdisplayDC)
Local $hImage = _GDIPlus_BitmapCreateFromGraphics($strXY[0] + $strXY[2] + 2, $strXY[1] + $strXY[3] + 2, $hGraphics)
Local $hContext = _GDIPlus_ImageGetGraphicsContext($hImage)
_GDIPlus_GraphicsClear($hContext, 0x00000000)
_GDIPlus_GraphicsSetSmoothingMode($hContext, 2);绘制质量 2 - 使用 8 X 8 矩形过滤器
_GDIPlus_GraphicsSetInterpolationMode($hContext, 7);插值法 7=High-quality, bicubic interpolation.
DllCall($ghGDIPDll, "uint", "GdipSetTextRenderingHint", "hwnd", $hContext, "int", 4)
Local $hPen = _PenInit($ColorSide)
_GDIPlus_PenSetLineJoin($hPen, 2);2 - Line join produces a smooth, circular arc between the lines.
DllCall($ghGDIPDll, "uint", "GdipDrawPath", "hwnd", $hContext, "hwnd", $hPen, "hwnd", $hPath);字体描边
_GDIPlus_PenDispose($hPen)
Local $hBrush = _PenInit($ColorArray, $size)
DllCall($ghGDIPDll, "uint", "GdipFillPath", "hwnd", $hContext, "hwnd", $hBrush, "hwnd", $hPath);填充
_GDIPlus_BrushDispose($hBrush)
_GDIPlus_PathDispose($hPath)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_GraphicsDispose($hContext)
_GDIPlus_GraphicsDispose($hGraphics)
_WinAPI_DeleteDC($hdisplayDC)
Return $hImage
EndFunc ;==>_ImageString
Func _PenInit($ColorArray, $size = Default, $Angle = 90)
Switch UBound($ColorArray)
Case 0
If $size = Default Then $size = 2
Return _GDIPlus_PenCreate($ColorArray, $size, 0)
Case 1
If $size = Default Then $size = 2
Return _GDIPlus_PenCreate($ColorArray[0], $size, 0)
Case 2
If $size = Default Then $size = 38
Local $tRect = _GDIPlus_RectFCreate(0, 0, $size, $size);设置区域色块
Return _GDIPlus_LineBrushCreateFromRect($tRect, $ColorArray[0], $ColorArray[1], 1, 3);1为竖直方向渐变
Case Else
If $size = Default Then $size = 38
Local $tRect = _GDIPlus_RectFCreate(0, 0, $size, $size);设置区域色块
Local $hBrush = _GDIPlus_LineBrushCreateFromRectWithAngle($tRect, 0xFF000000, 0xFFFFFFFF, $Angle, True, 3)
;设置画刷多色渐变数组
Local $aInterpolations[4][2]
Local $colorCount = UBound($ColorArray)
ReDim $aInterpolations[$colorCount + 1][2]
$aInterpolations[0][0] = $colorCount;0,0保存颜色数
For $i = 1 To $colorCount
$aInterpolations[$i][0] = $ColorArray[$i - 1];设置颜色
$aInterpolations[$i][1] = Round(($i - 1) / ($colorCount - 1), 1);设置颜色位置,比如0.1即是离左边10%位置
Next
;设置画刷多色渐变
_GDIPlus_LineBrushSetPresetBlend($hBrush, $aInterpolations)
Return $hBrush
EndSwitch
EndFunc ;==>_PenInit
|