VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)

VB爱好者乐园(VBGood)

 找回密码
 立即注册
搜索
查看: 399|回复: 2

[求助] VB如何截图指定区域,如环绕光标位置扩展10个单位?

[复制链接]
发表于 2018-2-2 17:28:57 | 显示全部楼层 |阅读模式
如题。下面代码截图的范围为什么会大很多,且一直从屏幕左上角开始截图?
  1. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
  2. Private Declare Function BitBlt Lib "gdi32" (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
  3. Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
  4. Private Declare Function GetDesktopWindow Lib "user32" () As Long
  5. Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
  6. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long '获得鼠标位置的 API
  7. Private Type POINTAPI
  8.     X As Long
  9.     Y As Long
  10. End Type

  11. Private Sub Form_Load()
  12.     Me.AutoRedraw = True
  13. End Sub

  14. Private Sub Command1_Click()
  15.     CopyScreen "C:\Users\user\Desktop\8.bmp"
  16. End Sub

  17. Sub CopyScreen(FileName As String)
  18.     Dim w As Long, h As Long
  19.     Dim hwndSrc As Long
  20.     Dim hSrcDC As Long
  21.     Dim Cur1    As POINTAPI
  22.     Me.Cls
  23.     GetCursorPos Cur1
  24.     hwndSrc = GetDesktopWindow()
  25.     hSrcDC = GetWindowDC(hwndSrc)
  26.     Call BitBlt(hdc, Cur1.X - 10, Cur1.Y - 10, Cur1.X + 10, Cur1.Y + 10, GetDC(0), 0, 0, vbSrcCopy)
  27.     SavePicture Me.Image, FileName
  28.     Call ReleaseDC(hwndSrc, hSrcDC)
  29. End Sub
复制代码
发表于 2018-2-6 09:50:37 | 显示全部楼层
你的bitblt参数写错了   
Call BitBlt(Me.hdc, 0, 0, 20, 20, hSrcDC, Cur1.X - 10, Cur1.Y - 10, vbSrcCopy)
这样才是对的,前两个0,0是在目标区域的位置,20,20是大小,不进行缩放,srcdc后面的才是源区域的位置
不过这个代码在高dpi的屏幕会出问题,截到的东西跟鼠标位置不是对应的,而是有一个比例关系

评分

参与人数 1威望 +8 人气 +1 收起 理由
bruly + 8 + 1 很给力

查看全部评分

回复 支持 1 反对 0

使用道具 举报

发表于 2018-2-6 11:29:15 | 显示全部楼层
BitBlt函数力的源坐标与目标坐标搞反了吧

评分

参与人数 1威望 +5 人气 +1 收起 理由
bruly + 5 + 1 赞一个

查看全部评分

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

文字版|手机版|小黑屋|VBGood  

GMT+8, 2018-2-19 12:21

VB爱好者乐园(VBGood)
快速回复 返回顶部 返回列表