jordanp
asked on
GetTextExtentPoint32 fails on Printer...
If I call hdc = CreateCompatibleDC(hPrinte rDC), 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
What am I doing wrong?
Jordan Pinsker
pinskerj@vu.union.edu
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.lfOrien tation != 0 ?
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.lfOrien
ASKER
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(hPr
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_T
CF.cbSize = sizeof(CF);
SendMessage (TitleText,EM_GETCHARFORMA
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_T
// 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,CreateB
// must dipose of old bitmap before return hBitmap; not done yet
}