2 Replies - 5859 Views - Last Post: 26 July 2002 - 12:53 AM Rate Topic: -----

#1 ooosawaddee3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 25-July 02

Ask About Fillrect() And Print Data In Text File O

Post icon  Posted 25 July 2002 - 10:20 PM

I want to print data in text file out to the paper
If my data in text file is

110011110

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()) ;
}

Is This A Good Question/Topic? 0
  • +

Replies To: Ask About Fillrect() And Print Data In Text File O

#2 dlkj  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 137
  • Joined: 11-March 02

Re: Ask About Fillrect() And Print Data In Text File O

Posted 25 July 2002 - 11:44 PM

ooosawaddee3, on Jul 29 2002, 02:20 PM, said:

// snip
CString testline = "101101001" ;
// snip
if ((testline.GetAt(pos)) == 1)
{
// snip
}

// fall through to '0' which just moves the position
else if ((testline.GetAt(pos)) == 0){
x += 2 ;
}

Hi,

Well, when you compare you have to be careful if you are comparing an ASCII character or a number. The ASCII character '1' has value 49 but the number 1 has value 1 (of course). So anyway, your code seems to be comparing with the number 1 where it should be comparing with the ASCII '1'. I know the next question you're going to ask is how to represent an ASCII '1'. The answer is to use the single quotes as I have been careful to so in this post. Use '1' instead of 1.

The second observation I'm going to make is that your comments seem to be incorrect. The comment says 'fall through' but really it doesn't. Remember to keep your comments updated or you may well lose your sanity.

Good luck
Was This Post Helpful? 0
  • +
  • -

#3 ooosawaddee3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 25-July 02

Re: Ask About Fillrect() And Print Data In Text File O

Posted 26 July 2002 - 12:53 AM

Thank you for your help dlkj.
I change code to this below.The printer print the blank page. Do you know what's wrong?

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;
}

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()) ;
}
------------------------------------------------------------------------------
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1