[APAC]手动截取当前活动窗口,并且按规则命名(1/2)

Function Take-ScreenShot {
    <#
.SYNOPSIS
    Used to take a screenshot of the desktop or the active window.
.DESCRIPTION
    Used to take a screenshot of the desktop or the active window and save to an image file if needed.
.PARAMETER screen
    Screenshot of the entire screen
.PARAMETER activewindow
    Screenshot of the active window
.PARAMETER file
    Name of the file to save as. Default is image.bmp
.PARAMETER imagetype
    Type of image being saved. Can use JPEG,BMP,PNG. Default is bitmap(bmp)
.PARAMETER print
    Sends the screenshot directly to your default printer
.INPUTS
.OUTPUTS
.NOTES
    Name: Take-ScreenShot
    Author: Boe Prox
    DateCreated: 07/25/2010
.EXAMPLE
    Take-ScreenShot -activewindow
    Takes a screen shot of the active window
.EXAMPLE
    Take-ScreenShot -Screen
    Takes a screenshot of the entire desktop
.EXAMPLE
    Take-ScreenShot -activewindow -file "C:\image.bmp" -imagetype bmp
    Takes a screenshot of the active window and saves the file named image.bmp with the image being bitmap
.EXAMPLE
    Take-ScreenShot -screen -file "C:\image.png" -imagetype png
    Takes a screenshot of the entire desktop and saves the file named image.png with the image being png
.EXAMPLE
    Take-ScreenShot -Screen -print
    Takes a screenshot of the entire desktop and sends to a printer
.EXAMPLE
    Take-ScreenShot -ActiveWindow -print
    Takes a screenshot of the active window and sends to a printer
#>
#Requires -Version 2
        [cmdletbinding(
                SupportsShouldProcess = $True,
                DefaultParameterSetName = "screen",
                ConfirmImpact = "low"
        )]
Param (
       [Parameter(
            Mandatory = $False,
            ParameterSetName = "screen",
            ValueFromPipeline = $True)]
            [switch]$screen,
       [Parameter(
            Mandatory = $False,
            ParameterSetName = "window",
            ValueFromPipeline = $False)]
            [switch]$activewindow,
       [Parameter(
            Mandatory = $False,
            ParameterSetName = "",
            ValueFromPipeline = $False)]
            [string]$file,
       [Parameter(
            Mandatory = $False,
            ParameterSetName = "",
            ValueFromPipeline = $False)]
            [string]
            [ValidateSet("bmp","jpeg","png")]
            $imagetype = "bmp",
       [Parameter(
            Mandatory = $False,
            ParameterSetName = "",
            ValueFromPipeline = $False)]
            [switch]$print

)
# C# code
$code = @‘
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
namespace ScreenShotDemo
{
  /// <summary>
  /// Provides functions to capture the entire screen, or a particular window, and save it to a file.
  /// </summary>
  public class ScreenCapture
  {
    /// <summary>
    /// Creates an Image object containing a screen shot the active window
    /// </summary>
    /// <returns></returns>
    public Image CaptureActiveWindow()
    {
      return CaptureWindow( User32.GetForegroundWindow() );
    }
    /// <summary>
    /// Creates an Image object containing a screen shot of the entire desktop
    /// </summary>
    /// <returns></returns>
    public Image CaptureScreen()
    {
      return CaptureWindow( User32.GetDesktopWindow() );
    }
    /// <summary>
    /// Creates an Image object containing a screen shot of a specific window
    /// </summary>
    /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
    /// <returns></returns>
    private Image CaptureWindow(IntPtr handle)
    {
      // get te hDC of the target window
      IntPtr hdcSrc = User32.GetWindowDC(handle);
      // get the size
      User32.RECT windowRect = new User32.RECT();
      User32.GetWindowRect(handle,ref windowRect);
      int width = windowRect.right - windowRect.left;
      int height = windowRect.bottom - windowRect.top;
      // create a device context we can copy to
      IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
      // create a bitmap we can copy it to,
      // using GetDeviceCaps to get the width/height
      IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,width,height);
      // select the bitmap object
      IntPtr hOld = GDI32.SelectObject(hdcDest,hBitmap);
      // bitblt over
      GDI32.BitBlt(hdcDest,0,0,width,height,hdcSrc,0,0,GDI32.SRCCOPY);
      // restore selection
      GDI32.SelectObject(hdcDest,hOld);
      // clean up
      GDI32.DeleteDC(hdcDest);
      User32.ReleaseDC(handle,hdcSrc);
      // get a .NET image object for it
      Image img = Image.FromHbitmap(hBitmap);
      // free up the Bitmap object
      GDI32.DeleteObject(hBitmap);
      return img;
    }
    /// <summary>
    /// Captures a screen shot of the active window, and saves it to a file
    /// </summary>
    /// <param name="filename"></param>
    /// <param name="format"></param>
    public void CaptureActiveWindowToFile(string filename, ImageFormat format)
    {
      Image img = CaptureActiveWindow();
      img.Save(filename,format);
    }
    /// <summary>
    /// Captures a screen shot of the entire desktop, and saves it to a file
    /// </summary>
    /// <param name="filename"></param>
    /// <param name="format"></param>
    public void CaptureScreenToFile(string filename, ImageFormat format)
    {
      Image img = CaptureScreen();
      img.Save(filename,format);
    }

    /// <summary>
    /// Helper class containing Gdi32 API functions
    /// </summary>
    private class GDI32
    {

      public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
      [DllImport("gdi32.dll")]
      public static extern bool BitBlt(IntPtr hObject,int nXDest,int nYDest,
        int nWidth,int nHeight,IntPtr hObjectSource,
        int nXSrc,int nYSrc,int dwRop);
      [DllImport("gdi32.dll")]
      public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC,int nWidth,
        int nHeight);
      [DllImport("gdi32.dll")]
      public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
      [DllImport("gdi32.dll")]
      public static extern bool DeleteDC(IntPtr hDC);
      [DllImport("gdi32.dll")]
      public static extern bool DeleteObject(IntPtr hObject);
      [DllImport("gdi32.dll")]
      public static extern IntPtr SelectObject(IntPtr hDC,IntPtr hObject);
    }

    /// <summary>
    /// Helper class containing User32 API functions
    /// </summary>
    private class User32
    {
      [StructLayout(LayoutKind.Sequential)]
      public struct RECT
      {
        public int left;
        public int top;
        public int right;
        public int bottom;
      }
      [DllImport("user32.dll")]
      public static extern IntPtr GetDesktopWindow();
      [DllImport("user32.dll")]
      public static extern IntPtr GetWindowDC(IntPtr hWnd);
      [DllImport("user32.dll")]
      public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDC);
      [DllImport("user32.dll")]
      public static extern IntPtr GetWindowRect(IntPtr hWnd,ref RECT rect);
      [DllImport("user32.dll")]
      public static extern IntPtr GetForegroundWindow();
    }
  }
}
‘@
#User Add-Type to import the code
add-type $code -ReferencedAssemblies ‘System.Windows.Forms‘,‘System.Drawing‘
#Create the object for the Function
$capture = New-Object ScreenShotDemo.ScreenCapture

#Take screenshot of the entire screen
If ($Screen) {
    Write-Verbose "Taking screenshot of entire desktop"
    #Save to a file
    If ($file) {
        If ($file -eq "") {
            $file = "$pwd\image.bmp"
            }
        Write-Verbose "Creating screen file: $file with imagetype of $imagetype"
        $capture.CaptureScreenToFile($file,$imagetype)
        }
    ElseIf ($print) {
        $img = $Capture.CaptureScreen()
        $pd = New-Object System.Drawing.Printing.PrintDocument
        $pd.Add_PrintPage({$_.Graphics.DrawImage(([System.Drawing.Image]$img), 0, 0)})
        $pd.Print()
        }
    Else {
        $capture.CaptureScreen()
        }
    }
#Take screenshot of the active window
If ($ActiveWindow) {
    Write-Verbose "Taking screenshot of the active window"
    #Save to a file
    If ($file) {
        If ($file -eq "") {
            $file = "$pwd\image.bmp"
            }
        Write-Verbose "Creating activewindow file: $file with imagetype of $imagetype"
        $capture.CaptureActiveWindowToFile($file,$imagetype)
        }
    ElseIf ($print) {
        $img = $Capture.CaptureActiveWindow()
        $pd = New-Object System.Drawing.Printing.PrintDocument
        $pd.Add_PrintPage({$_.Graphics.DrawImage(([System.Drawing.Image]$img), 0, 0)})
        $pd.Print()
        }
    Else {
        $capture.CaptureActiveWindow()
        }
    }
}
#定义文件名(不含后缀名)
$datetime = (Get-Date).Tostring("yyyyMMddhhmmss")
#执行截图
Start-Sleep -Seconds 2
Take-ScreenShot -activewindow -imagetype jpeg -file "C:\screenshot\Backup_$datetime.JPEG"
时间: 2024-10-01 04:25:11

[APAC]手动截取当前活动窗口,并且按规则命名(1/2)的相关文章

[GE]手动截取当前活动窗口,并且按规则命名(1/2)

Function Take-ScreenShot { <# .SYNOPSIS Used to take a screenshot of the desktop or the active window. .DESCRIPTION Used to take a screenshot of the desktop or the active window and save to an image file if needed. .PARAMETER screen Screenshot of the

Ruby操作VBA的注意事项和技巧:乱码、获取VBA活动和非活动窗口的名称与路径、文件路径的智能拼接与截取(写入日期)、宏里调用和控制窗体以及窗体上的控件、不同workbook之间的宏互相调用、

1.VBA编辑器复制粘贴出来的代码乱码     解决方法:切换到中文输入模式再复制出来就行了 2.获取VBA活动和非活动窗口的名称与路径 1 1 Dim wbpath, filename As String 2 2 wbpath = ThisWorkbook.Path ’这个获取的是宏所在的workbook的路径 3 3 'filename = ThisWorkbook.Name '这个是宏所在的workbook的名字,不带路径 4 4 filename = ActiveWindow.Capti

QApplication::alert 如果窗口不是活动窗口,则会向窗口显示一个警告(非常好用,效果就和TeamViewer一样)

void QApplication::alert(QWidget * widget, int msec = 0)如果窗口不是活动窗口,则会向窗口显示一个警告.警报会显示msec 毫秒.如果毫秒为零,闪烁一段时间后会停止,任务栏图标会一直亮着.widget.h #ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QTimer> #include <QTime> namespace Ui { clas

asp.net获取屏幕截图、活动窗口截图

Rectangle R = System.Windows.Forms.Screen.PrimaryScreen.Bounds;//获取活动窗口截图 //Rectangle R = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;//获取整个屏幕截图 System.Drawing.Image img = new Bitmap(R.Width, R.Height); Graphics G = Graphics.FromImage(img);

win8.1系统下,点击一个窗口,【当前活动窗口】该窗口无法置顶

两个或多个窗口同时显示在桌面的时候,点击下一层的窗口,无法置顶显示,无论怎么点击,还是隐藏在原置顶窗口的后面,只能手动把原置顶窗口最小化后,才能看到.例如,A窗口现在置顶,B窗口在A的后面,露出来一部分,我点击这一部分,A窗口的边框变灰,B窗口的边框变蓝,但是B窗口依然隐藏在A窗口后面,只能手动最小化A窗口,才能看到B窗口,然后这个时候,就是B窗口置顶了,接下来,我在在电脑下面的任务栏点击A窗口,A窗口同样无法置顶,还是显示在B窗口的下面,尽管B窗口的边框变成了灰色.

Android中简单活动窗口的切换--Android

本例实现Android中简单Activity窗口切换:借助intent(意图)对应用操作(这里用按钮监听)等的描述,Android根据描述负责找对应的组件,完成组件的调用来实现活动的切换……案例比较简单直接附上代码了哈. 1.建两个Activity类,分别为MainActivity.java和GuideActivity.java…… MainActivity.java(核心文件): package livetelecast.thonlon.example.cn.thonlonliveteleca

Ubuntu 16.04 LTS 初体验 (转载)

一.前言 心血来潮,下载最新的Ubuntu Kylin 16.04尝鲜.但刚装完系统,还是有很多问题需要自己动手解决,这里就是把自己实际遇到的问题总结记录,希望也可以为其他刚接触 Ubuntu 的朋友提供一些帮助, 也欢迎大家补充.交流学习. 二.桌面使用引导 考虑到许多刚接触Ubuntu的朋友,对系统的使用做一些简单的引导. 三.系统设置  3.1 软件中心无法更新? 打开软件和更新面板后,修改下载服务器地址,然后选择其他站点(服务器可以随便自己选择,我选择了mirrors.sohu.com)

Linux课程第一天学习笔记

####################虚拟机控制####################[[email protected] Desktop]$ rht-vmctl start desktop        ##开启desktop虚拟机Error: desktop not started (is already running)            ##报错,desktop已经运行[[email protected] Desktop]$ rht-vmctl view desktop     

Selenium WebDriver 中鼠标和键盘事件分析及扩展(转)

本文将总结 Selenium WebDriver 中的一些鼠标和键盘事件的使用,以及组合键的使用,并且将介绍 WebDriver 中没有实现的键盘事件(Keys 枚举中没有列举的按键)的扩展.举例说明扩展 Alt+PrtSc 组合键来截取当前活动窗口并将剪切板图像保存到文件. 概念 在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和键盘的一些行为.比如使用鼠标单击.双击.右击.拖拽等动作:或者键盘输入.快捷键使用.组合键使用等模拟键盘的操作.在 WebDerive