Option ExplicitPrivate Const CF_BITMAP = 2 Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, lpInitData As Any) As Long 'lpDriverName --- String,用vbNullString传递null值给该参数,除非:1、用DISPLAY,是获取整个屏幕的设备场景;2、用WINSPOOL,则是访问打印驱动 ' lpDeviceName --- String,所用专门设备的名称。该名由打印管理器分配显示 ' lpOutput ------- String,用vbNullString传递null值给该参数 ' lpInitData ----- DEVMODE,这个结构保存初始值。用CreateDCBynum传递0(NULL)值则适用默认设置Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long 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 Private Declare Function GetDesktopWindow Lib "user32" () As Long Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function EmptyClipboard Lib "user32" () As Long Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long Private Declare Function CloseClipboard Lib "user32" () As Long Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long '获得前台窗口的句柄。这里的“前台窗口”是指前台应用程序的活动窗口 Private Declare Function GetForegroundWindow Lib "user32" _ () As Long '获取指定窗口的设备场景 Private Declare Function GetDC Lib "user32" _ (ByVal hwnd As Long) As LongPublic Sub ScrnCap(Left As Long, Top As Long, Right As Long, Bottom As Long) DoEvents Dim rWidth As Long Dim rHeight As Long Dim SourceDC As Long Dim DestDC As Long Dim BHandle As Long Dim Wnd As Long Dim DHandle As Long rWidth = Right - Left rHeight = Bottom - Top SourceDC = CreateDC("DISPLAY", 0, 0, 0) '取得全屏幕 SourceDC = GetDC(GetForegroundWindow()) '取得当前窗口 DestDC = CreateCompatibleDC(SourceDC) BHandle = CreateCompatibleBitmap(SourceDC, rWidth, rHeight) SelectObject DestDC, BHandle BitBlt DestDC, 0, 0, rWidth, rHeight, SourceDC, Left, Top, vbSrcCopy Wnd = GetDesktopWindow
DeleteDC DestDC ReleaseDC DHandle, SourceDC End SubOption Explicit Private Sub Form_Load() Me.ScaleMode = 3 ScrnCap 0, 0, Me.Width, Me.Height End SubPrivate Sub Timer1_Timer() Picture1.Cls Set Picture1.Picture = Clipboard.GetData(vbCFBitmap) ScrnCap 0, 0, Me.Width, Me.Height End Sub
Option Explicit 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 Private Const SRCCOPY = &HCC0020 '抓取窗体上固定区域,左上角座标(X,Y),宽度W,高度H '并保存到文件中 Private Const X As Long = 10 Private Const Y As Long = 10 Private Const W As Long = 200 Private Const H As Long = 200 Private Sub Command1_Click() Picture1.Width = W Picture1.Height = H '实际保存宽度为W-4,高度为H-4 BitBlt Picture1.hDC, 0, 0, W - 4, H - 4, Me.hDC, X, Y, SRCCOPY SavePicture Picture1.Image, "E:\chenjl1031.bmp" End SubPrivate Sub Form_Load() Me.ScaleMode = 3 Picture1.ScaleMode = 3 Picture1.AutoRedraw = True End Sub
FORM1就是抓取的对像。就是自己抓自己。能实现吗?
Picture1.AutoRedraw = True
Picture1.PaintPicture Me.Image, 500, 0, 500, 500
SavePicture Picture1.Image, "d:\1111.bmp"
End Sub
而是FORM1上的一部份区域。这个区域里面的内容是变化的。
Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, lpInitData As Any) As Long
'lpDriverName --- String,用vbNullString传递null值给该参数,除非:1、用DISPLAY,是获取整个屏幕的设备场景;2、用WINSPOOL,则是访问打印驱动
' lpDeviceName --- String,所用专门设备的名称。该名由打印管理器分配显示
' lpOutput ------- String,用vbNullString传递null值给该参数
' lpInitData ----- DEVMODE,这个结构保存初始值。用CreateDCBynum传递0(NULL)值则适用默认设置Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
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
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
'获得前台窗口的句柄。这里的“前台窗口”是指前台应用程序的活动窗口
Private Declare Function GetForegroundWindow Lib "user32" _
() As Long
'获取指定窗口的设备场景
Private Declare Function GetDC Lib "user32" _
(ByVal hwnd As Long) As LongPublic Sub ScrnCap(Left As Long, Top As Long, Right As Long, Bottom As Long)
DoEvents
Dim rWidth As Long
Dim rHeight As Long
Dim SourceDC As Long
Dim DestDC As Long
Dim BHandle As Long
Dim Wnd As Long
Dim DHandle As Long
rWidth = Right - Left
rHeight = Bottom - Top
SourceDC = CreateDC("DISPLAY", 0, 0, 0) '取得全屏幕
SourceDC = GetDC(GetForegroundWindow()) '取得当前窗口
DestDC = CreateCompatibleDC(SourceDC)
BHandle = CreateCompatibleBitmap(SourceDC, rWidth, rHeight)
SelectObject DestDC, BHandle
BitBlt DestDC, 0, 0, rWidth, rHeight, SourceDC, Left, Top, vbSrcCopy
Wnd = GetDesktopWindow
OpenClipboard Wnd
EmptyClipboard
SetClipboardData CF_BITMAP, BHandle
CloseClipboard
DeleteDC DestDC
ReleaseDC DHandle, SourceDC
End SubOption Explicit
Private Sub Form_Load()
Me.ScaleMode = 3
ScrnCap 0, 0, Me.Width, Me.Height
End SubPrivate Sub Timer1_Timer()
Picture1.Cls
Set Picture1.Picture = Clipboard.GetData(vbCFBitmap)
ScrnCap 0, 0, Me.Width, Me.Height
End Sub
你给出的好像不是的吧
其中FORM1就是程序自己本身。
这样的程序能实现吗?
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
Private Const SRCCOPY = &HCC0020
'抓取窗体上固定区域,左上角座标(X,Y),宽度W,高度H
'并保存到文件中
Private Const X As Long = 10
Private Const Y As Long = 10
Private Const W As Long = 200
Private Const H As Long = 200
Private Sub Command1_Click()
Picture1.Width = W
Picture1.Height = H
'实际保存宽度为W-4,高度为H-4
BitBlt Picture1.hDC, 0, 0, W - 4, H - 4, Me.hDC, X, Y, SRCCOPY
SavePicture Picture1.Image, "E:\chenjl1031.bmp"
End SubPrivate Sub Form_Load()
Me.ScaleMode = 3
Picture1.ScaleMode = 3
Picture1.AutoRedraw = True
End Sub