ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 1691|回复: 0

[求助] VBA后台窗口截图代码,请帮完善下

[复制链接]

TA的精华主题

TA的得分主题

发表于 2018-9-13 13:28 | 显示全部楼层 |阅读模式
本帖最后由 shofing 于 2018-9-13 13:58 编辑

网上找到了个VBA后台窗口截图代码,测试没有成功,请高手请帮完善下,感激


360截图18750817226065.jpg


引用地址 http://wolfccb.com/?p=296

以下是代码
  1. Function ScreenDump(ByVal hWnd As Long, Optional ByVal croptop As Integer = 0, Optional ByVal cropbottom As Integer = 0, Optional ByVal cropleft As Integer = 0, Optional ByVal cropright As Integer = 0) As String
  2.     'Dump the screen of specified form and save to clipboard.
  3.     'hWnd is the handle of target form, croptop/cropbottom/cropleft/cropright are pixels to crop, 0 by default means no crop.
  4.     'Return string message
  5.    
  6.     Dim UserFormHwnd As Long, DeskHwnd As Long
  7.     Dim hdc As Long
  8.     Dim hdcMem, hdcMemc As Long
  9.     Dim hBitmap, hBitmapc As Long
  10.     Dim rect As RECT_Type
  11.     Dim retval As Long
  12.     Dim fwidth As Long, fheight As Long
  13.     Dim isCrop As Boolean

  14.     If croptop < 0 Or cropbottom < 0 Or cropleft < 0 Or cropright < 0 Then
  15.         ScreenDump = "0-Wrong parameter"
  16.         Exit Function
  17.     End If
  18.    
  19.     If croptop > 0 Or cropbottom > 0 Or cropleft > 0 Or cropright > 0 Then
  20.         isCrop = True
  21.     End If
  22.    
  23.     ' Get window handle
  24.     DeskHwnd = GetDesktopWindow()
  25.     UserFormHwnd = hWnd
  26.    
  27.     ' Get screen coordinates
  28.     Call GetWindowRect(UserFormHwnd, rect)

  29.     fwidth = rect.right - rect.left
  30.     fheight = rect.bottom - rect.top

  31.     ' Get the device context of Desktop and allocate memory
  32.     hdc = GetDC(DeskHwnd)
  33.     hdcMem = CreateCompatibleDC(hdc)
  34.     hBitmap = CreateCompatibleBitmap(hdc, fwidth, fheight)

  35.     If hBitmap <> 0 Then
  36.         
  37.         retval = SelectObject(hdcMem, hBitmap)

  38.         'Redraw window before capture
  39.         RedrawWindow UserFormHwnd, ByVal 0, ByVal 0, RDW_INVALIDATE Or RDW_ALLCHILDREN Or RDW_UPDATENOW
  40.         
  41.         ' Copy bitmap to memory location
  42.         
  43.         retval = PrintWindow(UserFormHwnd, hdcMem, 0)

  44.         'Crop image
  45.         If isCrop = True Then
  46.             
  47.             fwidth = fwidth - cropleft - cropright
  48.             fheight = fheight - croptop - cropbottom
  49.             
  50.             'Allocate memory for cropped image
  51.             hdcMemc = CreateCompatibleDC(hdc)
  52.             hBitmapc = CreateCompatibleBitmap(hdc, fwidth, fheight)
  53.             retval = SelectObject(hdcMemc, hBitmapc)
  54.             
  55.             'Crop
  56.             retval = BitBlt(hdcMemc, 0, 0, fwidth, fheight, hdcMem, cropleft, croptop, SRCCOPY)

  57.             'Set up the Clipboard and copy bitmap
  58.             retval = OpenClipboard(DeskHwnd)
  59.             retval = EmptyClipboard()
  60.             retval = SetClipboardData(CF_BITMAP, hBitmapc)
  61.             retval = CloseClipboard()
  62.             
  63.             'Clean up
  64.             retval = DeleteDC(hdcMemc)
  65.             retval = DeleteObject(hBitmapc)
  66.             
  67.         Else
  68.             
  69.             'Set up the Clipboard and copy bitmap
  70.             retval = OpenClipboard(DeskHwnd)
  71.             retval = EmptyClipboard()
  72.             retval = SetClipboardData(CF_BITMAP, hBitmap)
  73.             retval = CloseClipboard()
  74.             
  75.         End If

  76.         ScreenDump = "1-Success"
  77.         
  78.     Else
  79.    
  80.         ScreenDump = "2-No bitmap"
  81.    
  82.     End If

  83.     ' Clean up
  84.     retval = DeleteDC(hdcMem)
  85.     retval = ReleaseDC(DeskHwnd, hdc)
  86.     retval = DeleteObject(hBitmap)

  87. End Function

复制代码


您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2025-1-15 07:52 , Processed in 0.019489 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表