我编写此powershell脚本的目的是创建一个GUI,在该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()
答案 0 :(得分:0)
除了没有静态图像,我无法对它进行全面测试,因为我没有android。
但这似乎对我有用:
我将这些行移到了脚本的顶部:
Set-ExecutionPolicy Bypass -Scope Process -Force
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000
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 ""
}
$timer.Add_Tick({TimerTick})
并在最后添加这些:
# clean up
$timer.Stop()
$timer.Dispose()
$objForm.Dispose()
对我来说,这是可行的,但是我也看到了$timer
的范围为$global:timer
...
希望这会有所帮助