Link to home
Start Free TrialLog in
Avatar of borntough
borntough

asked on

Writing text on Bitmap at RunTime in SDI application?

Hi DanRollins and All!!

I am getting some problem in writing text at Run Time on any Bitmap in SDI application(FormView class).I am using the Picture Box to load the picture.
I want to write different texts at Different positions on the bitmap without using 'label' and 'edit boxes'.Can any of u help me in this regard.
Thanks and waiting anxiously..
Borntough
Avatar of Axter
Axter
Flag of United States of America image

Use TextOut function.
You can also use ExtTextOut and DrawText API functions.
If you need an example, try posting some of your existing code, and I'll try to add one of these functions into your code as an example.
To send text in different fonts, you want to use SelectObject API function before using above functions.

SelectObject(My_hdc, My_hfont);

Or

SelectObject(My_hdc, GetStockObject(iFont));

Then call one of the previously posted API functions.

You can also use TabbedTextOut API function for writing text.
Avatar of Anurag_n_g
Anurag_n_g

You can use this function to draw text anywhere:

BOOL ExtTextOut(
  HDC hdc,          // handle to DC
  int X,            // x-coordinate of reference point
  int Y,            // y-coordinate of reference point
  UINT fuOptions,   // text-output options
  CONST RECT* lprc, // optional dimensions
  LPCTSTR lpString, // string
  UINT cbCount,     // number of characters in string
  CONST INT* lpDx   // array of spacing values
);

hdc [in] Handle to the device context.
X [in] Specifies the x-coordinate, in logical coordinates, of the reference point used to position the string.
Y [in] Specifies the y-coordinate, in logical coordinates, of the reference point used to position the string.
fuOptions [in] Specifies how to use the application-defined rectangle. This parameter can be one or more of the following values.

Values for fuOptions:

ETO_CLIPPED
The text will be clipped to the rectangle.

ETO_GLYPH_INDEX
Windows 95 and Windows NT 4.0 and later: The lpString array refers to an array returned from GetCharacterPlacement and should be parsed directly by GDI as no further language-specific processing is required. Glyph indexing only applies to TrueType fonts, but the flag can be used for bitmap and vector fonts to indicate that no further language processing is necessary and GDI should process the string directly. Note that all glyph indexes are 16-bit values even though the string is assumed to be an array of 8-bit values for raster fonts.
For ExtTextOutW, the glyph indexes are saved to a metafile. However, to display the correct characters the metafile must be played back using the same font. For ExtTextOutA, the glyph indexes are not saved.
 
ETO_IGNORELANGUAGE
Windows NT 4.0 and later: Reserved for system use. If an application sets this flag, it loses international scripting support and in some cases it may display no text at all.  
ETO_NUMERICSLATIN
Windows 95 and Windows NT 4.0 and later: To display numbers, use European digits.
ETO_NUMERICSLOCAL
Windows 95 and Windows NT 4.0 and later: To display numbers, use digits appropriate to the locale.
ETO_OPAQUE The current background color should be used to fill the rectangle.
ETO_PDY
Windows 2000/XP: When this is set, the array pointed to by lpDx contains pairs of values. The first value of each pair is, as usual, the distance between origins of adjacent character cells, but the second value is the displacement along the vertical direction of the font.
ETO_RTLREADING
Windows 95 and Windows NT 4.0 and later for Middle East language edition of Windows: If this value is specified and a Hebrew or Arabic font is selected into the device context, the string is output using right-to-left reading order. If this value is not specified, the string is output in left-to-right order. The same effect can be achieved by setting the TA_RTLREADING value in SetTextAlign. This value is preserved for backward compatibility.  


The ETO_GLYPH_INDEX and ETO_RTLREADING values cannot be used together. Because ETO_GLYPH_INDEX implies that all language processing has been completed, the function ignores the ETO_RTLREADING flag if also specified.

lprc [in] Pointer to an optional RECT structure that specifies the dimensions, in logical coordinates, of a rectangle that is used for clipping, opaquing, or both.

lpString [in] Pointer to a string that specifies the text to be drawn. The string does not need to be zero-terminated, since cbCount specifies the length of the string.

cbCount [in] Specifies the length of the string. For the ANSI function it is a BYTE count and for the Unicode function it is a WORD count. Note that for the ANSI function, characters in SBCS code pages take one byte each, while most characters in DBCS code pages take two bytes; for the Unicode function, most currently defined Unicode characters (those in the Basic Multilingual Plane (BMP)) are one WORD while Unicode surrogates are two WORDs. Windows 95/98/Me: This value may not exceed 8192.

lpDx [in] Pointer to an optional array of values that indicate the distance between origins of adjacent character cells. For example, lpDx[i] logical units separate the origins of character cell i and character cell i + 1.


On success this function returns 0 and on failure it returns nonzero value.


Thanx
Anurag
Avatar of borntough

ASKER

Hi Axter
Thanks for replying...
My few code lines are :

void CPictureView::OnBitmap1()
{
        CEdit* Picture;
     Picture=(CEdit*)GetDlgItem(IDC_STATIC1);
     Picture->EnableWindow (true);
     Picture->ShowWindow (SW_SHOW);    
}
I have added the picture in the PictureBox at runTime.Similarly I have lot of Bitmaps in different menus..
Now ,there is no property of 'TextOut','ExtTextOut ' in it .How can now I proceed...?Guide me plz

borntough
Example:

void CPictureView::OnBitmap1()
{
     CEdit* Picture;
    Picture=(CEdit*)GetDlgItem(IDC_STATIC1);
    Picture->EnableWindow (true);
    Picture->ShowWindow (SW_SHOW);
    CDC * my_cdc = Picture->GetDC();
     if (!my_cdc) return;
     CString Msg = "Hello World!";
     my_cdc->TextOut(5, 10, Msg);
}
Hello Axter
Thanks for answering...
I have tried the above code but unfortunately,its not working...It shows no text on the Picture..Even i have changed the textcolor but still its not working..what should I do now...?
borntough
Try connecting the code to a button, and then click on the button to activate the code.
Try clicking on it twice.

When I tested the code, it seem to me that either the EnableWindow command or the ShowWindow was interfereing with the code.

Try removing those two commands when you test it.
void CPictureView::OnButtonPress()
{
    CEdit* Picture;
   Picture=(CEdit*)GetDlgItem(IDC_STATIC1);
   CDC * my_cdc = Picture->GetDC();
    if (!my_cdc) return;
    CString Msg = "Hello World!";
    my_cdc->TextOut(5, 10, Msg);
}
FYI:
If you want to paste the text in transparent mode, use SetBkMode function.
Example:
void CPictureView::OnButtonPress()
{
   CEdit* Picture;
  Picture=(CEdit*)GetDlgItem(IDC_STATIC1);
  CDC * my_cdc = Picture->GetDC();
   if (!my_cdc) return;
   CString Msg = "Hello World!";
   my_cdc->SetBkMode(TRANSPARENT);
   my_cdc->TextOut(5, 10, Msg);
}
Here is an api example of what you would want. If your bitmap is a resource. This example is shortened because you may wish to convert to MFC 6.0

void DoBitmapAndText( char *st)
{
HDC dc=GetDC(...);
HDC MemDC=CreateCompatibleDC(dc);
HBITMAP hb=LoadBitmap(...);
BITMAP bmp;GetObject(hb,sizeof(BITMAP),&bmp);
HBITMAP HOldBitmap=SelectObject(MemDC,hb);
BitBlt(dc,0,0,bmp.bmWidth,bmp.bmHeight,MemDC,0,0,SRCCOPY);
RECT rr;GetClientRect(...,&rr);
DrawText(dc,st,strlen(st),&rr,DT_SINGLELINE | DT_VCENTER | DT_CENTER);
//cleanup
ReleaseDC(...,dc);
SelectObject(MemDC,HOldBitmap);
DeleteDC(MemDC);
DeleteObject(hb);
}//endfunc
Hi Axter
I again tried a lot according to ur suggestion but still problem is there...I am writing my code here..The background color of my picture is black.I also tried to set the TextColor by using property of SetTextColor,but of no use.see it and tell me where i am going wrong..

void CPictureTryView::OnPicturesBitmap1()
{    
  CEdit* Picture;
     
   Picture=(CEdit*)GetDlgItem(IDC_STATIC1);
  CDC * my_cdc = Picture->GetDC();
   if (!my_cdc) return;
   CString Msg = "Hello World!";
   my_cdc->SetBkMode(TRANSPARENT);
   my_cdc->TextOut(20, 30, Msg);

}
borntough
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If that still doesn't work, you could try emailing me the project (david@axter.com).
Or just email me the class (*.h & *.cpp files).

HI
Thanks a lot...Its working now....gr8....But on second click on menu.y not on first click..If I want to do that,then what should I do..
Again thanks a lot for my help..
borntough
It works with one click if you're not trying to do ShowWindow (SW_SHOW) at the same time.
Example:
void CTextOutToBitmapDlg::OnButton2()
{
     CEdit* Picture;
     Picture=(CEdit*)GetDlgItem(IDC_STATIC1);
     CDC * my_cdc = Picture->GetDC();
     if (!my_cdc)
     {
          AfxMessageBox("Error:  Invalid CDC pointer!!!");
          return;
     }
     CString Msg = "Hello World!";
     //my_cdc->SetBkMode(TRANSPARENT);
     my_cdc->SetTextColor(RGB(0x00,0xFF,0x00));
     my_cdc->SetBkColor(RGB(0xFF,0x00,0xFF));
     my_cdc->TextOut(5, 10, Msg);
}

I'm not sure why adding the Picture->ShowWindow(SW_SHOW) code causes it to require double clicking.