'模块代码 Public Type PALETTEENTRY peRed As Byte peGreen As Byte peBlue As Byte peFlags As Byte End TypePublic Type LOGPALETTE palVersion As Integer palNumEntries As Integer palPalEntry(255) As PALETTEENTRY End TypePublic Type GUID Data1 As Long Data2 As Integer Data3 As Integer Data4(7) As Byte End TypePublic Const RASTERCAPS As Long = 38 Public Const RC_PALETTE As Long = &H100 Public Const SIZEPALETTE As Long = 104 Public Const vbHiMetric As Integer = 8 Public Type RECT Left As Long Top As Long Right As Long Bottom As Long End TypePublic Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Long) As Long Public Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long Public Declare Function GetDeviceCaps Lib "GDI32" (ByVal hDC As Long, ByVal iCapabilitiy As Long) As Long Public Declare Function GetSystemPaletteEntries Lib "GDI32" (ByVal hDC As Long, ByVal wStartIndex As Long, ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) As Long Public Declare Function CreatePalette Lib "GDI32" (lpLogPalette As LOGPALETTE) As Long Public Declare Function SelectObject Lib "GDI32" (ByVal hDC As Long, ByVal hObject As Long) As Long Public Declare Function BitBlt Lib "GDI32" (ByVal hDCDest As Long, ByVal XDest As Long, ByVal YDest As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hDCSrc As Long, ByVal XSrc As Long, ByVal YSrc As Long, ByVal dwRop As Long) As Long Public Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Long) As Long Public Declare Function GetForegroundWindow Lib "USER32" () As Long Public Declare Function SelectPalette Lib "GDI32" (ByVal hDC As Long, ByVal hPalette As Long, ByVal bForceBackground As Long) As Long Public Declare Function RealizePalette Lib "GDI32" (ByVal hDC As Long) As Long Public Declare Function GetWindowDC Lib "USER32" (ByVal hWnd As Long) As Long Public Declare Function GetDC Lib "USER32" (ByVal hWnd As Long) As Long Public Declare Function GetWindowRect Lib "USER32" (ByVal hWnd As Long, lpRect As RECT) As Long Public Declare Function ReleaseDC Lib "USER32" (ByVal hWnd As Long, ByVal hDC As Long) As Long Public Declare Function GetDesktopWindow Lib "USER32" () As Long Public Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As LongPublic Type PicBmp Size As Long Type As Long hBmp As Long hPal As Long Reserved As Long End Type'窗体代码 Option Explicit Public Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) As Picture Dim r As Long Dim Pic As PicBmp Dim IPic As IPicture Dim gu As GUID With gu .Data1 = &H20400 .Data4(0) = &HC0 .Data4(7) = &H46 End With With Pic .Size = Len(Pic) .Type = vbPicTypeBitmap .hBmp = hBmp .hPal = hPal End With r = OleCreatePictureIndirect(Pic, gu, 1, IPic) Set CreateBitmapPicture = IPic End Function Public Function CaptureWindow(ByVal hWndSrc As Long, ByVal Client As Boolean, ByVal LeftSrc As Long, ByVal TopSrc As Long, ByVal WidthSrc As Long, ByVal HeightSrc As Long) As Picture Dim hDCMemory As Long Dim hBmp As Long Dim hBmpPrev As Long Dim r As Long Dim hDCSrc As Long Dim hPal As Long Dim hPalPrev As Long Dim RasterCapsScrn As Long Dim HasPaletteScrn As Long Dim PaletteSizeScrn As Long Dim LogPal As LOGPALETTE If Client Then hDCSrc = GetDC(hWndSrc) Else hDCSrc = GetWindowDC(hWndSrc) End If hDCMemory = CreateCompatibleDC(hDCSrc) hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc) hBmpPrev = SelectObject(hDCMemory, hBmp) RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS) HasPaletteScrn = RasterCapsScrn And RC_PALETTE PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE) If HasPaletteScrn And (PaletteSizeScrn = 256) Then LogPal.palVersion = &H300 LogPal.palNumEntries = 256 r = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry(0)) hPal = CreatePalette(LogPal) hPalPrev = SelectPalette(hDCMemory, hPal, 0) r = RealizePalette(hDCMemory) End If r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, LeftSrc, TopSrc, vbSrcCopy) hBmp = SelectObject(hDCMemory, hBmpPrev) If HasPaletteScrn And (PaletteSizeScrn = 256) Then hPal = SelectPalette(hDCMemory, hPalPrev, 0) End If r = DeleteDC(hDCMemory) r = ReleaseDC(hWndSrc, hDCSrc) Set CaptureWindow = CreateBitmapPicture(hBmp, hPal) End FunctionPublic Function CaptureScreen() As Picture Dim hWndScreen As Long '取得窗体句柄 hWndScreen = GetDesktopWindow() '捕获窗体 Set CaptureScreen = CaptureWindow(hWndScreen, False, 0, 0, Screen.Width \ Screen.TwipsPerPixelX, Screen.Height \ Screen.TwipsPerPixelY) End FunctionPrivate Sub Command1_Click() Picture1.Picture = CaptureScreen() End Sub
'以下程序可以实现将屏幕按比例保存成jpg文件[保存成jpg文件另外需要文件支持,可来消息索要!] 希望能帮到楼主Public Function Get_Desktop(ByVal theFile As String, SetSx As Integer, SetSy As Integer) As Boolean Dim hdc As Long, sx As Integer, sy As Integer
http://www.codeguru.com/vb/articles/1998.shtml
Public Type PALETTEENTRY
peRed As Byte
peGreen As Byte
peBlue As Byte
peFlags As Byte
End TypePublic Type LOGPALETTE
palVersion As Integer
palNumEntries As Integer
palPalEntry(255) As PALETTEENTRY
End TypePublic Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End TypePublic Const RASTERCAPS As Long = 38
Public Const RC_PALETTE As Long = &H100
Public Const SIZEPALETTE As Long = 104
Public Const vbHiMetric As Integer = 8
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End TypePublic Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Long) As Long
Public Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Public Declare Function GetDeviceCaps Lib "GDI32" (ByVal hDC As Long, ByVal iCapabilitiy As Long) As Long
Public Declare Function GetSystemPaletteEntries Lib "GDI32" (ByVal hDC As Long, ByVal wStartIndex As Long, ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) As Long
Public Declare Function CreatePalette Lib "GDI32" (lpLogPalette As LOGPALETTE) As Long
Public Declare Function SelectObject Lib "GDI32" (ByVal hDC As Long, ByVal hObject As Long) As Long
Public Declare Function BitBlt Lib "GDI32" (ByVal hDCDest As Long, ByVal XDest As Long, ByVal YDest As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hDCSrc As Long, ByVal XSrc As Long, ByVal YSrc As Long, ByVal dwRop As Long) As Long
Public Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Long) As Long
Public Declare Function GetForegroundWindow Lib "USER32" () As Long
Public Declare Function SelectPalette Lib "GDI32" (ByVal hDC As Long, ByVal hPalette As Long, ByVal bForceBackground As Long) As Long
Public Declare Function RealizePalette Lib "GDI32" (ByVal hDC As Long) As Long
Public Declare Function GetWindowDC Lib "USER32" (ByVal hWnd As Long) As Long
Public Declare Function GetDC Lib "USER32" (ByVal hWnd As Long) As Long
Public Declare Function GetWindowRect Lib "USER32" (ByVal hWnd As Long, lpRect As RECT) As Long
Public Declare Function ReleaseDC Lib "USER32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
Public Declare Function GetDesktopWindow Lib "USER32" () As Long
Public Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As LongPublic Type PicBmp
Size As Long
Type As Long
hBmp As Long
hPal As Long
Reserved As Long
End Type'窗体代码
Option Explicit Public Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) As Picture
Dim r As Long
Dim Pic As PicBmp
Dim IPic As IPicture
Dim gu As GUID
With gu
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With With Pic
.Size = Len(Pic)
.Type = vbPicTypeBitmap
.hBmp = hBmp
.hPal = hPal
End With
r = OleCreatePictureIndirect(Pic, gu, 1, IPic)
Set CreateBitmapPicture = IPic
End Function
Public Function CaptureWindow(ByVal hWndSrc As Long, ByVal Client As Boolean, ByVal LeftSrc As Long, ByVal TopSrc As Long, ByVal WidthSrc As Long, ByVal HeightSrc As Long) As Picture
Dim hDCMemory As Long
Dim hBmp As Long
Dim hBmpPrev As Long
Dim r As Long
Dim hDCSrc As Long
Dim hPal As Long
Dim hPalPrev As Long
Dim RasterCapsScrn As Long
Dim HasPaletteScrn As Long
Dim PaletteSizeScrn As Long
Dim LogPal As LOGPALETTE
If Client Then
hDCSrc = GetDC(hWndSrc)
Else
hDCSrc = GetWindowDC(hWndSrc)
End If
hDCMemory = CreateCompatibleDC(hDCSrc)
hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
hBmpPrev = SelectObject(hDCMemory, hBmp)
RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS)
HasPaletteScrn = RasterCapsScrn And RC_PALETTE
PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE)
If HasPaletteScrn And (PaletteSizeScrn = 256) Then
LogPal.palVersion = &H300
LogPal.palNumEntries = 256
r = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry(0))
hPal = CreatePalette(LogPal)
hPalPrev = SelectPalette(hDCMemory, hPal, 0)
r = RealizePalette(hDCMemory)
End If
r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, LeftSrc, TopSrc, vbSrcCopy)
hBmp = SelectObject(hDCMemory, hBmpPrev)
If HasPaletteScrn And (PaletteSizeScrn = 256) Then
hPal = SelectPalette(hDCMemory, hPalPrev, 0)
End If
r = DeleteDC(hDCMemory)
r = ReleaseDC(hWndSrc, hDCSrc)
Set CaptureWindow = CreateBitmapPicture(hBmp, hPal)
End FunctionPublic Function CaptureScreen() As Picture
Dim hWndScreen As Long
'取得窗体句柄
hWndScreen = GetDesktopWindow()
'捕获窗体
Set CaptureScreen = CaptureWindow(hWndScreen, False, 0, 0, Screen.Width \ Screen.TwipsPerPixelX, Screen.Height \ Screen.TwipsPerPixelY)
End FunctionPrivate Sub Command1_Click()
Picture1.Picture = CaptureScreen()
End Sub
picCopy.AutoRedraw = True
hdc = GetDC(0)
sx = Screen.Width / Screen.TwipsPerPixelX
sy = Screen.Height / Screen.TwipsPerPixelY
If SetSx = 0 Or SetSy = 0 Then SetSx = sx: SetSy = sy
picCopy.Width = SetSx * Screen.TwipsPerPixelX
picCopy.Height = SetSy * Screen.TwipsPerPixelY
SetStretchBltMode picCopy.hdc, 3
StretchBlt picCopy.hdc, 0, 0, SetSx, SetSy, hdc, 0, 0, sx, sy, vbSrcCopy 'ËõС
ReleaseDC 0, hdc
picCopy.Picture = picCopy.Image 'ÕâµãºÜÖØÒª£¡
'--------------------------------------------±£´æ³ÉjpgÎļþ
Dim p As New aDIBSection
p.CreateFromPicture picCopy.Picture
SaveJPG p, theFile, 75
'--------------------------------------------------------
picCopy.Width = 1
picCopy.Height = 1
picCopy.AutoRedraw = False
Get_Desktop = True
GetCursorInfo获取相关信息
DrawIcon将鼠标图像画到指定的地方
picCopy.AutoRedraw = True
hdc = GetDC(0)
sx = Screen.Width / Screen.TwipsPerPixelX
sy = Screen.Height / Screen.TwipsPerPixelY
If SetSx = 0 Or SetSy = 0 Then SetSx = sx: SetSy = sy
picCopy.Width = SetSx * Screen.TwipsPerPixelX
picCopy.Height = SetSy * Screen.TwipsPerPixelY
SetStretchBltMode picCopy.hdc, 3
StretchBlt picCopy.hdc, 0, 0, SetSx, SetSy, hdc, 0, 0, sx, sy, vbSrcCopy 'ËõС
ReleaseDC 0, hdc
picCopy.Picture = picCopy.Image
Dim p As New aDIBSection
p.CreateFromPicture picCopy.Picture
SaveJPG p, theFile, 75
picCopy.Width = 1
picCopy.Height = 1
picCopy.AutoRedraw = False
Get_Desktop = True
End Function