GetBitmapDC example for C++ Builder
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 DrawTextStr example. |
1. |
Start with the project that you created in Loading and Displaying an Image. |
2. |
Select the Button 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. |
3. |
Change the Button control's Caption property to GDI for Text. |
4. |
Code the GDI for Text button's Click procedure as follows. In online help, you can copy the block of code and paste it into your application. |
|
Notice that the code prints in two different places: the bitmap and the control. You can see the difference when you run the program. |
void __fastcall TForm1::Button1Click(TObject *Sender)
{
// First, print text on the bitmap.
HDC hdcLead = (HDC)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.
HFONT hfnt = (HFONT)GetStockObject(SYSTEM_FONT);
HFONT hOldFont = (HFONT)SelectObject(hdcLead, hfnt);
// Assign the string value
static char mystring[] = "Text on the bitmap";
// Make the background transparent, so that the text will overlay the image
SetBkMode( hdcLead, TRANSPARENT);
// output black text on white background
SetBkColor( hdcLead, RGB(255, 255, 255));
// Print the text on the bitmap. This is a permanent change in the bitmap
TextOut( hdcLead, 20, 40, mystring, lstrlen(mystring));
// Select the old font
SelectObject( hdcLead, hOldFont);
// Release the device context
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
hdcLead = (HDC)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 = (HFONT)GetStockObject(SYSTEM_FONT);
hOldFont = (HFONT)SelectObject(hdcLead, hfnt);
// Assign the string value
static char mystring2[] = "Text on the control";
// Make the background transparent, so that the text will overlay the image
SetBkMode( hdcLead, TRANSPARENT);
// output black text on white background
SetBkColor( hdcLead, RGB(255, 255, 255));
// Print the text on the control. This is an overlay that does not affect the bitmap.
TextOut( hdcLead, 20, 20, mystring2, lstrlen(mystring2));
// Restore the previous font
SelectObject( hdcLead, hOldFont);
// Remove the lock from the device control
Lead1->ReleaseClientDC();
}
5. |
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. |