ACC: How to Capture Screen Shots of Your Forms (95/97)

ID: Q148392

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 capture screen shots of the Microsoft Access user interface, Microsoft Access objects, other Microsoft Windows applications, or the entire desktop.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

MORE INFORMATION

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

After capturing a screen shot, you can open Paint (Pbrush.exe) and click the Paste command on the Edit menu. The screen shot from Microsoft Access is loaded into the Paint window.

To run this code, call the ScreenDump() function from an event, a macro, or the module's Debug window. To run ScreenDump() from the Debug 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.

NOTE: You may have some Microsoft Windows API functions defined in an existing Microsoft Access library; therefore, your declarations may be duplicates. If you receive a duplicate procedure name error message, remove or comment out the declarations statement in your code.

Insert the following code into the Declarations section of your module:

   Option Compare Database
   Option Explicit

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

   'The following declare statements are case sensitive.

   Declare Function GetActiveWindow Lib "User32" () As Long
   Declare Function GetDesktopWindow Lib "User32" () As Long
   Declare Sub GetWindowRect Lib "User32" (ByVal Hwnd As Long, _
                                       lpRect As RECT_Type)
   Declare Function GetDC Lib "User32" (ByVal Hwnd As Long) As Long
   Declare Function CreateCompatibleDC Lib "Gdi32" (ByVal hdc As Long) _
                                       As Long
   Declare Function CreateCompatibleBitmap Lib "Gdi32" (ByVal hdc _
                                       As Long, ByVal nWidth As Long, _
                                       ByVal nHeight As Long) As Long
   Declare Function SelectObject Lib "Gdi32" (ByVal hdc As Long, _
                                       ByVal hObject As Long) As Long
   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
   Declare Function OpenClipboard Lib "User32" (ByVal Hwnd As Long) As Long
   Declare Function EmptyClipboard Lib "User32" () As Long
   Declare Function SetClipboardData Lib "User32" (ByVal wFormat As Long, _
                                       ByVal hMem As Long) As Long
   Declare Function CloseClipboard Lib "User32" () As Long
   Declare Function ReleaseDC Lib "User32" (ByVal Hwnd As Long, _
                                       ByVal hdc As Long) As Long
   Declare Function DeleteDC Lib "Gdi32" (ByVal hdc As Long) As Long

   Global Const SRCCOPY = &HCC0020
   Global Const CF_BITMAP = 2

Code for the ScreenDump() Function:

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

      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 more information about API calls, please see the following article here in the Microsoft Knowledge Base:

   ARTICLE-ID: Q147781
   TITLE     : ACC: How to Convert API Calls from 16-bit to 32-bit

For more information about a screen-capture function for Microsoft Access version 2.0, please see the following article here in the Microsoft Knowledge Base:

   ARTICLE-ID: Q100973
   TITLE     : ACC2: How to Capture Screen Shots of Your Forms

Additional query words: dump how to
Keywords          : kbprg
Version           : 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto

Last Reviewed: November 20, 1998