Powershell实现圆缩小放大 (实时刷新窗口)
生活随笔
收集整理的這篇文章主要介紹了
Powershell实现圆缩小放大 (实时刷新窗口)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用Powershell,創建實時刷新的窗口,繪制圖形,這里以圓作為例子,做縮小放大動畫。
【分析】
Powershell是windows內置的自動部署平臺,功能強大在于可以調取.net框架,因此,即使沒有Opengl加持,也可以創建實時刷新的窗口。可以調用windows.Form程序集創建窗口,然后調用System.Drawing程序集來繪制。graphics.FillEllipse可以繪制實心圓,graphics.DrawEllipse可以繪制空心圓。對于實時刷新畫面,可以設置一個定時器,例如每過16.7毫秒觸發一次繪制,即每秒60幀畫面刷新率。每次刷新繪制,圓的大小參數修改并重新繪制。
【實現】
1. 繪制空心圓動畫
# 加載 System.Drawing 和 System.Windows.Forms 程序集
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
# 創建一個新的窗體
$form = New-Object System.Windows.Forms.Form
$form.Text = "圓放大縮小循環動畫"
# 設置窗體大小為 1000x800
$form.Size = New-Object System.Drawing.Size(1000, 800)
$form.StartPosition = "CenterScreen"
# 設置背景顏色為黑色
$form.BackColor = [System.Drawing.Color]::Black
# 啟用雙緩沖以減少閃爍
$doubleBufferProperty = $form.GetType().GetProperty("DoubleBuffered", [System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Instance)
$doubleBufferProperty.SetValue($form, $true, $null)
# 定義圓的初始半徑
$radius = 10
# 定義最大半徑和最小半徑
$maxRadius = [int]([Math]::Min($form.ClientSize.Width, $form.ClientSize.Height) / 2)
$minRadius = 10
# 定義一個標志,用于判斷圓是在放大還是縮小
$isGrowing = $true
# 定義一個定時器
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 16.7 # 每 16.7 毫秒觸發一次
# 定時器的 Tick 事件處理程序
$timer.Add_Tick({
param($sender, $e)
if ($isGrowing) {
# 圓正在放大
$script:radius += 1
if ($script:radius -ge $maxRadius) {
# 達到最大半徑,開始縮小
$script:isGrowing = $false
}
} else {
# 圓正在縮小
$script:radius -= 1
if ($script:radius -le $minRadius) {
# 達到最小半徑,開始放大
$script:isGrowing = $true
}
}
# 使窗體無效,觸發重繪事件
$form.Invalidate()
})
# 窗體大小改變事件處理程序,確保圓心始終在窗口中心
$form.Add_SizeChanged({
$script:maxRadius = [int]([Math]::Min($form.ClientSize.Width, $form.ClientSize.Height) / 2)
$form.Invalidate()
})
# 窗體的 Paint 事件處理程序
$form.Add_Paint({
param($sender, $e)
$graphics = $e.Graphics
# 設置畫筆顏色和寬度,將邊框設置更粗
$pen = New-Object System.Drawing.Pen([System.Drawing.Color]::Red, 5)
# 計算窗口中心位置作為圓心
$centerX = [int]($form.ClientSize.Width / 2)
$centerY = [int]($form.ClientSize.Height / 2)
# 計算圓的矩形邊界
$x = $centerX - $radius
$y = $centerY - $radius
$width = $radius * 2
$height = $radius * 2
# 繪制圓
$graphics.DrawEllipse($pen, $x, $y, $width, $height)
# 釋放畫筆資源
$pen.Dispose()
})
# 啟動定時器
$timer.Start()
# 顯示窗體
$form.ShowDialog()
2.繪制實心圓動畫
# 加載 System.Drawing 和 System.Windows.Forms 程序集
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
# 創建一個新的窗體
$form = New-Object System.Windows.Forms.Form
$form.Text = "圓放大縮小循環動畫"
# 設置窗體大小為 1000x800
$form.Size = New-Object System.Drawing.Size(1000, 800)
$form.StartPosition = "CenterScreen"
# 設置背景顏色為黑色
$form.BackColor = [System.Drawing.Color]::Black
# 通過反射啟用雙緩沖
$doubleBufferProperty = $form.GetType().GetProperty("DoubleBuffered", [System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Instance)
$doubleBufferProperty.SetValue($form, $true, $null)
# 定義圓的初始半徑和圓心
$centerX = [int]($form.ClientSize.Width / 2)
$centerY = [int]($form.ClientSize.Height / 2)
$radius = 10
# 定義最大半徑和最小半徑
$maxRadius = [int]([Math]::Min($form.ClientSize.Width, $form.ClientSize.Height) / 2)
$minRadius = 10
# 定義一個標志,用于判斷圓是在放大還是縮小
$isGrowing = $true
# 定義一個定時器
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 20 # 每 20 毫秒觸發一次
# 定時器的 Tick 事件處理程序
$timer.Add_Tick({
param($sender, $e)
if ($isGrowing) {
# 圓正在放大
$script:radius += 1
if ($script:radius -ge $maxRadius) {
# 達到最大半徑,開始縮小
$script:isGrowing = $false
}
} else {
# 圓正在縮小
$script:radius -= 1
if ($script:radius -le $minRadius) {
# 達到最小半徑,開始放大
$script:isGrowing = $true
}
}
# 使窗體無效,觸發重繪事件
$form.Invalidate()
})
# 窗體的 Paint 事件處理程序
$form.Add_Paint({
param($sender, $e)
$graphics = $e.Graphics
# 設置畫刷顏色,這里設置為紅色
$brush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::Red)
# 計算圓的矩形邊界
$x = $centerX - $radius
$y = $centerY - $radius
$width = $radius * 2
$height = $radius * 2
# 繪制實心圓
$graphics.FillEllipse($brush, $x, $y, $width, $height)
# 釋放畫刷資源
$brush.Dispose()
})
# 啟動定時器
$timer.Start()
# 顯示窗體
$form.ShowDialog()
【補充】
1. 調用 System.Drawing 程序集、 System.Windows.Forms 程序集。
2. 設計定時器,每過多少時間重繪一次畫面。
3. 設計圓動畫邏輯,設置一個開關,圓縮小放大的狀態量。并設定圓大小極值。
4. 通過反射啟用雙緩沖,避免畫面閃爍。
【效果】
總結
以上是生活随笔為你收集整理的Powershell实现圆缩小放大 (实时刷新窗口)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 性能监测与优化命令free
- 下一篇: Python之面向对象继承和派生