GetBitmapDC example for Visual Basic
Note: Also works with 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. |
Open the API Viewer by double-clicking the API Text Viewer icon in the Visual Basic program group on your desktop. |
3. |
From the File menu, choose Load Text File. |
4. |
Open WIN32API.TXT (32-bit Windows declares, types, and constants), which is located in the \WINAPI subdirectory of the main Visual Basic directory. |
5. |
In the API Type drop-down list box, select Declares; then select and add the following items: |
|
SetBkMode |
|
TextOut |
|
GetStockObject |
|
SelectObject |
6. |
In the API Type drop-down list box, select Constants; then select and add the following items: |
|
SYSTEM_FONT |
|
TRANSPARENT |
7. |
Copy the selected items, and paste them into the declarations procedure of the general object in your form. In this example, you must edit the declarations and constants to make them private. On a 32-bit system, they should appear as follows: |
Private Declare Function SetBkMode Lib "gdi32" (ByVal hdc As Long, ByVal nBkMode As Long) As Long
Private 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
Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Const SYSTEM_FONT = 13
Private Const TRANSPARENT = 1
8. |
Select the CommandButton control; then add the control to your main form. Put the control at the top of the form to keep it away from the image. |
9. |
In the Properties box, change the CommandButton control's Caption property to GDI for Text. |
10. |
Add the following code to the CommandButton control's Click procedure. In online help, you can use the Edit pull-down menu to copy the block of code. |
|
Notice that the code prints in two different places: the bitmap and the control. You can see the difference when you run the program. |
Sub Command1_Click ()
Dim LeadDC As Long
Dim LeadClientDC As Long
Dim hfnt As Long
Dim hOldFont As Long
Dim MyReturn As Long
Dim mystring$
'Ensure that the Lead control is visible
LEADRasterView1.Visible = True
'First, print text on the bitmap.
'Create a device context for the bitmap that the GDI functions can access
LeadDC = LEADRasterView1.Raster.GetBitmapDC
'Get the system font and select it. The old font is saved.
'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
LEADRasterView1.Raster.ReleaseBitmapDC
'Repaint the part of the image that has changed
LEADRasterView1.BackErase = False
LEADRasterView1.ForceRepaint
'Allow full repainting again
LEADRasterView1.BackErase = True
'Next, print text on the control.
'Create a device context for the control that the GDI functions can access
LeadClientDC = LEADRasterView1.GetClientDC
'Get the system font and select it. The old font is saved.
'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
LEADRasterView1.ReleaseClientDC
End Sub
11. |
Run your program to test it. 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. |