Office VBA也能任性截图(屏幕任意区域截图)

25 篇文章 2 订阅
1 篇文章 0 订阅

Excel是否可以实现屏幕任意区域截图?听起来似乎有些不务正业,已经有那么多截图软件,并且微信、QQ等也都提供了截图功能,但是存在的就是合理的,借助强大的PowerShell就可以实现任意区域截图功能。

示例代码如下。

Sub ScreenShot(strFile, pos_x, pos_y, image_width, image_height)
    Dim strArgs As String
    Dim strPSCmd As String
    strArgs = Join(Array("'" & strFile & "'", pos_x, pos_y, image_width, image_height))
    strPSCmd = "function SSRegion($strFile, $pos_x, $pos_y, $image_width, $image_height) {" & _
            "Add-Type -AssemblyName System.Drawing;" & _
            "$bitmap = [System.Drawing.Bitmap]::new($image_width, $image_height);" & _
            "$graphics = [System.Drawing.Graphics]::FromImage($bitmap);" & _
            "$graphics.CopyFromScreen([System.Drawing.Point]::new($pos_x, $pos_y), [System.Drawing.Point]::new(0, 0), $bitmap.Size);" & _
            "$graphics.Dispose();" & _
            "$bitmap.Save($strFile);" & _
            "$bitmap.Dispose();}; SSRegion "
    Shell "powershell -Command " & strPSCmd & strArgs
End Sub

【代码解析】
第4行代码将参数组合为一个空格分隔的字符串。
第5~10行代码为PowerShell命令。
第11行代码调用Shell执行PowerShell命令。
第14行代码调用ScreenShot过程完成截屏,截屏区域区域左上角屏幕坐标为(100,100),截图区域宽度和高度均为300像素。

运行Demo过程,悄无声息完成截图,是不是有点儿酷!

Sub Demo()
    ScreenShot "C:\Temp\SquareScrShot.png", 100, 100, 300, 300
End Sub

更新代码,截图保存为硬盘文件,并且拷贝到Windows系统剪贴板

Sub ScreenShot2(strFile, pos_x, pos_y, image_width, image_height)
    Dim strArgs As String
    Dim strPSCmd As String
    strArgs = Join(Array("'" & strFile & "'", pos_x, pos_y, image_width, image_height))
    strPSCmd = "function SSRegion($strFile, $pos_x, $pos_y, $image_width, $image_height) {" & _
            "Add-Type -AssemblyName System.Drawing; " & _
            "Add-Type -AssemblyName System.Windows.Forms; " & _
            "$bitmap = [System.Drawing.Bitmap]::new($image_width, $image_height); " & _
            "$graphics = [System.Drawing.Graphics]::FromImage($bitmap); " & _
            "$graphics.CopyFromScreen([System.Drawing.Point]::new($pos_x, $pos_y), [System.Drawing.Point]::new(0, 0), $bitmap.Size); " & _
            "[System.Windows.Forms.Clipboard]::SetImage($bitmap); " & _
            "$graphics.Dispose(); " & _
            "$bitmap.Save($strFile); " & _
            "$bitmap.Dispose();}; SSRegion "
    Shell "powershell -Command " & strPSCmd & strArgs
End Sub

代码仅为功能演示,并未考虑截图区域是否会超出屏幕分辨率等细节问题,大家可以自行完善。

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
VBA中想要截取屏幕指定的区域,可以通过API函数来实现: 1. 首先需要导入Windows API库: Private Declare PtrSafe Function BitBlt Lib "GDI32" Alias "BitBlt" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long Private Declare PtrSafe Function GetDC Lib "user32" Alias "GetDC" (ByVal hWnd As Long) As Long Private Declare PtrSafe Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hWnd As Long, ByVal hdc As Long) As Long 2. 接下来,需要使用以下代码来获取屏幕截图并将其保存到文件中: Public Sub ScreenShot() Dim hwnd As Long Dim hdcSrc As Long Dim hdcDest As Long Dim nWidth As Long Dim nHeight As Long Dim nLeft As Long Dim nTop As Long Dim lRet As Long '获取当前窗口句柄 hwnd = GetDesktopWindow() '获取屏幕DC hdcSrc = GetDC(hwnd) hdcDest = CreateCompatibleDC(hdcSrc) nWidth = 500 '截取的宽度 nHeight = 500 '截取的高度 nLeft = 500 '截取的左边距 nTop = 500 '截取的上边距 '创建一个设备兼容的DC Dim BMP As Byte ReDim BMP(nWidth * nHeight * 4) BMP(0) = &HFF BMP(1) = &HFF BMP(2) = &HFF BMP(3) = &H0 lRet = BitBlt(hdcDest, 0, 0, nWidth, nHeight, hdcSrc, nLeft, nTop, vbSrcCopy) 'ReleaseDC Call ReleaseDC(hwnd, hdcSrc) Call DeleteDC(hdcDest) 'Save to file Open "C:\myimage.bmp" For Output As #1 Put #1, , BMP Close #1 End Sub 3. 在执行完以上代码后,将会在C盘根目录下生成一个名为“myimage.bmp”的文件,其中就保存了屏幕截图的指定区域。 需要注意的是,由于屏幕分辨率不同,如果要在不同的屏幕上使用截图功能,可能需要根据实际情况进行一些参数调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值