VB程序员博客
这是我截取的一段桌面抓屏的程序片段
Private Sub frmCapture_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Me.DrawStyle = DOT
'Me.DrawWidth = 2
'Me.ForeColor = &HFF0000
DrawBox = True
Dim DeskhWnd, DeskDC As Integer
Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
DeskhWnd = modGraphics.GetDesktopWindow()
DeskDC = GetDC(DeskhWnd)
BitBlt(Me.hDC, 0, 0, VB6.PixelsToTwipsX(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width), VB6.PixelsToTwipsY(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height), DeskDC, 0, 0, SRCCOPY)
Me.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None
Me.BackgroundImage = Me.Image
UpFlag = False
blnCapturing = True
Cursor = System.Windows.Forms.Cursors.Cross
Timer1.Enabled = True
End Sub
我用的是VS2005,但不知道为什么在“Me.DrawStyle = DOT”和“BitBlt(Me.hDC, 0, 0, VB6.PixelsToTwipsX(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width), VB6.PixelsToTwipsY(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height), DeskDC, 0, 0, SRCCOPY)”处,“Me.DrawStyle = DOT”和“Me.hDC”老是报错,提示为“DrawStyle”,"hDC",“Image”不是Project的成员.这是为什么?还请各位大虾原来小弟的幼稚
Const SRCCOPY =&HCC0020 '你没加这个宣告
这个 SRCCOPY 你可以改为 vbSrcCopy 不必宣告常量
VS2005 我没用过, 下面代码你试试看, 能不能用也请告知, 顺便我也可以得到经验
'抓取桌面 代码 1 添加 Command1
Option Explicit
Private Declare Function GetDC Lib "user32" (ByVal hwnd 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 ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Sub Command1_Click()
Dim H&
Me.AutoRedraw = True
Me.Hide
DoEvents
H = GetDC(0)
BitBlt Me.hdc, 0, 0, Screen.Width / 15, Screen.Height / 15, H, 0, 0, vbSrcCopy
ReleaseDC 0, H
SavePicture Me.Image, "c: t.bmp"
Me.Show
End Sub
'*****************************************************
'抓取桌面 代码 2 添加 Command1 Picture1
'模拟按下Print Screen
Option Explicit
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Sub Form_Load()
Me.AutoRedraw = False
Picture1.AutoRedraw = True
End Sub
Private Sub Command1_Click()
Me.Move Screen.Width
DoEvents
Call keybd_event(vbKeySnapshot, 0, 0, 0)
DoEvents
Picture1.Picture = Clipboard.GetData(vbCFBitmap)
SavePicture Picture1.Image, "C:TT.BMP"
Me.Move (Screen.Width - Me.Width) 2, (Screen.Height - Me.Height) 2
MsgBox "抓屏完成!"
End Sub