Solved

GetTextExtentPoint32 fails on Printer...

Posted on 1997-05-07
3
753 Views
Last Modified: 2013-12-04
If I call hdc = CreateCompatibleDC(hPrinterDC), assuming the printerDC is valid, and then call GetTextExtentPoint(hdc,...), this call crashes the computer everytime. GetTextExtentPoint32 also crashes, but I need to know the size (height and width) of a string for size it will be on the printer. This code works fine for the screen when I am not printing.

What am I doing wrong?

Jordan Pinsker
pinskerj@vu.union.edu
0
Comment
Question by:jordanp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
NickRepin earned 100 total points
ID: 1397156
CreateCompatibleDC() can only be used with devices that support raster operations. You can determine whether a printer supports these operations by calling the GetDeviceCaps(hPrinterDC,RASTERCAPS) function. Do you check hdc!=NULL?Also, why do not to use GetTextExtentPoint32(hPrinterDC)?See also MS knowledge base article Q22553.
0
 

Author Comment

by:jordanp
ID: 1397157
Your proposed answer helped a little but did not solve my overall problem. Maybe this will explain things better.

I am creating a bitmap based on the PrinterDC. I draw text to this bitmap to rotate it, and then draw the bitmap to a certain portion of the printed page. Thus, for this to work just as it does for my print preview, I need to create a comaptibleDC for the printer.

I used your suggestion and only changed the GetTextExtentPoint32 to use hPrinterDC, and created the bitmap by calling createcompatiblebitmap(hPrinterDC), but for whatever reason the text is becoming corrupted when printing (it appears that the bitmap is too large and it is printing garbage after the text). I am not sure if this is caused by the differences between the printer and the page as it works fine on screen. Here is my function below:

HANDLE GetRotateText (char * TextName, HWND TitleText, BOOL      left)
     {
     static LOGFONT lf ;
     HBITMAP        hBitmap ;
     HDC            hdc, hdcMem ;
     HFONT          hFont ;
     SIZE           size ;
     CHARFORMAT         CF;
        BITMAPINFOHEADER      bi;
      
       if (gPrinter == true) // we are using the printer, not screen
               SetMapMode(hPrinterDC,MM_TEXT);
      
     CF.cbSize = sizeof(CF);
        SendMessage (TitleText,EM_GETCHARFORMAT, 1, (LPARAM)&CF);

     hdc = CreateIC ("DISPLAY", nil, nil, nil);
     hdcMem = CreateCompatibleDC (hdc);
     
     lf.lfUnderline = (CF.dwEffects & CFE_UNDERLINE);
     lf.lfStrikeOut =  (CF.dwEffects & CFE_STRIKEOUT);
     if (CF.dwEffects & CFE_BOLD)
       lf.lfWeight = 700;
     else
       lf.lfWeight = 400;
       lf.lfItalic = (CF.dwEffects & CFE_ITALIC);

            // Get the size of the text...
       if (false == gPrinter)
             lf.lfHeight = -MulDiv(CF.yHeight/20, GetDeviceCaps(hdcMem,
LOGPIXELSY), 72);
       else
             lf.lfHeight = -MulDiv(CF.yHeight/20, GetDeviceCaps(hPrinterDC,
LOGPIXELSY), 72);

     lstrcpy ((char *) lf.lfFaceName, CF.szFaceName) ;
         
 hFont = (HFONT) SelectObject (hdcMem, CreateFontIndirect (&lf));
     if (false == gPrinter)
           {
                 GetTextExtentPoint32 (hdcMem, TextName, strlen (TextName), &size);
                hBitmap = CreateCompatibleBitmap (hdcMem, size.cy, size.cx) ;//switch x,y for rotation...
            }
     else
           {
                 GetTextExtentPoint32 (hPrinterDC, TextName, strlen (TextName),
&size);
                  hBitmap = CreateCompatibleBitmap (hPrinterDC, size.cy, size.cx) ;//switch x,y for rotation...
            }
         
     if (left)
     lf.lfEscapement = lf.lfOrientation = 900 ;//90 degrees
     else
     lf.lfEscapement = lf.lfOrientation = 2700 ;//270 degrees
   
     SelectObject (hdcMem, hBitmap) ;
       hFont = (HFONT) SelectObject (hdcMem, CreateFontIndirect (&lf));

       if (left)
             TextOut (hdcMem, 0,size.cx, TextName, strlen (TextName));
       else
        TextOut (hdcMem, size.cy,0, TextName, strlen (TextName))      
     
DeleteObject (SelectObject (hdcMem, hFont)) ;
     DeleteDC (hdcMem) ;
     DeleteDC (hdc) ;
      
       if (gPrinter == true) // return to twips for the printer...
               SetMapMode(hPrinterDC,MM_TWIPS);
      
 // Now convert the bitmap to a dib and return it...
       bi.biSize = sizeof(BITMAPINFOHEADER);
       bi.biWidth = size.cy;
     bi.biHeight = size.cx;
     bi.biPlanes = 1;
     bi.biBitCount = 16;
     bi.biCompression = BI_RGB;
     bi.biSizeImage = 0;
     bi.biXPelsPerMeter = 0;
     bi.biYPelsPerMeter = 0;
     bi.biClrUsed = 0;
     bi.biClrImportant = 0;
      
       return (DibFromBitmap (hBitmap,BI_RGB,32,CreateBIPalette (&bi)));

           // must dipose of old bitmap before return hBitmap; not done yet
     }



0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1397158
It seems that you use Win95, don't you?
There is no problem to rotate text in WinNT.

As to your code,

1) you should select font on printerDC to call GetTextExtentPoint:

   hFont = (HFONT) SelectObject (hdcMem, CreateFontIndirect (&lf));
   hPrinterDC = (HFONT) SelectObject (hPrinterDC, CreateFontIndirect (&lf));
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   if (false == gPrinter) {
      GetTextExtentPoint32 (hdcMem, TextName, strlen (TextName), &size);
      hBitmap = CreateCompatibleBitmap (hdcMem, size.cy, size.cx) ;//switch x,y for rotation...
   }
   else {
      GetTextExtentPoint32 (hPrinterDC, TextName, strlen (TextName),&size);
      hBitmap = CreateCompatibleBitmap (hPrinterDC, size.cy, size.cx) ;//switch x,y for rotation...
   }

2) in my opinion, you should swap lines with TextOut in following code:

   if (left)
      // 90 degrees, from top to bottom
      TextOut (hdcMem, 0,size.cx, TextName, strlen (TextName));
   else
      // 270 degrees, from bottom to top
      TextOut (hdcMem, size.cy,0, TextName, strlen (TextName));

3) why do not to print directly to hPrinterDC (or to screen) by TextOut  with lf.lfEscapement=lf.lfOrientation != 0 ?

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question