我编写这个 powershell 脚本的目的是创建一个 GUI,我可以在其中看到我的 android 屏幕投射到我的计算机上。
该脚本创建了一个表单,其中计时器应使用从 adb 中提取的图像更新图片框。该脚本还确保安装了 adb。
我的问题是图片框中的图像没有更新,它只是加载捕获的第一张图像。
我相信这是 adb 没有覆盖现有 png 文件的问题,但我不确定,它可能会安静可能是别的东西。
我会很感激任何帮助!
Set-ExecutionPolicy Bypass -Scope Process -Force
function startTimer() {
$timer.start()
Write-Host "Timer Started"
}
function stopTimer() {
$timer.Enabled = $false
Write-Host "Timer Stopped"
$btnStart.Text = "Continue"
}
function TimerTick()
{
adb shell rm -f /sdcard/sc.png
adb shell screencap -p /sdcard/sc.png
adb pull /sdcard/sc.png $BpathTo
$image = [System.Drawing.Image]::Fromfile("$BpathTo\sc.png")
$picb.Image = $image
$lblLog.Text = (Get-Date).ToString()
Write-Host "Refreshed at: "(Get-Date).ToString()
Write-Host ""
}
if (!(Get-Command adb -ErrorAction SilentlyContinue)){
if (!(Get-Command choco -ErrorAction SilentlyContinue)){
Write-Host "Downloading Chocolatey Installer to setup ADB (required)"
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
}
Write-Host " Installing ADB..."
choco install adb --force -y
}
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000
$timer.add_tick({TimerTick})
$BpathTo = (Get-Item -Path ".\").FullName
if ([System.IO.File]::Exists("$BpathTo\sc.png")){
Remove-Item -Path "$BpathTo\sc.png"
}
adb shell screencap -p /sdcard/sc.png
adb pull /sdcard/sc.png $BpathTo
$image = [drawing.image]::FromFile("$BpathTo\sc.png")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "ADB Screencast"
$objForm.Size = New-Object Drawing.Size @(($image.Width+20),($image.Height+85))
$objForm.StartPosition = "CenterScreen"
$objForm.Add_KeyDown({
if ($_.KeyCode -eq "Escape")
{
$objForm.Close()
}
})
$objForm.MinimizeBox=$false
$objForm.MaximizeBox=$false
$objForm.ShowIcon=$false
$objForm.ShowInTaskbar=$false
$halfH=(($image.Width)/2)
$btnStart = New-Object System.Windows.Forms.Button
$btnStart.Location = New-Object System.Drawing.Size(1,$image.Height)
$btnStart.Size = New-Object System.Drawing.Size($halfH,25)
$btnStart.Text = "Start"
$btnStart.Add_Click({StartTimer; })
$objForm.Controls.Add($btnStart)
$btnStop = New-Object System.Windows.Forms.Button
$btnStop.Location = New-Object System.Drawing.Size($halfH,$image.Height)
$btnStop.Size = New-Object System.Drawing.Size($halfH,25)
$btnStop.Text = "Stop"
$btnStop.Add_Click({StopTimer; })
$objForm.Controls.Add($btnStop)
$btnStop.Enabled = $true
$lblLog = New-Object System.Windows.Forms.Label
$lblLog.Location = New-Object System.Drawing.Size(10,($image.Height+25))
$lblLog.Size = New-Object System.Drawing.Size($image.Width,20)
$lblLog.Text = "Refresh Info:"
$objForm.Controls.Add($lblLog)
$picb = New-Object Windows.Forms.PictureBox
$picb.size = New-Object Drawing.Size @($image.Width,$image.Height)
$picb.Location = New-Object Drawing.Point 1,1
$picb.Image = $image
$objForm.Controls.Add($picb)
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()