Public
Shared
Function cap()
Function cap() As Bitmap
Dim desktopbmp As Bitmap = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
Dim g As Graphics = Graphics.FromImage(desktopbmp)
g.CopyFromScreen(0, 0, 0, 0, _
New Size(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, _
System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height))
Dim rect As Rectangle = New Rectangle(Cursor.Position.X - Cursor.Current.HotSpot.X, _
Cursor.Position.Y - Cursor.Current.HotSpot.Y, _
Cursor.Current.Size.Width, Cursor.Current.Size.Height)
Cursor.Current.Draw(g, rect)
Return desktopbmp
End Function
Win32Stuff.vb
Imports
System
Imports
System.Collections.Generic
Imports
System.Text
Imports
System.Runtime.InteropServices


Namespace ScreenshotCaptureWithMouse
Namespace ScreenshotCaptureWithMouse.ScreenCapture
Class Win32StuffClass Win32Stuff

Class Variables#Region "Class Variables"
Public Const SM_CXSCREEN As Integer = 0
Public Const SM_CYSCREEN As Integer = 1
Public Const CURSOR_SHOWING As Int32 = 1
<StructLayout(LayoutKind.Sequential)> _
Public Structure ICONINFOStructure ICONINFO
Public fIcon As Boolean
' Specifies whether this structure defines an icon or a cursor. A value of TRUE specifies
Public xHotspot As Int32
' Specifies the x-coordinate of a cursor's hot spot. If this structure defines an icon, the hot
Public yHotspot As Int32
' Specifies the y-coordinate of the cursor's hot spot. If this structure defines an icon, the hot
Public hbmMask As IntPtr
' (HBITMAP) Specifies the icon bitmask bitmap. If this structure defines a black and white icon,
Public hbmColor As IntPtr
' (HBITMAP) Handle to the icon color bitmap. This member can be optional if this
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure POINTStructure POINT
Public x As Int32
Public y As Int32
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure CURSORINFOStructure CURSORINFO
Public cbSize As Int32
' Specifies the size, in bytes, of the structure.
Public flags As Int32
' Specifies the cursor state. This parameter can be one of the following values:
Public hCursor As IntPtr
' Handle to the cursor.
Public ptScreenPos As POINT
' A POINT structure that receives the screen coordinates of the cursor.
End Structure
#End Region


Class Functions#Region "Class Functions"
<DllImport("user32.dll", EntryPoint:="GetDesktopWindow")> _
Public Shared Function GetDesktopWindow()Function GetDesktopWindow() As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="GetDC")> _
Public Shared Function GetDC()Function GetDC(ByVal ptr As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="GetSystemMetrics")> _
Public Shared Function GetSystemMetrics()Function GetSystemMetrics(ByVal abc As Integer) As Integer
End Function
<DllImport("user32.dll", EntryPoint:="GetWindowDC")> _
Public Shared Function GetWindowDC()Function GetWindowDC(ByVal ptr As Int32) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="ReleaseDC")> _
Public Shared Function ReleaseDC()Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDc As IntPtr) As IntPtr
End Function

<DllImport("user32.dll", EntryPoint:="GetCursorInfo")> _
Public Shared Function GetCursorInfo()Function GetCursorInfo(ByRef pci As CURSORINFO) As Boolean
End Function
<DllImport("user32.dll", EntryPoint:="CopyIcon")> _
Public Shared Function CopyIcon()Function CopyIcon(ByVal hIcon As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="GetIconInfo")> _
Public Shared Function GetIconInfo()Function GetIconInfo(ByVal hIcon As IntPtr, ByRef piconinfo As ICONINFO) As Boolean
End Function

#End Region
End Class
End Namespace
Imports
System
Imports
System.Collections.Generic
Imports
System.Text
Imports
System.Runtime.InteropServices

Namespace ScreenshotCaptureWithMouse
Namespace ScreenshotCaptureWithMouse.ScreenCapture
Class GDIStuffClass GDIStuff
Class Variables#Region "Class Variables"
Public Const SRCCOPY As Integer = 13369376
#End Region


Class Functions#Region "Class Functions"
<DllImport("gdi32.dll", EntryPoint:="CreateDC")> _
Public Shared Function CreateDC()Function CreateDC(ByVal lpszDriver As IntPtr, ByVal lpszDevice As String, ByVal lpszOutput As IntPtr, ByVal lpInitData As IntPtr) As IntPtr
End Function
<DllImport("gdi32.dll", EntryPoint:="DeleteDC")> _
Public Shared Function DeleteDC()Function DeleteDC(ByVal hDc As IntPtr) As IntPtr
End Function
<DllImport("gdi32.dll", EntryPoint:="DeleteObject")> _
Public Shared Function DeleteObject()Function DeleteObject(ByVal hDc As IntPtr) As IntPtr
End Function
<DllImport("gdi32.dll", EntryPoint:="BitBlt")> _
Public Shared Function BitBlt()Function BitBlt(ByVal hdcDest As IntPtr, ByVal xDest As Integer, ByVal yDest As Integer, ByVal wDest As Integer, ByVal hDest As Integer, ByVal hdcSource As IntPtr, _
ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal RasterOp As Integer) As Boolean
End Function
<DllImport("gdi32.dll", EntryPoint:="CreateCompatibleBitmap")> _
Public Shared Function CreateCompatibleBitmap()Function CreateCompatibleBitmap(ByVal hdc As IntPtr, ByVal nWidth As Integer, ByVal nHeight As Integer) As IntPtr
End Function
<DllImport("gdi32.dll", EntryPoint:="CreateCompatibleDC")> _
Public Shared Function CreateCompatibleDC()Function CreateCompatibleDC(ByVal hdc As IntPtr) As IntPtr
End Function
<DllImport("gdi32.dll", EntryPoint:="SelectObject")> _
Public Shared Function SelectObject()Function SelectObject(ByVal hdc As IntPtr, ByVal bmp As IntPtr) As IntPtr
End Function
#End Region
End Class
End Namespace
Imports
System.Drawing
Imports
System.Runtime.InteropServices


Namespace ScreenshotCaptureWithMouse
Namespace ScreenshotCaptureWithMouse.ScreenCapture
Class CaptureScreenClass CaptureScreen
'This structure shall be used to keep the size of the screen.
Public Structure SIZEStructure SIZE
Public cx As Integer
Public cy As Integer
End Structure

Private Shared Function CaptureDesktop()Function CaptureDesktop() As Bitmap
Dim size As SIZE
Dim hBitmap As IntPtr
Dim hDC As IntPtr = Win32Stuff.GetDC(Win32Stuff.GetDesktopWindow())
Dim hMemDC As IntPtr = GDIStuff.CreateCompatibleDC(hDC)
size.cx = Win32Stuff.GetSystemMetrics(Win32Stuff.SM_CXSCREEN)
size.cy = Win32Stuff.GetSystemMetrics(Win32Stuff.SM_CYSCREEN)
hBitmap = GDIStuff.CreateCompatibleBitmap(hDC, size.cx, size.cy)
If hBitmap <> IntPtr.Zero Then
Dim hOld As IntPtr = DirectCast(GDIStuff.SelectObject(hMemDC, hBitmap), IntPtr)
GDIStuff.BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, _
0, 0, GDIStuff.SRCCOPY)
GDIStuff.SelectObject(hMemDC, hOld)
GDIStuff.DeleteDC(hMemDC)
Win32Stuff.ReleaseDC(Win32Stuff.GetDesktopWindow(), hDC)
Dim bmp As Bitmap = System.Drawing.Image.FromHbitmap(hBitmap)
GDIStuff.DeleteObject(hBitmap)
GC.Collect()
Return bmp
End If
Return Nothing
End Function


Private Shared Function CaptureCursor()Function CaptureCursor(ByRef x As Integer, ByRef y As Integer) As Bitmap
Dim bmp As Bitmap
Dim hicon As IntPtr
Dim ci As New Win32Stuff.CURSORINFO()
Dim icInfo As Win32Stuff.ICONINFO
ci.cbSize = Marshal.SizeOf(ci)
If Win32Stuff.GetCursorInfo(ci) Then
If ci.flags = Win32Stuff.CURSOR_SHOWING Then
hicon = Win32Stuff.CopyIcon(ci.hCursor)
If Win32Stuff.GetIconInfo(hicon, icInfo) Then
x = ci.ptScreenPos.x - CInt(icInfo.xHotspot)
y = ci.ptScreenPos.y - CInt(icInfo.yHotspot)
Dim ic As Icon = Icon.FromHandle(hicon)
bmp = ic.ToBitmap()
Return bmp
End If
End If
End If
Return Nothing
End Function

Public Shared Function CaptureDesktopWithCursor()Function CaptureDesktopWithCursor() As Bitmap
Dim cursorX As Integer = 0
Dim cursorY As Integer = 0
Dim desktopBMP As Bitmap
Dim cursorBMP As Bitmap
Dim g As Graphics
Dim r As Rectangle
desktopBMP = CaptureDesktop()
cursorBMP = CaptureCursor(cursorX, cursorY)
If desktopBMP IsNot Nothing Then
If cursorBMP IsNot Nothing Then
r = New Rectangle(cursorX, cursorY, cursorBMP.Width, cursorBMP.Height)
g = Graphics.FromImage(desktopBMP)
g.DrawImage(cursorBMP, r)
g.Flush()
Return desktop
Else
Return desktopBMP
End If
End If
Return Nothing
End Function
End Class
End Namespace
本文介绍了一种使用.NET框架结合Windows API实现全屏截图并同时捕获鼠标光标的方法。通过创建Bitmap对象和利用Graphics类绘制图像,最终生成包含鼠标光标的屏幕快照。
1251





