GetBitmapDC example for Access 95 and 97

Take the following steps to demonstrate how GDI functions can be used to print text on the bitmap and on the control. The text printed on the bitmap is a permanent part of the bitmap and becomes permanent if the bitmap is saved; whereas the text on the control is superficial and does not affect the bitmap.

Note: For an easier way of drawing text, refer to the DrawText example.

1. Start with the project that you created in Loading and Displaying an Image.

2. In the Database window, select the Modules tab, and click the New button.

3. Add the following public declarations and constants to support the GDI functions. In online help, you can select the block of code with the mouse, then press Ctrl-C to copy it. (Refer to the Access documentation for instructions on using API functions.)

Public Declare Function SetBkMode Lib "gdi32" (ByVal hdc As Long, ByVal nBkMode As Long) As Long
Public Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Public Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long
Public Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long
Global Const SYSTEM_FONT = 13
Global Const TRANSPARENT = 1

4. Close the file, and save it as the GDI Declarations module.

5. In the Database window, select the Forms tab, select Form1, and click the Design button.

6. image\btncmd.gif Select the Command Button control; then add a control to your form. (Cancel the Command Button Wizard when it appears.) Put the control at the top of the form to keep it away from the image.

7. In the Properties box, change the Command Button control's Name property to PrintText and change the Caption property to Print Text.

8. Click the Code Window icon on the toolbar.

image\btncode.gif For Access 95.

image\btncode2.gif For Access 97.

9. Select the Click procedure for the PrintText object, and add the following code. In online help, you can select the block of code with the mouse, then press Ctrl-C to copy it.

' Declare some local variables
Dim LeadDC, hfnt, hOldFont, MyReturn, mystring$, LeadClientDC

' Ensure that the Lead control is visible
Me![Lead1].Visible = True

' First, print text on the bitmap.
'Create a device context for the bitmap that the GDI functions can access
LeadDC = Lead1.GetBitmapDC

' Get the system font and select it. The old font is saved so that it can be restored.
' Using other fonts is more complex. Refer to the Windows API documentation.
hfnt = GetStockObject(SYSTEM_FONT)
hOldFont = SelectObject(LeadDC, hfnt) 

' Make the background transparent, so that the text will overlay the image
MyReturn = SetBkMode(LeadDC, TRANSPARENT) 

' Assign the string value
mystring$ = "Text on the bitmap"

' Print the text on the bitmap. This is a permanent change in the bitmap
MyReturn = TextOut(LeadDC, 20, 40, mystring$, Len(mystring$))

' Restore the previous font
MyReturn = SelectObject(LeadDC, hOldFont)
'Remove the lock from the device control
Lead1.ReleaseBitmapDC

' Repaint the part of the image that has changed
Lead1.BackErase = False
Lead1.ForceRepaint

' Allow full repainting again
Lead1.BackErase = True

' Next, print text on the control.
' Create a device context for the control that the GDI functions can access
LeadClientDC = Lead1.GetClientDC

' Get the system font and select it. The old font is saved so that it can be restored.
' Using other fonts is more complex. Refer to the Windows API documentation.
hfnt = GetStockObject(SYSTEM_FONT)
hOldFont = SelectObject(LeadDC, hfnt)

' Make the background transparent, so that the text will overlay the image
MyReturn = SetBkMode(LeadClientDC, TRANSPARENT)

' Assign the string value
mystring$ = "Text on the LEAD control"

' Print the text on the control. This is an overlay that does not affect the bitmap.
MyReturn = TextOut(LeadClientDC, 20, 80, mystring$, Len(mystring$))

' Restore the previous font
MyReturn = SelectObject(LeadClientDC, hOldFont)

' Remove the lock from the device control
Lead1.ReleaseClientDC

10. image\btncmpl.gif Click the compile button on the toolbar; then close the code window.

11. Close the form designer, saving the changes.

12. With Form1 selected in the Database window, click the Open button to test the form. Notice that the text printed on the bitmap is scaled as part of the bitmap. If you zoom in, it becomes larger, and if you zoom out, it becomes smaller. The text printed on the control is the normal size for the system font and is not affected by zooming.