Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I want to print data in text file out to the paper
If my data in text file is110011110
I want to change 1 in the above data to solid black (which have 2 pixel width and 4 pixel height) and 0 in the above data to solid white (which have 2 pixel width and 4 pixel height)
This below is code but it does not work. The printer do not print anything.What wrong with my code?
void CTestprint::OnPrintButton()
{
DOCINFO di ;
CPrintInfo printInfo ;CDC dc ;
CRect draw_area ;
CPrintDialog dlg(FALSE) ;if (dlg.DoModal() == IDCANCEL)
return ;HDC hDC = dlg.GetPrinterDC() ;
if (hDC == NULL)
return ;di.cbSize = sizeof(DOCINFO) ;
di.lpszDocName = "Document name";
di.lpszOutput = NULL ;// prepare the print information structure
dc.Attach(hDC) ;
printInfo.m_bDirect = TRUE ;
printInfo.m_rectDraw.left = 0 ;
printInfo.m_rectDraw.right = dc.GetDeviceCaps(HORZRES) ;
printInfo.m_rectDraw.top = 0 ;
printInfo.m_rectDraw.bottom = dc.GetDeviceCaps(VERTRES) ;
draw_area = printInfo.m_rectDraw ;dc.StartDoc(&di) ;
CString testline = "101101001" ;
int x = 0 ;
int y = 0 ;
int pos = 0 ;// start printing the document
dc.StartPage() ;
while (pos < testline.GetLength())
{
if ((testline.GetAt(pos)) == 1)
{
CBrush brush;// Creation of the brush with a black color
brush.CreateSolidBrush(RGB(0, 0, 0));
// Create a rectangle with coordinates x,y,x+2,y+4 corresponding with top, left, bottom, right
CRect Rectangle(x, y, x + 2, y + 4) ;// Fill a rectangle in the current device context or DC
dc.FillRect(&Rectangle , &brush);x += 2;
}// fall through to '0' which just moves the position
else if ((testline.GetAt(pos)) == 0){
x += 2 ;
}
pos++ ;
}
y += 4 ;
x = 0 ;
pos = 0 ;dc.EndPage() ;
printInfo.m_rectDraw = draw_area ;dc.EndDoc() ;
VERIFY(dc.DeleteDC()) ;
}

I haven't gone through this thoroughly, but you are comparing the characters '0' and '1' to the integers 0 and 1, after calling testline.GetAt(pos). It's a common mishap, because characters are integers too, so no error occurs. Post back if that doesn't resolve it.
Cheers

![]() |
OM19.dll
|
Executing PHP on Server
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |