ACC: How to Capture Screen Shots of Your Forms (1.x/2.0)

ID: Q100973


The information in this article applies to:


SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

You can use Microsoft Windows API functions to capture screen shots of any object on the Microsoft Windows desktop. You can perform screen captures of Microsoft Access, Microsoft Access objects, other Microsoft Windows applications, or the entire desktop.

This article assumes that you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information on Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x, or the "Building Applications" manual in version 2.0.


MORE INFORMATION

The following sample Access Basic code will capture an object on the screen to the Clipboard. Note that you can change the handle AccessHwnd in the sample code below to create screen captures of other applications, including specific objects within Microsoft Access.

After capturing a screen, you can open Paintbrush (PBRUSH.EXE) and choose the Paste command from the Edit menu. The screen capture from Microsoft Access will be loaded into the Paintbrush window.

To run this code, call the ScreenDump() function from an event, a macro, or the module's Immediate window. To run ScreenDump() from the Immediate window, type the following, and then press ENTER:

?ScreenDump()

To set up the ScreenDump() function, open a new or existing module and insert the code listed below.

Notes:

Code for the ScreenDump() Function:

   Option Compare Database
   Option Explicit

   Type RECT_Type
       left As Integer
       top As Integer
       right As Integer
       bottom As Integer
   End Type

   Declare Function GetActiveWindow% Lib "User" ()
   Declare Function GetDesktopWindow% Lib "User" ()
   Declare Sub GetWindowRect Lib "User" (ByVal Hwnd%, _
                                       lpRect As RECT_Type)
   Declare Function GetDC% Lib "User" (ByVal Hwnd%)
   Declare Function CreateCompatibleDC% Lib "GDI" (ByVal hdc%)
   Declare Function CreateCompatibleBitmap% Lib "GDI" (ByVal hdc%, _
                                       ByVal nWidth%, ByVal nHeight%)
   Declare Function SelectObject% Lib "GDI" (ByVal hdc%, ByVal hObject%)
   Declare Function BitBlt% Lib "GDI" (ByVal hDestDC%, ByVal X%, _
                                       ByVal Y%, ByVal nWidth%, _
                                       ByVal nHeight%, ByVal hSrcDC%, _
                                       ByVal XSrc%, ByVal YSrc%, _
                                       ByVal dwRop&)
   Declare Function OpenClipboard% Lib "User" (ByVal Hwnd%)
   Declare Function EmptyClipboard% Lib "User" ()
   Declare Function SetClipboardData% Lib "User" (ByVal wFormat%, _
                                       ByVal hMem%)
   Declare Function CloseClipboard% Lib "User" ()
   Declare Function ReleaseDC% Lib "User" (ByVal Hwnd%, ByVal hdc%)
   Declare Function DeleteDC% Lib "GDI" (ByVal hdc%)

   Global Const SRCCOPY = &HCC0020
   Global Const CF_BITMAP = 2

   Function ScreenDump ()
      Dim AccessHwnd%, DeskHwnd%
      Dim hdc%
      Dim hdcMem%
      Dim rect As RECT_Type
      Dim junk%
      Dim fwidth%, fheight%
      Dim hBitmap%

       DoCmd Hourglass True

       '---------------------------------------------------
       ' Get window handle to Windows and Microsoft Access
       '---------------------------------------------------
       DeskHwnd = GetDesktopWindow()
       AccessHwnd = GetActiveWindow()

       '---------------------------------------------------
       ' Get screen coordinates of Microsoft Access
       '---------------------------------------------------
       Call GetWindowRect(AccessHwnd, rect)
       fwidth = rect.right - rect.left
       fheight = rect.bottom - rect.top

       '---------------------------------------------------
       ' Get the device context of Desktop and allocate memory
       '---------------------------------------------------
       hdc = GetDC(DeskHwnd)
       hdcMem = CreateCompatibleDC(hdc)
       hBitmap = CreateCompatibleBitmap(hdc, fwidth, fheight)

       If hBitmap <> 0 Then
          junk = SelectObject(hdcMem, hBitmap)

          '---------------------------------------------
          ' Copy the Desktop bitmap to memory location
          ' based on Microsoft Access coordinates.
          '---------------------------------------------
          junk = BitBlt(hdcMem, 0, 0, fwidth, fheight, hdc, rect.left, _
                        rect.top, SRCCOPY)

          '---------------------------------------------
          ' Set up the Clipboard and copy bitmap
          '---------------------------------------------
          junk = OpenClipboard(DeskHwnd)
          junk = EmptyClipboard()
          junk = SetClipboardData(CF_BITMAP, hBitmap)
          junk = CloseClipboard()
       End If

       '---------------------------------------------
       ' Clean up handles
       '---------------------------------------------
       junk = DeleteDC(hdcMem)
       junk = ReleaseDC(DeskHwnd, hdc)

       DoCmd Hourglass False

   End Function 


REFERENCES

For an example of how to capture screens in Microsoft Access 95 and 97, please see the following article here in the Microsoft Knowledge Base:

Q148392 ACC: How to Capture Screens of Your Forms (95/97)

Additional query words: dump


Keywords          : kbprg 
Version           : 1.0 1.1 2.0
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: March 23, 1999