【问题标题】:take a screenshot of whole scrollable wpf listview in powershell?在powershell中截取整个可滚动wpf listview的屏幕截图?
【发布时间】:2019-11-12 22:37:34
【问题描述】:

我有一个带有自定义图标和图像的 Powershell WPF ListView。即使列表视图内容由于很多行而可滚动,也想截取整个列表视图的屏幕截图(以捕获图标/图像)。

使用这样的代码,我只能截取特定区域(此处为 1000x900 像素)的屏幕截图。

Add-Type -Assembly System.Drawing
function Get-Screenshot
{
 param([System.Drawing.Rectangle]$Bereich)
 $Bmp = New-Object System.Drawing.Bitmap $Bereich.Width, $Bereich.Height
 $Graphics = [System.Drawing.Graphics]::FromImage($Bmp)
 $Graphics.CopyFromScreen($Bereich.Location, [System.Drawing.Point]::Empty,$Bereich.Size)
 $BmpPath = "$env:userprofile\documents\Screenshot_{0:dd}{0:MM}_{0:HH}_{0:mm}_{0:ss}.png" -f (Get-Date)
 $Bmp.Save($BmpPath)
 $Graphics.Dispose()
 $Bmp.Dispose()
}
$Bereich = [System.Drawing.Rectangle]::FromLTRB(0, 0, 1000, 900)
Get-Screenshot -Bereich $Bereich 

找到一些类似的文章Take whole "Screenshot" of a scrollable View 但是没有“仅powershell”方式的代码。

【问题讨论】:

  • 另一种方法:我将列表视图转换为 xml,然后我可以添加一些自定义 css 类,最后将其作为 html 导出

标签: .net wpf powershell listview screenshot


【解决方案1】:

另一种方法:我将 listview 转换为 xml,然后我可以添加 css 类,最后导出为 html:

# HTML formatting
$head = @"
<style>
BODY{background-color:white;}
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;foreground-color: black;background-color: LightBlue}
TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;foreground-color: black;background-color: white}
.status1{width:80px;height:18px;background-image: url('C:/Programdata/ZPTInfo/Icons/status1.gif');background-repeat:no-repeat;background-size:80px 18px;}
.status3{width:80px;height:18px;background-image: url('C:/Programdata/ZPTInfo/Icons/status3.gif');background-repeat:no-repeat;background-size:80px 18px;}
.status8{width:80px;height:18px;background-image: url('C:/Programdata/ZPTInfo/Icons/status8.gif');background-repeat:no-repeat;background-size:80px 18px;}
.status9{width:80px;height:18px;background-image: url('C:/Programdata/ZPTInfo/Icons/status9.gif');background-repeat:no-repeat;background-size:80px 18px;}
.status81{width:80px;height:18px;background-image: url('C:/Programdata/ZPTInfo/Icons/status81.gif');background-repeat:no-repeat;background-size:80px 18px;}
.status91{width:80px;height:18px;background-image: url('C:/Programdata/ZPTInfo/Icons/status91.gif');background-repeat:no-repeat;background-size:80px 18px;}
</style>
"@

$filedate=Get-Date -UFormat '%m-%d-%Y-%H%M%S'
$filename = "C:\Temp\export_" + $filedate + ".htm"
#$filename = "C:\Temp\export_.htm"

# Convert the selection to an XML object
[xml]$xmlObject = $WPFergebnisse.items | select-Object Ebene,Typ,Pos,Identnr,BENr,Bez,AGKurz,Menge,Status,letzterm,einkaufskz,zusatz | ConvertTo-Html -Fragment

# Parse XML object and set colour class according to value in 4th last column
for($i=1;$i -le $xmlObject.table.tr.count-1;$i++) {
    switch($xmlObject.table.tr[$i].td[-4] -as [int]){
        1 { $xmlObject.table.tr[$i].ChildNodes[($xmlObject.table.tr[$i].ChildNodes.Count-4)].SetAttribute('class','status1') }
        3 { $xmlObject.table.tr[$i].ChildNodes[($xmlObject.table.tr[$i].ChildNodes.Count-4)].SetAttribute('class','status3') }
        8 { $xmlObject.table.tr[$i].ChildNodes[($xmlObject.table.tr[$i].ChildNodes.Count-4)].SetAttribute('class','status8') }
        9 { $xmlObject.table.tr[$i].ChildNodes[($xmlObject.table.tr[$i].ChildNodes.Count-4)].SetAttribute('class','status9') }
        81 { $xmlObject.table.tr[$i].ChildNodes[($xmlObject.table.tr[$i].ChildNodes.Count-4)].SetAttribute('class','status81') }
        91 { $xmlObject.table.tr[$i].ChildNodes[($xmlObject.table.tr[$i].ChildNodes.Count-4)].SetAttribute('class','status91') }
    } 
}

# Define body and append the modified XML object
$body = @"
<H5>Generiert am:  $filedate</H5>
$($xmlObject.innerxml)
"@

# Convert to HTML and save the file
ConvertTo-Html -Head $head -Body $body | Out-File $filename

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    • 2018-05-17
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多