GetBitmapDC example for Visual J++

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.

  1. 2.  In the private section of the main from add the following declarations:

/** @dll.import("GDI32") */
private static native int SetBkMode( int hDC, int nBkMode );
/** @dll.import("GDI32") */
private static native int SelectObject( int hDC, int hObject );
/** @dll.import("GDI32") */
private static native int TextOut( int hDC, int X, int Y, String lpString, int nCount );
/** @dll.import("GDI32") */
private static native int GetStockObject( int nIndex );

private static final int SYSTEM_FONT = 13;
private static final int TRANSPARENT = 1;

3. 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.

4. In the Properties box, change the Button control's Caption property to GDI for Text.

5. Add the following code to the Button control's Click event. 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.

private void button1_click(Object source, Event e)
{
   // Ensure that the Lead control is visible
   LEAD1.setVisible( true );

   // First, print text on the bitmap.
   // Create a device context for the bitmap that the GDI functions can access
   int hLeadDC = LEAD1.GetBitmapDC();

   // Get the system font and select it. The old font is saved.
   // Using other fonts is more complex. Refer to the Windows C DLL documentation.
   int hFont = GetStockObject( SYSTEM_FONT );
   int hOldFont = SelectObject( hLeadDC, hFont );

   // Make the background transparent, so that the text will overlay the image
   SetBkMode( hLeadDC, TRANSPARENT );

   // Assign the string value
   String str = "Text on the bitmap";

   // Print the text on the bitmap. This is a permanent change in the bitmap
   TextOut( hLeadDC, 20, 40, str, str.length() );

   // Restore the previous font
   SelectObject( hLeadDC, hOldFont );

   // Remove the lock from the device control
   LEAD1.ReleaseBitmapDC();

   // Repaint the part of the image that has changed
   LEAD1.setBackErase( false );
   LEAD1.ForceRepaint();

   // Allow full repainting again
   LEAD1.setBackErase( true );

   // Next, print text on the control.
   // Create a device context for the control that the GDI functions can access
   int hLeadClientDC = LEAD1.GetClientDC();

   // Get the system font and select it. The old font is saved.
   // Using other fonts is more complex. Refer to the Windows C DLL documentation.
   hFont = GetStockObject( SYSTEM_FONT );
   hOldFont = SelectObject( hLeadDC, hFont );

   // Make the background transparent, so that the text will overlay the image
   SetBkMode( hLeadClientDC, TRANSPARENT );

   // Assign the string value
   str = "Text on the LEAD control";

   // Print the text on the control. 
   // This is an overlay that does not affect the bitmap.
   TextOut( hLeadClientDC, 20, 80, str, str.length() );

   // Restore the previous font
   SelectObject( hLeadClientDC, hOldFont );

   // Remove the lock from the device control
   LEAD1.ReleaseClientDC();
}