Microsoft KB Archive/100973

From BetaArchive Wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Knowledge Base


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

Article ID: 100973

Article Last Modified on 5/6/2003



APPLIES TO

  • Microsoft Access 1.0 Standard Edition
  • Microsoft Access 1.1 Standard Edition
  • Microsoft Access 2.0 Standard Edition



This article was previously published under Q100973

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:

  • In the following sample code, an underscore (_) is used as a line- continuation character. Remove the underscore when re-creating this code in Access Basic.
  • 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.

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:

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



Additional query words: dump

Keywords: kbhowto kbprogramming KB100973