The new vbAccelerator Site - more VB and .NET Code and Controls

Get a Picture of the Desktop's Contents

Author:

Steve McMahon(steve@vbaccelerator.com)

Keywords:

API,Desktop,GDI,Graphics

Updated:

01/08/98

Other Tips
All Tips
By Date
By Subject


API (33)
Bit
Manipulation (3)

Clipboard (3)
Combo
Box (5)

Desktop (3)
GDI (13)
Graphics (13)
Internet (2)
Interprocess
Comms (3)

Keyboard (2)
Mouse (1)
Shell (1)
Sprites (1)
Subclassing (3)
Text
Box (2)

Windows (11)
Windows
Controls (10)



Submit


This tip shows how to get a picture of the entire desktop. You can use the picture that you get to draw onto (perhaps as the basis for a screen saver), or as a basis for capturing areas of the desktop.

Start a new project in VB. Add a new module, and add the following code:

Private Type RECT
&nbsp &nbsp Left As Long
&nbsp &nbsp Top As Long
&nbsp &nbsp Right As Long
&nbsp &nbsp Bottom As Long
End Type
Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
Private Declare Function CreateDCAsNull Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, lpDeviceName As Any, lpOutput As Any, lpInitData As Any) 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 Const SRCCOPY = &HCC0020 ' (DWORD) dest = source
Private Declare Function GetDesktopWindow Lib "user32" () As Long

Public Sub CopyDesktop( _
&nbsp &nbsp &nbsp &nbsp ByRef objTo As Object _
&nbsp &nbsp )
Dim hWnd As Long
Dim tR As RECT
Dim hDC As Long
&nbsp &nbsp
&nbsp &nbsp
' Note: objTo must have hDC,Picture,Width and Height
&nbsp &nbsp ' properties and should have AutoRedraw = True
&nbsp &nbsp
&nbsp &nbsp
' Get the size of the desktop window:
&nbsp &nbsp hWnd = GetDesktopWindow()
&nbsp &nbsp GetWindowRect hWnd, tR
&nbsp &nbsp
&nbsp &nbsp
' Set the object to the relevant size:
&nbsp &nbsp objTo.Width = (tR.Right - tR.Left) * Screen.TwipsPerPixelX
&nbsp &nbsp objTo.Height = (tR.Bottom - tR.Top) * Screen.TwipsPerPixelY
&nbsp &nbsp
&nbsp &nbsp
' Now get the desktop DC:
&nbsp &nbsp hDC = CreateDCAsNull("DISPLAY", ByVal 0&, ByVal 0&, ByVal 0&)
&nbsp &nbsp
' Copy the contents of the desktop to the object:
&nbsp &nbsp BitBlt objTo.hDC, 0, 0, (tR.Right - tR.Left), (tR.Bottom - tR.Top), hDC, 0, 0, SRCCOPY
&nbsp &nbsp
' Ensure we clear up DC GDI has given us:
&nbsp &nbsp DeleteDC hDC
&nbsp &nbsp
End Sub


To try out the desktop capture, add a Picture Box to the project's form. Set the AutoRedraw property of the Picture Box to True. Then add a Command button. Make sure that the Command button is above the Picture Box in the form, otherwise when you capture the desktop and get an Image of your form, it can get a little confusing working out which is the real button to click on! Add the following code to the Command button's click event:

Private Sub Command1_Click()
&nbsp &nbsp Picture1.Cls
&nbsp &nbsp CopyDesktop Picture1
&nbsp &nbsp Picture1.Refresh
End Sub

When you click on the button, the Picture will get a complete image of the desktop. It will also be resized to the size of the desktop.


&nbsp

Related Tips and Articles:

&nbsp
 

About  Contribute  Send Feedback  Privacy

Copyright © 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 01/08/98