> ???撰寫於文章 <3SbP3g$vLl@bbs.ncnu.edu.tw>...
> 請問有誰曾寫過截取螢幕的程式,不是模擬 Print Screen 鍵喔!!
'執行本範例請於表單布置 Picture1,按下 CTRL+ALT+S 則抓圖存檔。
Option Explicit
'利用繪圖物件的 DC 做圖像移轉的函數
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
'取得繪圖物件 DC 的函數
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
'釋放繪圖物件 DC 的函數
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, _
ByVal hdc As Long) As Long
'圖像截取之長度及寬度
Dim sx As Single, sy As Single
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim ScreenDC As Long
'按下 CTRL+ALT+S 則抓圖存檔
If Shift = vbCtrlMask + vbAltMask And KeyCode = vbKeyS Then
'把表單關掉以截取視窗圖
Me.Visible = False
'強迫視窗關閉再進行下一步
DoEvents
'代碼 0 代表 Windows 的桌面畫面
ScreenDC = GetDC(0)
With Picture1
.AutoRedraw = True
BitBlt Picture1.hdc, 0, 0, sx, sy, ScreenDC, 0, 0, vbSrcCopy
SavePicture .Image, App.Path & "\Test.bmp"
.AutoRedraw = False
End With
'釋放 Windows 桌面畫面的 DC
ReleaseDC 0, ScreenDC
'回復表單為可見
Me.Visible = True
End If
End Sub
Private Sub Form_Load()
Me.KeyPreview = True
'單位換算成 Pixel
With Screen
sx = .Width / .TwipsPerPixelX
sy = .Height / .TwipsPerPixelY
End With
'依 Form 大小調整 Picture1 大小
With Picture1
.Width = Screen.Width + .Width - .ScaleWidth
.Height = Screen.Height + .Height - .ScaleHeight
.Visible = False
End With
End Sub