2011年7月4日 星期一

copy the contents of screen or the active window

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const KEYEVENTF_KEYUP = &H2

' Return the current contents of the screen or the active window
'
' It works by simulating the typing of the Print-Screen key
' (and Alt key if ActiveWindow is True), which dumps the screen
' to the clipboard. The original contents of the clipboard is then
' restored, but this action might affect the behavior of other
' applications that are monitoring the clipboard.

Function GetScreenBitmap(Optional ActiveWindow As Boolean) As Picture
' save the current picture in the clipboard, if any
Dim pic As StdPicture
Set pic = Clipboard.GetData(vbCFBitmap)

' Alt-Print Screen captures the active window only
If ActiveWindow Then
' Press the Alt key
keybd_event vbKeyMenu, 0, 0, 0
End If
' Press the Print Screen key
keybd_event vbKeySnapshot, 0, 0, 0
DoEvents

' Release the Print Screen key
keybd_event vbKeySnapshot, 0, KEYEVENTF_KEYUP, 0
If ActiveWindow Then
' Release the Alt key
keybd_event vbKeyMenu, 0, KEYEVENTF_KEYUP, 0
End If
DoEvents

' return the bitmap now in the clipboard
Set GetScreenBitmap = Clipboard.GetData(vbCFBitmap)
' restore the original contents of the clipboard
Clipboard.SetData pic, vbCFBitmap

End Function

 

http://www.devx.com/vb2themax/Tip/18341

 

沒有留言: