Solved

How do I print things in eVB 3.0 ?

Posted on 2004-08-13
123
363 Views
Last Modified: 2007-12-19
I have a app which displays items from a DB in a ListView control.

What I need, is to be able to print these out to my portable printer. The type of printer is a Datecs PP-55. (The type where the PDA sits inside of it). I have installed all the drivers etc and can communicate with the printer via the following code, but can't do anything else:

Dim DC as long

Private Print_Items()
   DC = GetPrinterDC(True)

   'What could I put in here ???

   DeleteDC (DC)
End Sub

What I want to do is print out the data inside of the ListView box (i.e Like a table format). I also want be able to draw a rectangle at the top of the page so I can put my company name inside of it.

Thanks in advance.
0
Comment
Question by:markh51
  • 82
  • 39
  • 2
123 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 11794030
0
 

Author Comment

by:markh51
ID: 11794289
I'm not using PrinterCE so the code won't work.

I need to now how to use thing like StartDoc and how to draw a rectangle to the printer like I explained above.
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 11794341
Try the function
TextOut(hdc, 0,0, "Test Printing", lstrlen( "Test Printing" ) )
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11794343

If you have installed the drivers then just use the Printer object to print to it just like you would to print to any printing device that has drivers.

0
 
LVL 7

Expert Comment

by:Enlade
ID: 11794423

For instance if you want to print a few lines of text to a printing device you might do something like this:

Printer.Font = "Arial"
Printer.FontSize = "10"
Printer.CurrentX = 0
Printer.CurrentY = 0
Printer.Print "This is a test to see it print to my printing device that is all."
Printer.Print "This is the end of the test."
Printer.EndDoc

You can also print graphics and whatever you want or even use the DC to print directly (as supplied with the Printer object).  Is there a reason you can't use the Printer object?

Mind you the above prints to the default printer, but it is easy enough to print to any printer using the same methods.  To test it you should make this printer your default printer and then try the above code.
0
 

Author Comment

by:markh51
ID: 11794467
Thanks for that...

I have worked out how to print simple lines of text to the printer, but how do you print text inside of a box/rectangle and things like that ?

Also I want to lay my data out so that it is formatted like a table, is there any way of doing that.

What do you mean by the printer object ?

Please note that I am new to programming in VB ;)

Thanks.
0
 

Author Comment

by:markh51
ID: 11794562
Enlade...

I can't get any of your code to work.


Mikal613:

You code returns a "Variable undefined: textout" error.


Sorry to be a pain but you'll have to explain in more detail.

0
 
LVL 7

Expert Comment

by:Enlade
ID: 11795354

What happens when you run the code?  Any errors?  Does it do anything?  Did you make sure your printer is set to be your default windows printer?

Can you print to this printer from Word or Notepad?
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11795412

Oh, I'm sorry I didn't see the "e" in front of the VB in your question.  None of that Printer stuff will work.  Sorry.
0
 

Author Comment

by:markh51
ID: 11795439
The PDA does not have notepad or word on it.

Yes I can print to the printer, but just simple lines by using the code which came with the printer.

This is what it say's in the Printer SDK:

1. call GetPrinterDC and check returned handle. Remember this handle to use it later in other function calls. None of the other printing functions will work if this call returns 0.

2. call StartDoc(dc, doc_info) and check returned code for positive value

3. call StartPage(dc) and check returned code for positive value

4. draw what you want to the dc - text and/or graphic using system functions Rectangle, Ellipse, DrawText, BitBlt, and so on ... any supported by WinCE

5. call EndPage(dc) and check returned code for positive value

6. repeat steps 3, 4, 5 for every page you want to print

7. call EndDoc(dc) and check returned code for positive value

Steps one works OK, as when I run the code the printer beeps and turns on, however I don't know how to use step 2, so this probaly where I am going wrong. I have looked in the eVB help file but I don't understand it.

This is the code I have so far which works:

Dim DC As Long

DC = GetPrinterDC(True)

If DC > 0 Then
   ' Print some things
Else
   ' Show error message
End If

DeleteDC (DC)

0
 
LVL 7

Expert Comment

by:Enlade
ID: 11795466

Doesn't look like printing is supported without PrinterCE.  Check out this link for details.  Not sure if it is helpful though.

http://support.microsoft.com/default.aspx?scid=kb;en-us;Q265765
0
 

Author Comment

by:markh51
ID: 11795537
Printing works fine on the PDA with the printer but I just can't figure out how to use some of the commands such as StartDoc.

Any ideas ?
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11795686

I see, you must be using an SDK that was provided by Datecs.  I notice on their website that they do provide a number of different SDK's that might be useful.  Though I must say they don't make it easy to find information about that kind of stuff on thier website.  Basically, eVB does not provide you with any Printer objects that will allow you to access the printer in a WYSIWYG way.  It only allows you to stream text to the printer and that won't help you (unless you want to write your own SDK using the printers control characters).  So, you need to have some SDK that will add such functionallity to eVB.  PrinterCE will do this, but I think that Datecs has already provided you with an SDK that will allow you to send WYSIWYG information to the printer.  Your problem is that you either have the wrong version of that SDK (since there are multiple version referenced on the Datecs website) or you just don't have enough documentation on how to use the SDK that Datecs provided (which I don't doubt since I can't seem to find much on their website on the subject).

Maybe someone else that has had some experiance with the Datecs SDK for the P-55 printer on PocketPC (WinCE) can help you out; because, I can't be of much help on the Datecs SDK's.  Still, I will look through their website again and see if I can find any additional information for you.  In the meantime, maybe you could give Datecs a call and see if they have anyone that could answer your questions.  I think that that is probably going to be your only solution if they don't have the information listed on thier site.  Sorry.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11795765

Here is something.  But it doesn't have any documentation.  Let me download it and see if it has any included in the download.

http://pda.datecs.bg/product.html?pda=PocketPC&page=Software&product_id=17
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11795816

Yea, maybe that is what you want to look at.  It has the SDK that looks about right and you can try out the sample code that they provided to see if it works.  It seems like they have a number of SDK's for the P-50 printer.  They don't talk much about the P-55, but I'm assuming that it should work with all the same software as the P-50.  Though maybe that is part of the problem.

Mind you there are other SDK's that Datecs refer to on their website.  They even have some other third party SDK's that you can try that might also help.  But start with this one above and see if it does anything.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11795850

Download the SDK above and read the information in the provided HTML file (VB_SDK_Help.html).  It gives you a pretty reasonable set of instructions for use.  Note however that the SDK refers to the P-50 and not the P-55.  I'm assuming that that is not a problem; however, it might be if the P-55 is a lower grade printer then the P-50 (doesn't have as much functionality).  Anyway, check it out and tell me what you think.

0
 
LVL 7

Expert Comment

by:Enlade
ID: 11795943

So, once you get this SDK it seems like you could use GetPrinterDC and then print through the DC or you could do it by using the built in SDK commands like PrintText or PrintGraphic or whatever.  So if you wanted to print a line of text to the printer you would do something like:

If Not PrintText ("Hello World!!!", 18, 0, False, False, False, False) Then
  MessageBox("Fail to print text! Check the printer connection and paper.")
  Exit
End If

Its all in the HTML file.  Check it out.
0
 

Author Comment

by:markh51
ID: 11796326
I have the new and up to date SDK from Datecs as it was provided with the printer, but I don't really understand the HTML help file.

I can use the PrintText function like you did above, but it is not much use as I need to be able to display the records which are returned from the DB in a table format.

I have tried to email DATECS for help but they say that it is all in the Help file and not to waste their time !

How would I do what I need to do using the SDK provided ?
0
 

Author Comment

by:markh51
ID: 11796337
Thanks for your efforts, I appreciate it.

I will extend your points if you can solve the problem :)
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11802389

Well, this is good news.  It seems like the SDK is working for you with that P-55.  I was a little worried that it would only work with the P-50 (as the documentation says).

If the SDK is working (and the function PrintText is working) then it should be easy enough to get your data printing in columns to the printer.  The data you read from your DB is just Strings, or Longs, or Singles, or whatever.  So, it doesn't matter much that the data comes from a DB record.  You read the record into your program (or into a control in your program) and that makes it accessable to you for printing.  So its just a matter of using the X, Y part of the PrintText function to position the data in columns (table) like you want.  I could probably do this for you if you need help with it but I will need more information from you to do so.

Can you give me an example of a record that is in your DB that you want to print?  And then can you show me an example of how you want it to look on the printer?  With that I can use the X, Y portion of the PrintText function to position that data on the printer for you.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11802432

Oh, I just looked at the PrintText function and it doesn't have an X, Y position.  So you will need to use get the DC using GetPrinterDC and then use DrawText or DrawPageText or any of the other DC related function that they do not document (darn) in the HTML file.  Since I don't have the printer here I can only guess at some test code you could try.  Let me write up some simple test so we can try to figure out how those functions work.  Give me a sec.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11802500

Ok, I didn't test this or anything and eVB might not give you access to the TextOut API.  So this might not work and then we will have to try using DrawText instead.  But it would really be helpful if the TextOut worked so its worth trying.

The other problem is that we don't know what units the printer is using for positioning (Twips, Pixels, Points, etc).  It is probably Twips.  I used a small position (50 and 200) so that we can get some idea of what units the printer is using as well as testing out the TextOut function.  So when this prints it will probably print the BB very close to (or even on top) the AA.  Don't worry about that.  All we are looking for is that the AA comes first on the line and then the BB fallows (or is on top of the second A or something).  And that the AA and the BB are both on the same line.  If that happens then we are good to go.  Mind you you will probably need some other functions (like to be able to measure the width of a string or something; and/or to set the printer fonts, etc...).

Anyway, create this little sample code.  Put the Declare somewhere at the beginning of the code and then put the code inside a function or in the main or whereever you want.  Just so long as it runs.  Try it out.

Public Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long



And put this code somewhere that will run (in the main form Load or in a Main function in some module or in some function that is called by pressing a button or whereever).


Dim dc as long
Dim result as Long
Dim stTemp as String

dc = GetPrinterDC(True)
If dc = 0 Then Exit Function

stTemp = "AA"
result = TextOut(dc, 50, 50, stTemp, Len(stTemp))

stTemp = "BB"
result = TextOut(dc, 200, 200, stTemp, Len(stTemp))

EndDoc dc
DeleteDC dc
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11802502

Mind you in the above sample code I am looking for the TextOut API in "gdi32".  That is where it is for VB proper.  It might be in a different place for eVB (or it might not exist).  So, we might still have a problem trying to locate the TextOut API function in eVB.  Still, lets start by looking for it in "gdi32" and see what happens.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11802507

Seems like Datecs has all of its API functions in "coredll".  So maybe we should try that to see if it also contains the TextOut function.  So, maybe use this code after you try using the above code (API part).

Public Declare Function TextOut Lib "coredll" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long

0
 
LVL 7

Expert Comment

by:Enlade
ID: 11802531

Ok try this sample code as well.  I'm trying to figure out how we might make use of the DrawText function (as well as trying to figure out the units of the printer).  Lets call this the second text (Test 2) and the test above lets call the first test (Test 1).  Try them and then tell me what each prints (or doesn't print).

Anyway, try this code as well (Test 2).

Dim dc as long
Dim result as Long
Dim stTemp as String
Dim Modified As String

dc = GetPrinterDC(True)
If dc = 0 Then Exit Function

stTemp = "AA"
Modified = doRect(50, 50, 5000, 5000)
result = DrawText(dc, stTemp, -1, Modified, DRAW_TEXT_CALC)

stTemp = "BB"
Modified = doRect(200, 200, 5000, 5000)
result = DrawText(dc, stTemp, -1, Modified, DRAW_TEXT_CALC)

EndDoc dc
DeleteDC dc
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11802532

Remember that both tests need the Printer.Bas file that Datecs provides you.  I'm sure you know that already, but I thought I would mention it.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11802573

Also try using DRAW_TEXT_DRAW instead of DRAW_TEXT_CALC.  They have hard coded the draw properties and we probably want to set up our own properties, but we just want to get something printing that we can learn from.  So, lets use try both of their hard coded draw properties for the DrawText function (just to test it).

0
 
LVL 7

Expert Comment

by:Enlade
ID: 11802590

I think that the DrawText function uses the Pixel unit.  So if you know the DPI of your printer then that will be the units.  For instance, if your printer prints at 300 DPI quality then we will use 300 to indicate an inch.  Then we can use that information to set up the columns and/or areas that you want the data to be printed into.  So, maybe find out the DPI for your printer while you are testing things out.
0
 

Author Comment

by:markh51
ID: 11803831
Thanks for that !

eVB does not seem to use the TextOut API but your test 2 just draws a big rectangle filled in black !!!

I am not too sure what the DPI of the print is ? 203 DPI ?

Anyway, I want it to print the data from the database like:

DATE          C1         C2          C3         C4
11/08/04     DATA     DATA     DATA     DATA
12/08/04     DATA     DATA     DATA     DATA
13/08/04     DATA     DATA     DATA     DATA

you get the idea, there is 5 columns and the rest is completed via looping.

Thanks for your time.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11804272

Well, its actually good that it is drawing a rectangle.  Lets focus on that second test.  First off, change all those 5000's to something more reasonable.  Try 400 instead of 5000 for each of the 5000's in the code.

Also, once you run the code with the above change can you measure how far over and down from the top of the page that the upper left corner of the rectangle starts (just over if down is too hard to figure out).  That will give me some idea of the units on the printer.

Also, do you get 2 rectangles or just one?

Also, could you try using DRAW_TEXT_DRAW instead of DRAW_TEXT_CALC and tell me what it does?

Try those 4 things and that might give us something to work with.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11804291

While you are at it lets create a third test.  Lets see if we can stop the rectangle from filling in itself with black.  Lets try this:


Dim dc as long
Dim result as Long
Dim stTemp as String
Dim Modified As String

dc = GetPrinterDC(True)
If dc = 0 Then Exit Function

result = SetBkMode(dc, TRANSPARENT)
result = SetTextColor(dc, 0)

stTemp = "AA"
Modified = doRect(50, 50, 5000, 5000)
result = DrawText(dc, stTemp, -1, Modified, DRAW_TEXT_CALC)

stTemp = "BB"
Modified = doRect(200, 200, 5000, 5000)
result = DrawText(dc, stTemp, -1, Modified, DRAW_TEXT_CALC)

EndDoc dc
DeleteDC dc
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11804293

Opps, I forgot we wanted to use a smaller rectangle (changing the 5000).  Try this instead for test 3.

Dim dc as long
Dim result as Long
Dim stTemp as String
Dim Modified As String

dc = GetPrinterDC(True)
If dc = 0 Then Exit Function

result = SetBkMode(dc, TRANSPARENT)
result = SetTextColor(dc, 0)

stTemp = "AA"
Modified = doRect(50, 50, 250, 250)
result = DrawText(dc, stTemp, -1, Modified, DRAW_TEXT_CALC)

stTemp = "BB"
Modified = doRect(300, 300, 500, 500)
result = DrawText(dc, stTemp, -1, Modified, DRAW_TEXT_CALC)

EndDoc dc
DeleteDC dc
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11804301

Yea, lets try that instead.  Again, whatever prints we want to measure with a ruler to find out where the very upper/left corner is relitive to the top/left corner of the paper.  So if it prints two filled in rectangles then measure to the upper/left corner of each rectangle.  If it prints "AA" and "BB" them measure to the upper/left part of the first "A" and the upper/left part of the first "B".  Then we can nail down the units.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11804321

I think you are right about that 203 DPI.  If so then your measurements from the left side of the paper should be approximitly  0.25" and 1.25".  Hopefully it is now printing "AA" and "BB" instead of black rectangles.  Give it a try.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11804338

Also, try the above code with DRAW_TEXT_DRAW instead of DRAW_TEXT_CALC.  Like I said before, we will probably create our own set of properties instead of useing their hard coded ones; however, they do have those two hard coded so we might as well try both.  I think that the only difference is that the DRAW_TEXT_CALC will wrap the text into the region (rectangle) that you are printing and the DRAW_TEXT_DRAW will not wrap the text.  Any text that you try to print outside the region (rectangle) will be truncated (not viewable).  We will probably want to use something like DRAW_TEXT_CALC because i don't see any functions that will allow us to measure the height and/or width of the string we are printing (in Pixels).  There is a need to know the size of things prior to trying to print them to the printer, but I won't go into that yet.  Lets just get the thing printing first and then we can figure out some method of lining things up in columns.
0
 

Author Comment

by:markh51
ID: 11804576
OK, just done your test and it prints exactly the same... just one big rectangle, filled with black. The rectangle is centered on the paper with about .25 inch either side.
0
 

Author Comment

by:markh51
ID: 11804601
Just had a look at the eVB documentation for the DrawText fuction, and it says "This method draws text in a PictureBox control"

Will it still work for what you are trying to do ?
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11805089

I think that you read something that is telling you that you can use the DrawText function to draw text inside of a PictureBox control.  But that is just one use for that function.  It can be used to draw text into anything with a DC.

Its good that the rectangle is .25" from the boarder; however, its not good that the rectangle is black.  Let me see if I can work out why that is.  Hold up a sec.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11805179

Lets add in the font selection.  I don't think this should matter, but lets try it anyways.  Try this code:


Dim dc as long
Dim result as Long
Dim stTemp as String
Dim Modified As String
Dim points As Long
Dim font As Long
Dim fontType As String
Dim oldFont As Long

dc = GetPrinterDC(True)
If dc = 0 Then Exit Function

points = 18
fontType = LongToMemoryString(-(points * GetDeviceCaps(dc, LOGPIXELSY)) / 72)  'font size
fontType = fontType & LongToMemoryString(0)    'appropriate width
fontType = fontType & LongToMemoryString(0)    'no escapment
fontType = fontType & LongToMemoryString(0)    'no orientation
fontType = fontType & LongToMemoryString(400)  'some weight THIN=100,NORMAL=400,BOLD=700
fontType = fontType & ByteToMemoryString(0)    'italic
fontType = fontType & ByteToMemoryString(0)    'underline
fontType = fontType & ByteToMemoryString(0)    'strikeout
fontType = fontType & ByteToMemoryString(0)    'default charset
fontType = fontType & ByteToMemoryString(0)    'default out precision
fontType = fontType & ByteToMemoryString(0)    'default clip precision
fontType = fontType & ByteToMemoryString(0)    'default quality
fontType = fontType & ByteToMemoryString(0)    'default pitch
fontType = fontType & "Tahoma"                 'most popular and existing TTF

font = CreateFontIndirect(fontType)

oldFont = SelectObject(dc, font)

result = SetBkMode(dc, TRANSPARENT)
result = SetTextColor(dc, 0)

stTemp = "AA"
Modified = doRect(50, 50, 250, 250)
result = DrawText(dc, stTemp, -1, Modified, DRAW_TEXT_CALC)

stTemp = "BB"
Modified = doRect(300, 300, 500, 500)
result = DrawText(dc, stTemp, -1, Modified, DRAW_TEXT_CALC)

EndDoc dc
Result = SelectObject(dc, oldFont)
DeleteDC dc
DeleteObject (font)
0
 

Author Comment

by:markh51
ID: 11805195
Exactly the same mate :(
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11805262

hmmmmm....this is getting tough.  Lets try this:

Dim dc as long
Dim result as Long
Dim stTemp as String
Dim Modified As String
Dim points As Long
Dim font As Long
Dim fontType As String
Dim oldFont As Long
Dim tempVal as Long

dc = GetPrinterDC(True)
If dc = 0 Then Exit Function

points = 18
fontType = LongToMemoryString(-(points * GetDeviceCaps(dc, LOGPIXELSY)) / 72)  'font size
fontType = fontType & LongToMemoryString(0)    'appropriate width
fontType = fontType & LongToMemoryString(0)    'no escapment
fontType = fontType & LongToMemoryString(0)    'no orientation
fontType = fontType & LongToMemoryString(400)  'some weight THIN=100,NORMAL=400,BOLD=700
fontType = fontType & ByteToMemoryString(0)    'italic
fontType = fontType & ByteToMemoryString(0)    'underline
fontType = fontType & ByteToMemoryString(0)    'strikeout
fontType = fontType & ByteToMemoryString(0)    'default charset
fontType = fontType & ByteToMemoryString(0)    'default out precision
fontType = fontType & ByteToMemoryString(0)    'default clip precision
fontType = fontType & ByteToMemoryString(0)    'default quality
fontType = fontType & ByteToMemoryString(0)    'default pitch
fontType = fontType & "Tahoma"                 'most popular and existing TTF

font = CreateFontIndirect(fontType)

oldFont = SelectObject(dc, font)

result = SetBkMode(dc, TRANSPARENT)
result = SetTextColor(dc, 0)

tempVal = LongToMemoryString(5 * 4) & _
          LongToMemoryString(0) & _
          LongToMemoryString(0) & _
          LongToMemoryString(0) & _
          LongToMemoryString(0)
StartDoc dc, tempVal

stTemp = "AA"
Modified = doRect(50, 50, 250, 250)
result = DrawText(dc, stTemp, -1, Modified, DRAW_TEXT_DRAW)

stTemp = "BB"
Modified = doRect(300, 300, 500, 500)
result = DrawText(dc, stTemp, -1, Modified, DRAW_TEXT_DRAW)

EndDoc dc
SelectObject dc, oldFont
DeleteDC dc
DeleteObject font
0
 

Author Comment

by:markh51
ID: 11805283
I've just tried:

stTemp = "AA"
Modified = doRect(5, 5, 10, 10)
Result = DrawText(dc, stTemp, -1, Modified, DRAW_TEXT_CALC)

and it prints exactly the same size rectangle as the other tests which is 2"x3".

STRANGE !

I'll try you test above and let you know.
0
 

Author Comment

by:markh51
ID: 11805292
nope, no change... exactly the same.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11805301

But when you use PrintText it works right?
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11805309

I mean, didn't we try something like:

If Not PrintText ("Hello World!!!", 18, 0, False, False, False, False) Then
  MessageBox("Fail to print text! Check the printer connection and paper.")
  Exit
End If

and it printed.  I know it doesn't help us to use PrintText, but PrintText is doing nearly the same thing I'm doing above.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11805311

I suppose we could modify PrintText and see what it will do for us.  Let me try that.
0
 

Author Comment

by:markh51
ID: 11805336
The PrintText function which comes with the DATECS SDK, works fine.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11805354

Hmmm....the only thing that I did that was different was to call the StartDoc before I called the DrawText function.  I can't understand why they call it after.  But lets try it their way.  Try this:

Dim dc as long
Dim result as Long
Dim stTemp as String
Dim Modified As String
Dim points As Long
Dim font As Long
Dim fontType As String
Dim oldFont As Long
Dim tempVal as Long

dc = GetPrinterDC(True)
If dc = 0 Then Exit Function

points = 18
fontType = LongToMemoryString(-(points * GetDeviceCaps(dc, LOGPIXELSY)) / 72)  'font size
fontType = fontType & LongToMemoryString(0)    'appropriate width
fontType = fontType & LongToMemoryString(0)    'no escapment
fontType = fontType & LongToMemoryString(0)    'no orientation
fontType = fontType & LongToMemoryString(400)  'some weight THIN=100,NORMAL=400,BOLD=700
fontType = fontType & ByteToMemoryString(0)    'italic
fontType = fontType & ByteToMemoryString(0)    'underline
fontType = fontType & ByteToMemoryString(0)    'strikeout
fontType = fontType & ByteToMemoryString(0)    'default charset
fontType = fontType & ByteToMemoryString(0)    'default out precision
fontType = fontType & ByteToMemoryString(0)    'default clip precision
fontType = fontType & ByteToMemoryString(0)    'default quality
fontType = fontType & ByteToMemoryString(0)    'default pitch
fontType = fontType & "Tahoma"                 'most popular and existing TTF

font = CreateFontIndirect(fontType)

oldFont = SelectObject(dc, font)

result = SetBkMode(dc, TRANSPARENT)
result = SetTextColor(dc, 0)

stTemp = "AA"
Modified = doRect(50, 50, 250, 250)
result = DrawText(dc, stTemp, -1, Modified, DRAW_TEXT_DRAW)

tempVal = LongToMemoryString(5 * 4) & _
          LongToMemoryString(0) & _
          LongToMemoryString(0) & _
          LongToMemoryString(0) & _
          LongToMemoryString(0)
StartDoc dc, tempVal

EndDoc dc
SelectObject dc, oldFont
DeleteDC dc
DeleteObject font
0
 

Author Comment

by:markh51
ID: 11805412
Sorry to say this... but it prints the same crap. If you can't work it out I'll have to just use the PrintText function somehow.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11805505

Oh, maybe tempVal needs to be a String and/or maybe some other little things that I changed in this code.  Try it and see what happens:

Dim dc as long
Dim result as Long
Dim stTemp as String
Dim Modified As String
Dim points As Long
Dim font As Long
Dim fontType As String
Dim oldFont As Long
Dim tempVal as String

dc = GetPrinterDC(True)

points = 18
fontType = LongToMemoryString(-(points * GetDeviceCaps(dc, LOGPIXELSY)) / 72)  'font size
fontType = fontType & LongToMemoryString(0)    'appropriate width
fontType = fontType & LongToMemoryString(0)    'no escapment
fontType = fontType & LongToMemoryString(0)    'no orientation
fontType = fontType & LongToMemoryString(400)  'some weight THIN=100,NORMAL=400,BOLD=700
fontType = fontType & ByteToMemoryString(0)    'italic
fontType = fontType & ByteToMemoryString(0)    'underline
fontType = fontType & ByteToMemoryString(0)    'strikeout
fontType = fontType & ByteToMemoryString(0)    'default charset
fontType = fontType & ByteToMemoryString(0)    'default out precision
fontType = fontType & ByteToMemoryString(0)    'default clip precision
fontType = fontType & ByteToMemoryString(0)    'default quality
fontType = fontType & ByteToMemoryString(0)    'default pitch
fontType = fontType & "Tahoma"                 'most popular and existing TTF

font = CreateFontIndirect(fontType)

oldFont = SelectObject(dc, font)

result = SetBkMode(dc, TRANSPARENT)
result = SetTextColor(dc, 0)

stTemp = "AA"
Modified = doRect(50, 50, 250, 250)
result = DrawText(dc, stTemp, -1, Modified, DRAW_TEXT_DRAW)

tempVal = LongToMemoryString(5 * 4) & _
          LongToMemoryString(0) & _
          LongToMemoryString(0) & _
          LongToMemoryString(0) & _
          LongToMemoryString(0)
StartDoc (dc, tempVal)

EndDoc (dc)
SelectObject (dc, oldFont)
DeleteDC (dc)
DeleteObject (font)
0
 

Author Comment

by:markh51
ID: 11805632
Sorry, just the same...

It looks like we're wasting our time with this one :(
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11805633

Ok, if the above didn't work then lets try to solve this from another approuch.  Lets start with thier PrintText function and then see if we can modify it to get it to work for us.  Lets create a function called MyPrintText.  You can call the function like this:

If Not MyPrintText ("AA", 18, 0, False, False, False, False, 50, 50, 300, 300) Then
  MessageBox("Fail to print text! Check the printer connection and paper.")
  Exit
End If

Initially we will not use the X1, Y1, X2, and Y2 values.  Lets start by just doing what the original PrintText function does (minus the page related stuff).  Try this and tell me what happens.

Public Function MyPrintText(ByVal text As String, ByVal points As Long, ByVal clr As Long, _
    ByVal isBold As Boolean, ByVal isItalic As Boolean, ByVal isStrike As Boolean, _
    ByVal isUnderline As Boolean, _
    ByVal X1 As Long, ByVal Y1 As Long, _
    ByVal X2 As Long, ByVal Y2 As Long) As Boolean
  PrintText = False
  On Error Resume Next ' for DEBUGGING purposes comment it
 
  Dim font, weight As Long
  Dim dc As Long
 
  dc = GetPrinterDC(True)
  If dc = 0 Then Exit Function
 
  If isBold Then weight = 700 Else weight = 400
  Dim Help As String
  Help = LongToMemoryString(-(points * GetDeviceCaps(dc, LOGPIXELSY)) / 72) ' font size
  Help = Help & LongToMemoryString(0) ' appropriate width
  Help = Help & LongToMemoryString(0) ' no escapment
  Help = Help & LongToMemoryString(0) ' no orientation
  Help = Help & LongToMemoryString(weight) ' some weight THIN=100,NORMAL=400,BOLD=700
  Help = Help & ByteToMemoryString(isItalic) ' italic
  Help = Help & ByteToMemoryString(isUnderline) ' underline
  Help = Help & ByteToMemoryString(isStrike) ' strikeout
  Help = Help & ByteToMemoryString(0) ' default charset
  Help = Help & ByteToMemoryString(0) ' default out precision
  Help = Help & ByteToMemoryString(0) ' default clip precision
  Help = Help & ByteToMemoryString(0) ' default quality
  Help = Help & ByteToMemoryString(0) ' default pitch
  Help = Help & "Tahoma" ' most popular and existing TTF
  font = CreateFontIndirect(Help)
 
  If font = 0 Then
    DeleteDC (dc)
    Exit Function
  End If
 
  ' Prepare DC
  Dim oldFont As Long
  oldFont = SelectObject(dc, font)
  Dim Result As Long
  Result = SetBkMode(dc, TRANSPARENT)
  Result = SetTextColor(dc, clr)  ' 0 for black
 
  Dim PageLeft, PageRight, PageTop, PageBottom, CalcRight, CalcBottom As Long
  PageLeft = 0
  PageTop = 0
  PageRight = pageWidth
  PageBottom = pageHeight
  Dim Modified As String
  Modified = doRect(PageLeft, PageTop, PageRight, PageBottom)
  Result = DrawText(dc, text, -1, Modified, DRAW_TEXT_CALC)
  Call getRect(Modified, PageLeft, PageTop, CalcRight, CalcBottom)
 
  Help = LongToMemoryString(5 * 4) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0)
  If StartDoc(dc, Help) <= 0 Then
    Result = SelectObject(dc, oldFont)
    DeleteDC (dc)
    DeleteObject (font)
    Exit Function
  End If
   
  If EndDoc(dc) <= 0 Then
    Result = SelectObject(dc, oldFont)
    DeleteDC (dc)
    DeleteObject (font)
    Exit Function
  End If

  ' finally - cleanup
  Result = SelectObject(dc, oldFont)
  DeleteDC (dc)
  DeleteObject (font)
  PrintText = True
End Function
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11805662

Oh....I think I see.  Hold up on that last test.  I need to change it.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11805712

Ok, try this function instead.  I really think we can do this.  We can't really use PrintText as it is because you can't make the colmns line up with the TTF you are using with the printer.  Though we might be able to figure out some fixed type font that you could use, but I think that if we can get this MyPrintText function working we could avoid all that other effort.  Give this a try.  You call this function with:

If Not MyPrintText ("AA", 18, 0, False, False, False, False, 50, 50, 300, 300) Then
  MessageBox("Fail to print text! Check the printer connection and paper.")
  Exit
End If


And here is the function:


Public Function MyPrintText(ByVal text As String, ByVal points As Long, ByVal clr As Long, _
    ByVal isBold As Boolean, ByVal isItalic As Boolean, ByVal isStrike As Boolean, _
    ByVal isUnderline As Boolean, _
    ByVal X1 As Long, ByVal Y1 As Long, _
    ByVal X2 As Long, ByVal Y2 As Long) As Boolean
  PrintText = False
  On Error Resume Next ' for DEBUGGING purposes comment it
 
  Dim font, weight As Long
  Dim dc As Long
 
  dc = GetPrinterDC(True)
  If dc = 0 Then Exit Function
 
  If isBold Then weight = 700 Else weight = 400
  Dim Help As String
  Help = LongToMemoryString(-(points * GetDeviceCaps(dc, LOGPIXELSY)) / 72) ' font size
  Help = Help & LongToMemoryString(0) ' appropriate width
  Help = Help & LongToMemoryString(0) ' no escapment
  Help = Help & LongToMemoryString(0) ' no orientation
  Help = Help & LongToMemoryString(weight) ' some weight THIN=100,NORMAL=400,BOLD=700
  Help = Help & ByteToMemoryString(isItalic) ' italic
  Help = Help & ByteToMemoryString(isUnderline) ' underline
  Help = Help & ByteToMemoryString(isStrike) ' strikeout
  Help = Help & ByteToMemoryString(0) ' default charset
  Help = Help & ByteToMemoryString(0) ' default out precision
  Help = Help & ByteToMemoryString(0) ' default clip precision
  Help = Help & ByteToMemoryString(0) ' default quality
  Help = Help & ByteToMemoryString(0) ' default pitch
  Help = Help & "Tahoma" ' most popular and existing TTF
  font = CreateFontIndirect(Help)
 
  If font = 0 Then
    DeleteDC (dc)
    Exit Function
  End If
 
  ' Prepare DC
  Dim oldFont As Long
  oldFont = SelectObject(dc, font)
  Dim Result As Long
  Result = SetBkMode(dc, TRANSPARENT)
  Result = SetTextColor(dc, clr)  ' 0 for black
 
  Dim PageLeft, PageRight, PageTop, PageBottom, CalcRight, CalcBottom As Long
  PageLeft = 0
  PageTop = 0
  PageRight = pageWidth
  PageBottom = pageHeight
  Dim Modified As String
  Modified = doRect(PageLeft, PageTop, PageRight, PageBottom)
  Result = DrawText(dc, text, -1, Modified, DRAW_TEXT_CALC)
  Call getRect(Modified, PageLeft, PageTop, CalcRight, CalcBottom)
 
  Help = LongToMemoryString(5 * 4) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0)
  If StartDoc(dc, Help) <= 0 Then
    Result = SelectObject(dc, oldFont)
    DeleteDC (dc)
    DeleteObject (font)
    Exit Function
  End If

  DrawPageText(dc, text, -1, doRect(PageLeft, PageTop, PageRight, PageBottom))
   
  If EndDoc(dc) <= 0 Then
    Result = SelectObject(dc, oldFont)
    DeleteDC (dc)
    DeleteObject (font)
    Exit Function
  End If

  ' finally - cleanup
  Result = SelectObject(dc, oldFont)
  DeleteDC (dc)
  DeleteObject (font)
  PrintText = True
End Function


0
 
LVL 7

Expert Comment

by:Enlade
ID: 11805777

If the above prints the "AA" (try the above first) then lets make a minor change to the DrawPageText function call.  Lets put in our X1, Y1, X2, and Y2 values.  So change the line:

  DrawPageText(dc, text, -1, doRect(PageLeft, PageTop, PageRight, PageBottom))

to the following:

  DrawPageText(dc, text, -1, doRect(X1, Y1, X2, Y2))

And call the function just as we did before:

If Not MyPrintText ("AA", 18, 0, False, False, False, False, 50, 50, 300, 300) Then
  MessageBox("Fail to print text! Check the printer connection and paper.")
  Exit
End If

0
 
LVL 7

Expert Comment

by:Enlade
ID: 11805883

If the above prints the "AA" (try the above first) then we can use the MyPrintText function to print out the rows like this:


'Print the heading
MyPrint 0, 0, "DATE"
MyPrint 1, 0, "C1"
MyPrint 2, 0, "C2"
MyPrint 3, 0, "C3"
MyPrint 4, 0, "C4"


And create a function called MyPrint to handle it for us.  In this case I have fixed the column sizes to be 1 inch wide and the rows to be 0.25 inches high.  So hopefully your paper is at least 5 inches wide or you will have to reduce the ColWidth.


Private Sub MyPrint(ByVal Col as long, ByVal Row as long, ByVal text as string)
  Dim RowHeight As Long
  Dim RowPos as Long
  Dim ColWidth as Long
  Dim ColPos as Long

  RowHeight = (203 * 0.25)
  RowPos = RowHeight * Row

  ColWidth = (203 * 1.0)
  ColPos = ColWidth * Col

  If Not MyPrintText (text, 9, 0, False, False, False, False, ColPos, RowPos, ColWidth, RowHeight) Then
    MessageBox("Fail to print text! Check the printer connection and paper.")
    Exit Sub
  End If
End Sub
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11805889

Or better yet you can pass the column width (in inches) to the function to get a variable column size (and the row height as well).

Private Sub MyPrint(ByVal Col as long, ByVal Row as long, ByVal text as string, ByVal ColWidthIn as Single, ByVal RowHeightIn as Single)
  Dim RowHeight As Long
  Dim RowPos as Long
  Dim ColWidth as Long
  Dim ColPos as Long

  RowHeight = (203 * RowHeightIn)
  RowPos = RowHeight * Row

  ColWidth = (203 * ColWidthIn)
  ColPos = ColWidth * Col

  If Not MyPrintText (text, 9, 0, False, False, False, False, ColPos, RowPos, ColWidth, RowHeight) Then
    MessageBox("Fail to print text! Check the printer connection and paper.")
    Exit Sub
  End If
End Sub
0
 

Author Comment

by:markh51
ID: 11805899
It doesn't print anything !

It connects to the printer and thats it... no printing at all.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11805941

Did you try the function that has the DrawPageText function call in it?  Because the previous function I posted will not print anything, but the one with the DrawPageText is the same as just calling PrintText directly.  I'll look to see if anything is different, but I think it is functionally identical to PrintText (though that first MyPrintText function I wrote is missing the DrawPageText call and won't print anything).
0
 

Author Comment

by:markh51
ID: 11807349
Right, we have something now !!!

We now have another problem when we call:

'Print the heading
MyPrint 0, 0, "DATE"
MyPrint 1, 0, "C1"
MyPrint 2, 0, "C2"
MyPrint 3, 0, "C3"
MyPrint 4, 0, "C4"

...the printer moves on to the next line between each one ? It must think it is the end of each page or something. Exactly the same happens with the normal PrintText function, you have to format your string something like:

TextToPrint = "SOME TEST TEXT" & chr(10) & "MORE TEXT ON THE NEXT LINE"

so that it doesn't do an automatic line feed between each page.

Also, the above only prints "DATE", then moves up then prints nothing then the same again for the rest of the MyPrint lines ?

At least we got something :)
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11813581


Ok, one more question and then I might have a solution.  Ignore the fact that each bit of text that you try to print is always ending up on the next line (like there is always a CR/LF after each line).  Focus on the position of the text from the left side of the paper.  Does it move over each time you print to a different column?  What is the distance that "DATE" is from the left side of the paper?  How about the distance to "C1"?  etc?

I'm interested in knowing if we are effecting the position across the page with our Rectangle.  I think I know why it is not working vertically.....but it is important that we get it working horizontally.  I'm afriad that the horizontal might be a problem and I just want to get past that before I give you the final solution.

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 7

Expert Comment

by:Enlade
ID: 11813666

Ok, since I won't be online much over the next few days let me give you some code to work with.  Still, answer the above questions first (just in case I can get back online before you try the code I'm about to give you).  In any case, let me work out some code that might be more useful.  Hold up a sec.

P.S.  Again, first answer the above question as it is important.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11813718

Ok, here we go.  I think that the call to GetPrinterDC and the call to StartDoc is giving you the trouble with each column being on its one line (Row).  So, we need to do all our row work in one call rather then multiple calls to MyPrintText.

So, lets create a new function called MyPrintTextRow that will print out a full row (columns and all).  To do this we will need to create some new UDT's (types).  So lets start with them.


Public Type ColDataType
  text As String
  ColWidth as Single           'In inches
End Type

Public Type RowDataType
  Col() As ColDataType
  nCol as Long
  RowHeight as Single          'In Inches
End Type



Now, once you fill up some variable that is of type RowDataType you can pass that structure to the new MyPrintTextRow function (see below).  Remember to Redim Col from 0 and then fill each Col text item with "DATE", and "C1", and "C2", etc.  And then set nCol to the number of cols.  And of course set the RowHeight and each ColWidth (in Inches).  If you have any trouble with this then I can write something, but I need to go somewhere for a bit.

Anyway, here is the function that I am hoping will work.  Please understand that I cannot test the code so it might have some syntax errors.  But hopefully it is correct.  Give it a try.


Public Function MyPrintTextRow(ByVal text As String, ByVal points As Long, ByVal clr As Long, _
    ByVal isBold As Boolean, ByVal isItalic As Boolean, ByVal isStrike As Boolean, _
    ByVal isUnderline As Boolean, _
    ByRef RowData As RowDataType) As Boolean
  PrintText = False
  On Error Resume Next ' for DEBUGGING purposes comment it
 
  Dim font, weight As Long
  Dim dc As Long
  Dim i As Long
 
  dc = GetPrinterDC(True)
  If dc = 0 Then Exit Function
 
  If isBold Then weight = 700 Else weight = 400
  Dim Help As String
  Help = LongToMemoryString(-(points * GetDeviceCaps(dc, LOGPIXELSY)) / 72) ' font size
  Help = Help & LongToMemoryString(0) ' appropriate width
  Help = Help & LongToMemoryString(0) ' no escapment
  Help = Help & LongToMemoryString(0) ' no orientation
  Help = Help & LongToMemoryString(weight) ' some weight THIN=100,NORMAL=400,BOLD=700
  Help = Help & ByteToMemoryString(isItalic) ' italic
  Help = Help & ByteToMemoryString(isUnderline) ' underline
  Help = Help & ByteToMemoryString(isStrike) ' strikeout
  Help = Help & ByteToMemoryString(0) ' default charset
  Help = Help & ByteToMemoryString(0) ' default out precision
  Help = Help & ByteToMemoryString(0) ' default clip precision
  Help = Help & ByteToMemoryString(0) ' default quality
  Help = Help & ByteToMemoryString(0) ' default pitch
  Help = Help & "Tahoma" ' most popular and existing TTF
  font = CreateFontIndirect(Help)
 
  If font = 0 Then
    DeleteDC (dc)
    Exit Function
  End If
 
  ' Prepare DC
  Dim oldFont As Long
  oldFont = SelectObject(dc, font)
  Dim Result As Long
  Result = SetBkMode(dc, TRANSPARENT)
  Result = SetTextColor(dc, clr)  ' 0 for black
 
  Dim PageLeft, PageRight, PageTop, PageBottom, CalcRight, CalcBottom As Long
  PageLeft = 0
  PageTop = 0
  PageRight = pageWidth
  PageBottom = pageHeight

'  Dim Modified As String
'  Modified = doRect(PageLeft, PageTop, PageRight, PageBottom)
'  Result = DrawText(dc, text, -1, Modified, DRAW_TEXT_CALC)
'  Call getRect(Modified, PageLeft, PageTop, CalcRight, CalcBottom)
 
  Help = LongToMemoryString(5 * 4) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0)
  If StartDoc(dc, Help) <= 0 Then
    Result = SelectObject(dc, oldFont)
    DeleteDC (dc)
    DeleteObject (font)
    Exit Function
  End If

  tempLeft = 0
  tempTop = 0
  tempBottom = (RowData.RowHeight * 203.0)

  For i = 0 to (RowData.nCol - 1)
    tempRight = tempLeft + (RowData.Col(i).ColWidth * 203.0)
    DrawPageText(dc, RowData.Col(i).text, -1, doRect(tempLeft, tempTop, tempRight, tempBottom))
    tempLeft = tempLeft + (RowData.Col(i).ColWidth * 203.0)
  Next i
   
  If EndDoc(dc) <= 0 Then
    Result = SelectObject(dc, oldFont)
    DeleteDC (dc)
    DeleteObject (font)
    Exit Function
  End If

  ' finally - cleanup
  Result = SelectObject(dc, oldFont)
  DeleteDC (dc)
  DeleteObject (font)
  PrintText = True
End Function

0
 

Author Comment

by:markh51
ID: 11813754
Using your old code, it prints the DATE column and thats it ! It then moves down for column thereafter. The date column is in a good position.
0
 

Author Comment

by:markh51
ID: 11813793
I've added your code to the project, but I'm not sure how to use it.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11815285

But does it print something like:

DATE
               C1
                                C2
                                                  C3
                                                                    C4


or does it print something like:

DATE
C1
C2
C3
C4

???
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11815392

Oh, and you don't need to pass text to the MyPrintTextRow function.  So change the function to:


Public Function MyPrintTextRow(ByVal points As Long, ByVal clr As Long, _
    ByVal isBold As Boolean, ByVal isItalic As Boolean, ByVal isStrike As Boolean, _
    ByVal isUnderline As Boolean, _
    ByRef RowData As RowDataType) As Boolean


You know it would probably be better to put all the formating into the structure as well.  So you could use the structure like this:


Public Type ColDataType
  text As String
  ColWidth as Single           'In inches
  points as long
  clr as long
  isBold as boolean
  isItalic as boolean
  isStrike as boolean
  isUnderline as boolean
End Type


And then change the function so to use those properties for each column.  And then you only need to set up the function like this:


Public Function MyPrintTextRow(ByRef RowData As RowDataType) As Boolean


HOWEVER, lets not do that just yet.  Instead, lets just work with what we got.  Just take out the text item from the function heading.  And then I'll throw together some code to fill up the structure for you.



0
 
LVL 7

Expert Comment

by:Enlade
ID: 11815445

We might be getting a little ahead of ourselves without me knowing for sure what we currently are able to print (how it looks that is).  But since I'm not logging in as much I want to give you some things to try.  So, let me explain how you would use the function that I wrote above (that I am hopeing will work).

Ok, lets say you have a function that is going to print out all the data.  Inside that function you can declare some temporary row variable that you will then fill up.  Then you will pass that row on to the MyPrintTextRow function.  Something like this:


Dim tempRow as RowDataType

tempRow.nCol = 5
Redim tempRow.Col(0 to (tempRow.nCol-1)) As ColDataType
tempRow.RowHeight = 1.5

tempRow.Col(0).text = "DATE"
tempRow.Col(0).ColWidth = 0.8

tempRow.Col(1).text = "C1"
tempRow.Col(1).ColWidth = 0.5

tempRow.Col(2).text = "C2"
tempRow.Col(2).ColWidth = 0.5

tempRow.Col(3).text = "C3"
tempRow.Col(3).ColWidth = 0.5

tempRow.Col(4).text = "C4"
tempRow.Col(4).ColWidth = 0.5

If Not MyPrintTextRow (9, 0, False, False, False, False, tempRow) Then
  MessageBox("Fail to print text! Check the printer connection and paper.")
  Exit Sub
End If


Keep in mind that I am assuming that you removed the "ByVal text as String" from the MyPrintTextRow function as I discribed in my previous message.  Otherwise we would have to pass it some dummy text that the function never uses.

Now, see if we moved all those other properties (points, italic, underline, etc) over to the structure (as I discribed in my previous message) then we would be initialzing them just like we initialzed the ColWidth (above).  And then you would call the function by just passing it tempRow (as I discribed in my previous message).  Though you will need to change the refrences inside the MyPrintTextRow function to address those properties through the Row structure.  So, lets just ignore that for now and you can fine tune all that after we get it printing (if we get it printing).
0
 

Author Comment

by:markh51
ID: 11815659
Few errors:

"You can not use a reserved word as a variable, const, sub, function or declare names" for this line:

Public Type ColDataType
  text As String
  ColWidth As Single           'In inches
End Type

and a "Expected '('" for line:

ReDim tempRow.Col(0 To (tempRow.nCol - 1)) As ColDataType
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11815737

Hmmmmmmm.....not sure why that is.  Maybe eVB uses ColWidth or ColDataType as a reserved word or something.  Try changing the names of stuff to figure out which one is causing the problem.  Like:

Public Type ColDataType1
  text1 As String
  ColWidth1 As Single           'In inches
End Type

Or, maybe it isn't a problem with the names.  Maybe you are declaring the type as Public within a form?  If its inside a module then you can make it Public, but if it is inside your test form then you need to make it Private.

Anyway, try the above and if it gets past the type definition then it is one of those names that is the problem.  So, change one at a time till you figure out which it is and then change that one name throught the program.  Tell me which it is though so I don't make that mistake again.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11815747

I still need to know how that previous test printed.

Did it print something like:

DATE
               C1
                                C2
                                                  C3
                                                                    C4


or did it print something like:

DATE
C1
C2
C3
C4


or did it print some other way, like???
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11815797

Actually, doesn't it break on the line that it has a problem with?  If it breaks on the line:

Public Type ColDataType

Then it is a problem with ColDataType.  So just change it to MyColDataType or something (I'm not being very complient with my naming conventions....hee hee).

If it breaks and highlights the line:

  ColWidth As Single           'In inches

Then change ColWidth to MyColWidth or something.  But remember you must change all reference to that variable throught your program (DO NOT do a find replace/all because it might be that Printer.BAS is using it and that is why we can't reuse it as a public or some such thing, and we don't want to unknowningly change anything in Printer.BAS).
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11815927

Oh, you know maybe eVB doesn't allow for type definitions.  Do you know if eVB allows for type definitions?
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11815943

Ahhhhh....yes....eVB has some other limitations.

http://support.microsoft.com/default.aspx?kbid=184650

So, we can't use the Type command.  That means we are stuck with Arrays and that makes me sad.

Let me rewrite the function using just one array of columns.  That will get rid of that error message.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11816038

Ok, sad as I am about not being able to use "Type" here is a similar function using just an Array.


Public Function MyPrintTextRow(ByVal points As Long, ByVal clr As Long, _
    ByVal isBold As Boolean, ByVal isItalic As Boolean, ByVal isStrike As Boolean, _
    ByVal isUnderline As Boolean, _
    ByRef colText() As String, ByVal nCol As Long, _
    ByVal RowHeight as Single, ByVal ColWidth as Single) As Boolean
  PrintText = False
  On Error Resume Next ' for DEBUGGING purposes comment it
 
  Dim font, weight As Long
  Dim dc As Long
  Dim i As Long
  Dim tempLeft as long
  Dim tempTop as long
  Dim tempRight as long
  Dim tempBottom as long
 
  dc = GetPrinterDC(True)
  If dc = 0 Then Exit Function
 
  If isBold Then weight = 700 Else weight = 400
  Dim Help As String
  Help = LongToMemoryString(-(points * GetDeviceCaps(dc, LOGPIXELSY)) / 72) ' font size
  Help = Help & LongToMemoryString(0) ' appropriate width
  Help = Help & LongToMemoryString(0) ' no escapment
  Help = Help & LongToMemoryString(0) ' no orientation
  Help = Help & LongToMemoryString(weight) ' some weight THIN=100,NORMAL=400,BOLD=700
  Help = Help & ByteToMemoryString(isItalic) ' italic
  Help = Help & ByteToMemoryString(isUnderline) ' underline
  Help = Help & ByteToMemoryString(isStrike) ' strikeout
  Help = Help & ByteToMemoryString(0) ' default charset
  Help = Help & ByteToMemoryString(0) ' default out precision
  Help = Help & ByteToMemoryString(0) ' default clip precision
  Help = Help & ByteToMemoryString(0) ' default quality
  Help = Help & ByteToMemoryString(0) ' default pitch
  Help = Help & "Tahoma" ' most popular and existing TTF
  font = CreateFontIndirect(Help)
 
  If font = 0 Then
    DeleteDC (dc)
    Exit Function
  End If
 
  ' Prepare DC
  Dim oldFont As Long
  oldFont = SelectObject(dc, font)
  Dim Result As Long
  Result = SetBkMode(dc, TRANSPARENT)
  Result = SetTextColor(dc, clr)  ' 0 for black
 
  Dim PageLeft, PageRight, PageTop, PageBottom, CalcRight, CalcBottom As Long
  PageLeft = 0
  PageTop = 0
  PageRight = pageWidth
  PageBottom = pageHeight

'  Dim Modified As String
'  Modified = doRect(PageLeft, PageTop, PageRight, PageBottom)
'  Result = DrawText(dc, text, -1, Modified, DRAW_TEXT_CALC)
'  Call getRect(Modified, PageLeft, PageTop, CalcRight, CalcBottom)
 
  Help = LongToMemoryString(5 * 4) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0)
  If StartDoc(dc, Help) <= 0 Then
    Result = SelectObject(dc, oldFont)
    DeleteDC (dc)
    DeleteObject (font)
    Exit Function
  End If

  tempLeft = 0
  tempTop = 0
  tempBottom = (RowHeight * 203.0)

  For i = 0 to (nCol - 1)
    tempRight = tempLeft + (ColWidth * 203.0)
    DrawPageText(dc, colText(i), -1, doRect(tempLeft, tempTop, tempRight, tempBottom))
    tempLeft = tempLeft + (ColWidth * 203.0)
  Next i
   
  If EndDoc(dc) <= 0 Then
    Result = SelectObject(dc, oldFont)
    DeleteDC (dc)
    DeleteObject (font)
    Exit Function
  End If

  ' finally - cleanup
  Result = SelectObject(dc, oldFont)
  DeleteDC (dc)
  DeleteObject (font)
  PrintText = True
End Function
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11816085

Oh heck, lets allow for variable column sizes instead of fixed sizes.  So, lets pass the ColWidth as an array as well.  Sniff...boo hoo...I have gone back in time to my old Trash-80 days of Basic programming with no UDT's...:)

Anyway, here is the function allowing for variable sized columns.

Public Function MyPrintTextRow(ByVal points As Long, ByVal clr As Long, _
    ByVal isBold As Boolean, ByVal isItalic As Boolean, ByVal isStrike As Boolean, _
    ByVal isUnderline As Boolean, _
    ByRef colText() As String, ByRef colWidth() As Single, ByVal nCol As Long, _
    ByVal RowHeight as Single) As Boolean
  PrintText = False
  On Error Resume Next ' for DEBUGGING purposes comment it
 
  Dim font, weight As Long
  Dim dc As Long
  Dim i As Long
  Dim tempLeft as long
  Dim tempTop as long
  Dim tempRight as long
  Dim tempBottom as long
 
  dc = GetPrinterDC(True)
  If dc = 0 Then Exit Function
 
  If isBold Then weight = 700 Else weight = 400
  Dim Help As String
  Help = LongToMemoryString(-(points * GetDeviceCaps(dc, LOGPIXELSY)) / 72) ' font size
  Help = Help & LongToMemoryString(0) ' appropriate width
  Help = Help & LongToMemoryString(0) ' no escapment
  Help = Help & LongToMemoryString(0) ' no orientation
  Help = Help & LongToMemoryString(weight) ' some weight THIN=100,NORMAL=400,BOLD=700
  Help = Help & ByteToMemoryString(isItalic) ' italic
  Help = Help & ByteToMemoryString(isUnderline) ' underline
  Help = Help & ByteToMemoryString(isStrike) ' strikeout
  Help = Help & ByteToMemoryString(0) ' default charset
  Help = Help & ByteToMemoryString(0) ' default out precision
  Help = Help & ByteToMemoryString(0) ' default clip precision
  Help = Help & ByteToMemoryString(0) ' default quality
  Help = Help & ByteToMemoryString(0) ' default pitch
  Help = Help & "Tahoma" ' most popular and existing TTF
  font = CreateFontIndirect(Help)
 
  If font = 0 Then
    DeleteDC (dc)
    Exit Function
  End If
 
  ' Prepare DC
  Dim oldFont As Long
  oldFont = SelectObject(dc, font)
  Dim Result As Long
  Result = SetBkMode(dc, TRANSPARENT)
  Result = SetTextColor(dc, clr)  ' 0 for black
 
  Dim PageLeft, PageRight, PageTop, PageBottom, CalcRight, CalcBottom As Long
  PageLeft = 0
  PageTop = 0
  PageRight = pageWidth
  PageBottom = pageHeight

'  Dim Modified As String
'  Modified = doRect(PageLeft, PageTop, PageRight, PageBottom)
'  Result = DrawText(dc, text, -1, Modified, DRAW_TEXT_CALC)
'  Call getRect(Modified, PageLeft, PageTop, CalcRight, CalcBottom)
 
  Help = LongToMemoryString(5 * 4) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0)
  If StartDoc(dc, Help) <= 0 Then
    Result = SelectObject(dc, oldFont)
    DeleteDC (dc)
    DeleteObject (font)
    Exit Function
  End If

  tempLeft = 0
  tempTop = 0
  tempBottom = (RowHeight * 203.0)

  For i = 0 to (nCol - 1)
    tempRight = tempLeft + (ColWidth(i) * 203.0)
    DrawPageText(dc, colText(i), -1, doRect(tempLeft, tempTop, tempRight, tempBottom))
    tempLeft = tempLeft + (ColWidth(i) * 203.0)
  Next i
   
  If EndDoc(dc) <= 0 Then
    Result = SelectObject(dc, oldFont)
    DeleteDC (dc)
    DeleteObject (font)
    Exit Function
  End If

  ' finally - cleanup
  Result = SelectObject(dc, oldFont)
  DeleteDC (dc)
  DeleteObject (font)
  PrintText = True
End Function
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11816110

And here is how you would use it:

Dim tempCols() As String
Dim tempColWidth() As Single
Dim nCols As Long
Dim tempRowHeight As Single

nCols = 5
Redim tempCols(0 to (nCols-1)) As String
Redim tempColWidth(0 to (nCols-1)) As Single
tempRowHeight = 1.5

tempCol(0) = "DATE"
tempColWidth(0) = 0.8

tempCol(1) = "C1"
tempColWidth(1) = 0.5

tempCol(2) = "C2"
tempColWidth(2) = 0.5

tempCol(3) = "C3"
tempColWidth(3) = 0.5

tempCol(4) = "C4"
tempColWidth(4) = 0.5

If Not MyPrintTextRow (9, 0, False, False, False, False, tempCol, tempColWidth, nCols, tempRowHeight) Then
  MessageBox("Fail to print text! Check the printer connection and paper.")
  Exit Sub
End If

0
 
LVL 7

Expert Comment

by:Enlade
ID: 11816149

Mind you, I am assuming the eVB supports the "ReDim" command.  If not then you will have to fix the array size.  That isn't a problem since you can make it 50 or something.  You probably would never have more then 50 columns (probably not more then 8).  Then you can reuse this function for any programs that print columns.  Though "ReDim" is better, so lets hope we can use it in eVB.  Fingers are crossed.

Hey, you still didn't tell me how that previous test printed?

Did it print something like:

DATE
               C1
                                C2
                                                  C3
                                                                    C4


or did it print something like:

DATE
C1
C2
C3
C4


or did it print some other way?

<Insert example here>

:)
0
 

Author Comment

by:markh51
ID: 11817875
The previous test only printed "DATE" and thats it !

It did try and print the other columns but  the printer kept movig down for each column, but printed nothing !

I'll try the above code.
0
 

Author Comment

by:markh51
ID: 11817900
The code generates a compile error on this line:

ReDim tempCols(0 To (nCols - 1)) As String

It said that it requires a ')' after the 0 !?!

eVB DOES support the reDim command:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbceide/htm/statemnt_17.asp
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11820553

Oh....it forces the lower bound to be 0.  So you don't have to list both lower and upper bounds.  Try this instead:

Redim tempCols(nCols - 1) As String

And, of course, also change tempColWidth:

Redim tempColWidth(nCols - 1) As Single

0
 
LVL 7

Expert Comment

by:Enlade
ID: 11820604

I'm a little discouraged that the previous test only printed "DATA" and no other columns.  I'm thinking that maybe it was paging down after printing each column.  But lets not worry about that yet.  Lets get these bugs out of the current test code.
0
 

Author Comment

by:markh51
ID: 11820667
Nearly working...

Now when it prints, it prints all of the columns BUT in a descending step fashion with the automatic line feed between each one !!!

Example printout:

DATE




          C1




                    C2




                              C3




                                        C4
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11821529

Boy this thing is fighting us all the way.

Well, we got the horizontal positioning working.....thats a good thing.  But what shall we do with that vertical positioning.  Let me think a sec.

0
 

Author Comment

by:markh51
ID: 11821572
I know, its a total and utter nightmare ;)
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11821576

Oh....i think i see.  The DrawPageText function is calling StartPage.  Lets move that out into our function and it might just work for us.  Try this:

Public Function MyPrintTextRow(ByVal points As Long, ByVal clr As Long, _
    ByVal isBold As Boolean, ByVal isItalic As Boolean, ByVal isStrike As Boolean, _
    ByVal isUnderline As Boolean, _
    ByRef colText() As String, ByRef colWidth() As Single, ByVal nCol As Long, _
    ByVal RowHeight as Single) As Boolean
  PrintText = False
  On Error Resume Next ' for DEBUGGING purposes comment it
 
  Dim font, weight As Long
  Dim dc As Long
  Dim i As Long
  Dim tempLeft as long
  Dim tempTop as long
  Dim tempRight as long
  Dim tempBottom as long
 
  dc = GetPrinterDC(True)
  If dc = 0 Then Exit Function
 
  If isBold Then weight = 700 Else weight = 400
  Dim Help As String
  Help = LongToMemoryString(-(points * GetDeviceCaps(dc, LOGPIXELSY)) / 72) ' font size
  Help = Help & LongToMemoryString(0) ' appropriate width
  Help = Help & LongToMemoryString(0) ' no escapment
  Help = Help & LongToMemoryString(0) ' no orientation
  Help = Help & LongToMemoryString(weight) ' some weight THIN=100,NORMAL=400,BOLD=700
  Help = Help & ByteToMemoryString(isItalic) ' italic
  Help = Help & ByteToMemoryString(isUnderline) ' underline
  Help = Help & ByteToMemoryString(isStrike) ' strikeout
  Help = Help & ByteToMemoryString(0) ' default charset
  Help = Help & ByteToMemoryString(0) ' default out precision
  Help = Help & ByteToMemoryString(0) ' default clip precision
  Help = Help & ByteToMemoryString(0) ' default quality
  Help = Help & ByteToMemoryString(0) ' default pitch
  Help = Help & "Tahoma" ' most popular and existing TTF
  font = CreateFontIndirect(Help)
 
  If font = 0 Then
    DeleteDC (dc)
    Exit Function
  End If
 
  ' Prepare DC
  Dim oldFont As Long
  oldFont = SelectObject(dc, font)
  Dim Result As Long
  Result = SetBkMode(dc, TRANSPARENT)
  Result = SetTextColor(dc, clr)  ' 0 for black
 
  Dim PageLeft, PageRight, PageTop, PageBottom, CalcRight, CalcBottom As Long
  PageLeft = 0
  PageTop = 0
  PageRight = pageWidth
  PageBottom = pageHeight

'  Dim Modified As String
'  Modified = doRect(PageLeft, PageTop, PageRight, PageBottom)
'  Result = DrawText(dc, text, -1, Modified, DRAW_TEXT_CALC)
'  Call getRect(Modified, PageLeft, PageTop, CalcRight, CalcBottom)
 
  Help = LongToMemoryString(5 * 4) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0)
  If StartDoc(dc, Help) <= 0 Then
    Result = SelectObject(dc, oldFont)
    DeleteDC (dc)
    DeleteObject (font)
    Exit Function
  End If


  StartPage (dc)

  tempLeft = 0
  tempTop = 0
  tempBottom = (RowHeight * 203.0)

  For i = 0 to (nCol - 1)
    tempRight = tempLeft + (ColWidth(i) * 203.0)
    DrawText(dc, colText(i), -1, doRect(tempLeft, tempTop, tempRight, tempBottom), DRAW_TEXT_DRAW)
    tempLeft = tempLeft + (ColWidth(i) * 203.0)
  Next i
 
  EndPage (dc)
   
  If EndDoc(dc) <= 0 Then
    Result = SelectObject(dc, oldFont)
    DeleteDC (dc)
    DeleteObject (font)
    Exit Function
  End If

  ' finally - cleanup
  Result = SelectObject(dc, oldFont)
  DeleteDC (dc)
  DeleteObject (font)
  PrintText = True
End Function
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11821613

Something to remember.  You will need to add in all the error checking that I pulled out.  Like if you call GetPrinterDC and it returns False then you should report an error and exit the function.  Same with StartPage and all the other functions.  Right now the code just assumes that everything works and doesn't do much error checking.

Anyway, I think this function might just work.  Give it a try.
0
 

Author Comment

by:markh51
ID: 11821765
Ahhhhhhhh, the printers on fire !
0
 

Author Comment

by:markh51
ID: 11821773
LOL, it seems to work fine apart from the text quality is very crappy compared to the PrintText function.

Any ideas why ?
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11821804

Also, could you find out what value pageWidth and pageHeight are?  Like put in DrawText that will print out those values?  Like, temporarilly comment out the For loop and insert two DrawText commands?  Something like:


  DrawText(dc, pageWidth, -1, doRect(0, 0, 300, 200), DRAW_TEXT_DRAW)
  DrawText(dc, pageHeight, -1, doRect(0, 200, 300, 400), DRAW_TEXT_DRAW)

'  For i = 0 to (nCol - 1)
'    tempRight = tempLeft + (ColWidth(i) * 203.0)
'    DrawText(dc, colText(i), -1, doRect(tempLeft, tempTop, tempRight, tempBottom), DRAW_TEXT_DRAW)
'    tempLeft = tempLeft + (ColWidth(i) * 203.0)
'  Next i
'  
'  EndPage (dc)


Once you get the values of pageWidth and pageHeight then you can put it back to the way it was.  The values of pageWidth and pageHeight might be important to us.  You see once we get this all printing in columns you might still have a problem with printing off the bottom edge of the paper.  I understand that this is a roll paper type printer so there really isn't a bottom edge, but technically there is a bottom edge and that is defined by pageHeight.  So programaticlly we need to be aware of how pageHeight is used in relation to the printer.  Knowing the value is the first step in understanding how it might be used.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11821845

Well, it should be the same because we didn't do much different.  Maybe try setting isBold to True when you call the MyPrintTextRow Function.  Maybe it will look nicer if it is bold.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11821880

Oh, and my code choose a font size of 9.  That size might not be a natural font size for the font type they are using ("Tahoma").  If you think you have room you could change the size to 18 like they used in thier example.  Or try 8 or 10 to see if they are more natural looking.

Another option is to try a nicer looking font, but then you would need to figure out what fonts are available for this printer.  Lets not do that yet.  First try a different font size (and turn isBold on or off while you are testing).
0
 

Author Comment

by:markh51
ID: 11821883
Where are these DrawText functions ?? I can't find them, are they in your fuction ?

Also how do I get the values just by commenting out those lines ?
0
 

Author Comment

by:markh51
ID: 11821916
Got to go out for 30 mins...
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11821942

I meant that you should add them to my function and comment out the For loop.  It isn't a perminant change.  Just a quick change to find out what pageWidth and pageHeight are.  Remember you want to remove them later.  Its just a quick test.  Like this:

Public Function MyPrintTextRow(ByVal points As Long, ByVal clr As Long, _
    ByVal isBold As Boolean, ByVal isItalic As Boolean, ByVal isStrike As Boolean, _
    ByVal isUnderline As Boolean, _
    ByRef colText() As String, ByRef colWidth() As Single, ByVal nCol As Long, _
    ByVal RowHeight as Single) As Boolean
  PrintText = False
  On Error Resume Next ' for DEBUGGING purposes comment it
 
  Dim font, weight As Long
  Dim dc As Long
  Dim i As Long
  Dim tempLeft as long
  Dim tempTop as long
  Dim tempRight as long
  Dim tempBottom as long
 
  dc = GetPrinterDC(True)
  If dc = 0 Then Exit Function
 
  If isBold Then weight = 700 Else weight = 400
  Dim Help As String
  Help = LongToMemoryString(-(points * GetDeviceCaps(dc, LOGPIXELSY)) / 72) ' font size
  Help = Help & LongToMemoryString(0) ' appropriate width
  Help = Help & LongToMemoryString(0) ' no escapment
  Help = Help & LongToMemoryString(0) ' no orientation
  Help = Help & LongToMemoryString(weight) ' some weight THIN=100,NORMAL=400,BOLD=700
  Help = Help & ByteToMemoryString(isItalic) ' italic
  Help = Help & ByteToMemoryString(isUnderline) ' underline
  Help = Help & ByteToMemoryString(isStrike) ' strikeout
  Help = Help & ByteToMemoryString(0) ' default charset
  Help = Help & ByteToMemoryString(0) ' default out precision
  Help = Help & ByteToMemoryString(0) ' default clip precision
  Help = Help & ByteToMemoryString(0) ' default quality
  Help = Help & ByteToMemoryString(0) ' default pitch
  Help = Help & "Tahoma" ' most popular and existing TTF
  font = CreateFontIndirect(Help)
 
  If font = 0 Then
    DeleteDC (dc)
    Exit Function
  End If
 
  ' Prepare DC
  Dim oldFont As Long
  oldFont = SelectObject(dc, font)
  Dim Result As Long
  Result = SetBkMode(dc, TRANSPARENT)
  Result = SetTextColor(dc, clr)  ' 0 for black
 
  Dim PageLeft, PageRight, PageTop, PageBottom, CalcRight, CalcBottom As Long
  PageLeft = 0
  PageTop = 0
  PageRight = pageWidth
  PageBottom = pageHeight

'  Dim Modified As String
'  Modified = doRect(PageLeft, PageTop, PageRight, PageBottom)
'  Result = DrawText(dc, text, -1, Modified, DRAW_TEXT_CALC)
'  Call getRect(Modified, PageLeft, PageTop, CalcRight, CalcBottom)
 
  Help = LongToMemoryString(5 * 4) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0)
  If StartDoc(dc, Help) <= 0 Then
    Result = SelectObject(dc, oldFont)
    DeleteDC (dc)
    DeleteObject (font)
    Exit Function
  End If

  StartPage (dc)

  tempLeft = 0
  tempTop = 0
  tempBottom = (RowHeight * 203.0)

  'This is a test.  Remove these and uncomment the For loop after test is done
  DrawText(dc, pageWidth, -1, doRect(0, 0, 300, 200), DRAW_TEXT_DRAW)
  DrawText(dc, pageHeight, -1, doRect(0, 200, 300, 400), DRAW_TEXT_DRAW)

'  For i = 0 to (nCol - 1)
'    tempRight = tempLeft + (ColWidth(i) * 203.0)
'    DrawText(dc, colText(i), -1, doRect(tempLeft, tempTop, tempRight, tempBottom), DRAW_TEXT_DRAW)
'    tempLeft = tempLeft + (ColWidth(i) * 203.0)
'  Next i
 
  EndPage (dc)
   
  If EndDoc(dc) <= 0 Then
    Result = SelectObject(dc, oldFont)
    DeleteDC (dc)
    DeleteObject (font)
    Exit Function
  End If

  ' finally - cleanup
  Result = SelectObject(dc, oldFont)
  DeleteDC (dc)
  DeleteObject (font)
  PrintText = True
End Function
0
 

Author Comment

by:markh51
ID: 11822365
The values which are printed are 384 and 543 in that order.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11822699

Ok....so now the problem is printing multiple rows.  I guess you should try just printing another row (print the same data twice) and see what happens.  I suspect that the EndPage will move us down more then we want.  If that is the case then we could subtract the difference from our next row.  Or, and most likely what we will do, we will break our MyPrintTextRow function up into three parts (MyStartTable, MyPrintRow, and MyEndTable).  That way we can prevent the function from calling EndPage (EndDoc, etc) after each row.  Still, lets start by calling the function a second time to see where the second row ends up being printed.

I gotta go out now.  Be back later.

0
 

Author Comment

by:markh51
ID: 11823290
Just as you thought, if you call the function again, it just ends up further down the page because of the EndDoc.
0
 

Author Comment

by:markh51
ID: 11823430
The text seems OK now, so at least one less thing to worry about ;)
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11825190


Ok....one thing I think we are doing wrong is trying to write a standard table function.  We have already come so far that we might as well finish, but when you actually write your code you should probably just write a table function that is specific to your data.  That way you can also draw lines and all sorts of other stuff to make your data look just right for you.

That said, lets break up or MyPrintTextRow function into the three functions that I mentioned earlier.  Hold up....


0
 

Author Comment

by:markh51
ID: 11825515
How would I draw a line under the column headers ?
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11826672

I'm sorry, I got called away.  The more I think of it the more it makes sense that you generate the table directly rather then create a global table like function.  It will make it easier to manage multiple pages since your page is actually only a few inches long (programaticlly).  Even though it is endless in reality since it is roll paper.  Anyway, lets complete the thought that I had above and give you some more code to toy with.  Try to understand what is going on and then what you do is combine everthing into one function that prints your data rather then trying to write a function that prints a table in general.  Anyway, here is some more code.  Notice first that we are making the MyPrinterDC, font and oldFont global (because they are used in multiple function).  You could pass them between function, but this is just a test anyways.


Public MyPrinterDC As Long
Public font as Long
Public oldFont As Long

Public Function MyStartTable(ByVal points As Long, ByVal clr As Long, _
    ByVal isBold As Boolean, ByVal isItalic As Boolean, ByVal isStrike As Boolean, _
    ByVal isUnderline As Boolean) As Boolean

  MyStartTable = False
  On Error Resume Next ' for DEBUGGING purposes comment it
 
  Dim weight As Long
  Dim Result As Long
 
  MyPrinterDC = GetPrinterDC(True)
  If MyPrinterDC = 0 Then Exit Function
 
  If isBold Then weight = 700 Else weight = 400
  Dim Help As String
  Help = LongToMemoryString(-(points * GetDeviceCaps(MyPrinterDC, LOGPIXELSY)) / 72) ' font size
  Help = Help & LongToMemoryString(0) ' appropriate width
  Help = Help & LongToMemoryString(0) ' no escapment
  Help = Help & LongToMemoryString(0) ' no orientation
  Help = Help & LongToMemoryString(weight) ' some weight THIN=100,NORMAL=400,BOLD=700
  Help = Help & ByteToMemoryString(isItalic) ' italic
  Help = Help & ByteToMemoryString(isUnderline) ' underline
  Help = Help & ByteToMemoryString(isStrike) ' strikeout
  Help = Help & ByteToMemoryString(0) ' default charset
  Help = Help & ByteToMemoryString(0) ' default out precision
  Help = Help & ByteToMemoryString(0) ' default clip precision
  Help = Help & ByteToMemoryString(0) ' default quality
  Help = Help & ByteToMemoryString(0) ' default pitch
  Help = Help & "Tahoma" ' most popular and existing TTF
  font = CreateFontIndirect(Help)
 
  If font = 0 Then
    DeleteDC (MyPrinterDC)
    Exit Function
  End If
 
  ' Prepare MyPrinterDC
  oldFont = SelectObject(MyPrinterDC, font)

  Result = SetBkMode(MyPrinterDC, TRANSPARENT)
  Result = SetTextColor(MyPrinterDC, clr)  ' 0 for black
 
  Help = LongToMemoryString(5 * 4) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0)

  If StartDoc(MyPrinterDC, Help) <= 0 Then
    Result = SelectObject(MyPrinterDC, oldFont)
    DeleteDC (MyPrinterDC)
    DeleteObject (font)
    Exit Function
  End If

  StartPage (MyPrinterDC)

  MyStartTable = True
End Function



Public Function MyPrintRow(ByRef colText() As String, ByRef colWidth() As Single, _
                           ByVal nCol As Long, ByVal RowHeight as Single) As Boolean
  Dim i as long
  Dim tempLeft as long
  Dim tempTop as long
  Dim tempRight as long
  Dim tempBottom as long

  MyPrintRow = False

  tempLeft = 0
  tempTop = 0
  tempBottom = (RowHeight * 203.0)

  For i = 0 to (nCol - 1)
    tempRight = tempLeft + (ColWidth(i) * 203.0)
    DrawText(MyPrinterDC, colText(i), -1, doRect(tempLeft, tempTop, tempRight, tempBottom), DRAW_TEXT_DRAW)
    tempLeft = tempLeft + (ColWidth(i) * 203.0)
  Next i

  MyPrintRow = True
End Function



Public Function MyEndTable() as Boolean

  Dim Result As Long
 
  MyEndTable = False

  EndPage (MyPRinterDC)
   
  If EndDoc(MyPrinterDC) <= 0 Then
    Result = SelectObject(MyPrinterDC, oldFont)
    DeleteDC (MyPrinterDC)
    DeleteObject (font)
    Exit Function
  End If

  ' finally - cleanup
  Result = SelectObject(MyPrinterDC, oldFont)
  DeleteDC (MyPrinterDC)
  DeleteObject (font)

  MyEndTable = True
End Function
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11826690

Do you need a line or will dashes work?  Like

DATE        C1       C2
-----------  -------  -------

Mind you you could also set the isUnderline parameter and maybe pass it "DATE      " with some spaces so it underlines the spaces.

Now, if that is not good enough then you can draw graphics on the page as well.  We can do that if you need to....but for now lets focus on getting the columns printing.

I'm going to be away for the next two days.  Though I'll probably log in in the evenings both days.  So, what you should do is to familirize yourself with the code.  Heck, you will probably figure it out before I get back to you next.  You pretty much have everything you need to create your specific print table function.  Still, I'll try to log in tomorrow night.  And a bit more tonight.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11826724

Oh, I just realized that we are probably not dealing with the rowHeight properly.  What we need to pass is a rowOffset.  Otherwise all the rows will be printed on the same line (at least that might be what happens).  Try the above anyways and see if it prints all the rows on the same line.  If it does print all on the same line then we will need to pass some offset that we increment before each call to MyPrintRow.  Mind you if we just abandon the idea of createing a general print table function and instead just write the code to print your specific table we could just avoid all this.  We can try that later.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11827247


Heck, if you can discribe how your data is organized then I could just write the code to print your data directly rather then spend so much time trying to get a more robust solution working (like we are trying to do above).  For instance, lets say that you have 4 arrays of data.  Just because I don't know what types of data you are dealing with.  Lets say that you are a teacher and you have students and you are tracking two (2) quizes and a Final exam.  So all of that is in some database, but you read that database data into some arrays (or into some controls, but let me use arrays and you can adjust it for whatever you are doing).  So you might have these three globals:

Public DataName() As String
Public DataQuiz1() as Long
Public DataQuiz2() as Long
Public DataExam() as Long
Public NumStudents as Long

And somewhere in your code you ReDim them and fill them with data from your database (and set NumStudents to the number of students that you will be displaying).

Ok, so if that is similar to what you are working with then you could write one function that prints the student table like this:


Public Function PrintStudentTable() As Boolean

  On Error Resume Next ' for DEBUGGING purposes comment it
 
  Dim font, weight As Long
  Dim dc As Long
  Dim i As Long
  Dim tempLeft as long
  Dim tempTop as long
  Dim tempRight as long
  Dim tempBottom as long
  Dim Help As String
  Dim points as Long
  Dim isBold as Boolean
  Dim isItalic as Boolean
  Dim isUnderline as Boolean
  Dim isStrike as Boolean
  Dim PageLeft, PageRight, PageTop, PageBottom, CalcRight, CalcBottom As Long
  Dim oldFont As Long
  Dim Result As Long
  Dim CurX As Long
  Dim CurY As Long

  PrintStudentTable = False
 
  dc = GetPrinterDC(True)
  If dc = 0 Then Exit Function

  points = 10
  isBold = True
  isItalic = False
  isUnderline = False
  isStrike = False
 
  If isBold Then weight = 700 Else weight = 400
  Help = LongToMemoryString(-(points * GetDeviceCaps(dc, LOGPIXELSY)) / 72) ' font size
  Help = Help & LongToMemoryString(0) ' appropriate width
  Help = Help & LongToMemoryString(0) ' no escapment
  Help = Help & LongToMemoryString(0) ' no orientation
  Help = Help & LongToMemoryString(weight) ' some weight THIN=100,NORMAL=400,BOLD=700
  Help = Help & ByteToMemoryString(isItalic) ' italic
  Help = Help & ByteToMemoryString(isUnderline) ' underline
  Help = Help & ByteToMemoryString(isStrike) ' strikeout
  Help = Help & ByteToMemoryString(0) ' default charset
  Help = Help & ByteToMemoryString(0) ' default out precision
  Help = Help & ByteToMemoryString(0) ' default clip precision
  Help = Help & ByteToMemoryString(0) ' default quality
  Help = Help & ByteToMemoryString(0) ' default pitch
  Help = Help & "Tahoma" ' most popular and existing TTF
  font = CreateFontIndirect(Help)
 
  If font = 0 Then
    DeleteDC (dc)
    Exit Function
  End If
 
  ' Prepare DC
  oldFont = SelectObject(dc, font)

  Result = SetBkMode(dc, TRANSPARENT)
  Result = SetTextColor(dc, clr)  ' 0 for black
 
  PageLeft = 0
  PageTop = 0
  PageRight = pageWidth
  PageBottom = pageHeight
 
  Help = LongToMemoryString(5 * 4) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0)

  If StartDoc(dc, Help) <= 0 Then
    Result = SelectObject(dc, oldFont)
    DeleteDC (dc)
    DeleteObject (font)
    Exit Function
  End If

  StartPage (dc)


  'Print the heading
  CurX = 0
  CurY = 0
  RowWidth = (1.0 * 203)
  RowHeight = (0.25 * 203)
  DrawText(dc, "Name", -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight, DRAW_TEXT_DRAW)

  CurX = CurX + RowWidth
  RowWidth = (0.5 * 203)
  DrawText(dc, "Q1", -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight, DRAW_TEXT_DRAW)

  CurX = CurX + RowWidth
  RowWidth = (0.5 * 203)
  DrawText(dc, "Q2", -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight, DRAW_TEXT_DRAW)

  CurX = CurX + RowWidth
  RowWidth = (0.5 * 203)
  DrawText(dc, "Exam", -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight, DRAW_TEXT_DRAW)

  'Finish the heading by moveing to the next line
  CurX = 0
  CurY = CurY + RowHeight



  'Print a line here (We will figure this out later)
  'For now lets just print dashes under each heading
  RowWidth = (1.0 * 203)
  RowHeight = (0.25 * 203)
  DrawText(dc, "-----", -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight, DRAW_TEXT_DRAW)

  CurX = CurX + RowWidth
  RowWidth = (0.5 * 203)
  DrawText(dc, "---", -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight, DRAW_TEXT_DRAW)

  CurX = CurX + RowWidth
  RowWidth = (0.5 * 203)
  DrawText(dc, "---", -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight, DRAW_TEXT_DRAW)

  CurX = CurX + RowWidth
  RowWidth = (0.5 * 203)
  DrawText(dc, "----", -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight, DRAW_TEXT_DRAW)

  'Move to the next line
  CurX = 0
  CurY = CurY + RowHeight



  'Print the data here (remember the data is inside of the global arrays)
  For i = 0 to (NumStudents - 1)
    RowWidth = (1.0 * 203)
    RowHeight = (0.25 * 203)
    DrawText(dc, DataName(i), -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight, DRAW_TEXT_DRAW)

    CurX = CurX + RowWidth
    RowWidth = (0.5 * 203)
    DrawText(dc, DataQuiz1(i), -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight, DRAW_TEXT_DRAW)

    CurX = CurX + RowWidth
    RowWidth = (0.5 * 203)
    DrawText(dc, DataQuiz2(i), -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight, DRAW_TEXT_DRAW)

    CurX = CurX + RowWidth
    RowWidth = (0.5 * 203)
    DrawText(dc, DataExam(i), -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight, DRAW_TEXT_DRAW)

    'Move to the next line
    CurX = 0
    CurY = CurY + RowHeight
  Next i

 

  EndPage (dc)
   
  If EndDoc(dc) <= 0 Then
    Result = SelectObject(dc, oldFont)
    DeleteDC (dc)
    DeleteObject (font)
    Exit Function
  End If

  ' finally - cleanup
  Result = SelectObject(dc, oldFont)
  DeleteDC (dc)
  DeleteObject (font)

  PrintStudentTable = True
End Function
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11827271

And to use that code you just need to fill up your data arrays.  I think you are reading the data from some database or something, but since I don't know how you are getting the data I'll just hard code it.  So, as I said above, you would already have your globals defined:


Public DataName() As String
Public DataQuiz1() as Long
Public DataQuiz2() as Long
Public DataExam() as Long
Public NumStudents as Long


And you would just fill up those arrays appropriately and then call the PrintStudentTable function.  For example, I hard coded them thusly:


NumStudents = 4
ReDim DataName(NumStudents) As String
ReDim DataQuiz1(NumStudents) As Long
ReDim DataQuiz2(NumStudents) As Long
ReDim DataExam(NumStudents) As Long

DataName(0) = "Jim"
DataQuiz1(0) = 78
DataQuiz2(0) = 81
DataExam(0) = 92

DataName(1) = "Tom"
DataQuiz1(1) = 77
DataQuiz2(1) = 79
DataExam(1) = 82

DataName(2) = "Pat"
DataQuiz1(2) = 42
DataQuiz2(2) = 32
DataExam(2) = 98      'Ahhhhh she cheated

DataName(3) = "Sam"
DataQuiz1(3) = 87
DataQuiz2(3) = 79
DataExam(3) = 89

PrintStudentTable
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11827274

Ok, give all that a try.  I will be out of my office for the next two days, but I'll try to log in tomorrow night sometime.  I thin that that last sample should give you a good idea of what you need to do so review the code and if you have questions just ask.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11827278

Keep in mind.....I can't compile any of this....so it probably has some simple syntax errors.
0
 

Author Comment

by:markh51
ID: 11835026
Whoopeee !

It's prints just perfect ! I used your function above and modified it, to work with my record set.

The only thing which I noticed is that you have to use the same font (size/bold etc) for the headings and the data. Is there any way we can use a bold font for the headings and a standard font for the data ?

This is my working function which I modified from yours, change this one if you want to add or remove any code:

____________________________________________

Public Function PrintHistoryTable() As Boolean

 'On Error Resume Next
 
  Dim font, weight As Long
  Dim dc As Long
  Dim I As Long
  Dim tempLeft As Long
  Dim tempTop As Long
  Dim tempRight As Long
  Dim tempBottom As Long
  Dim Help As String
  Dim points As Long
  Dim isBold As Boolean
  Dim isItalic As Boolean
  Dim isUnderline As Boolean
  Dim isStrike As Boolean
  Dim PageLeft, PageRight, PageTop, PageBottom, CalcRight, CalcBottom As Long
  Dim oldFont As Long
  Dim Result As Long
  Dim CurX As Long
  Dim CurY As Long
  Dim RowWidth As Long
  Dim RowHeight As Long

  PrintHistoryTable = False
 
  dc = GetPrinterDCEx(True)
  If dc = 0 Then Exit Function

  points = 12
  isBold = True
  isItalic = False
  isUnderline = False
  isStrike = False
 
  If isBold Then weight = 700 Else weight = 400
  Help = LongToMemoryString(-(points * GetDeviceCaps(dc, LOGPIXELSY)) / 72) ' font size
  Help = Help & LongToMemoryString(0) ' appropriate width
  Help = Help & LongToMemoryString(0) ' no escapment
  Help = Help & LongToMemoryString(0) ' no orientation
  Help = Help & LongToMemoryString(weight) ' some weight THIN=100,NORMAL=400,BOLD=700
  Help = Help & ByteToMemoryString(isItalic) ' italic
  Help = Help & ByteToMemoryString(isUnderline) ' underline
  Help = Help & ByteToMemoryString(isStrike) ' strikeout
  Help = Help & ByteToMemoryString(0) ' default charset
  Help = Help & ByteToMemoryString(0) ' default out precision
  Help = Help & ByteToMemoryString(0) ' default clip precision
  Help = Help & ByteToMemoryString(0) ' default quality
  Help = Help & ByteToMemoryString(0) ' default pitch
  Help = Help & "Tahoma" ' most popular and existing TTF
  font = CreateFontIndirect(Help)
 
  If font = 0 Then
    DeleteDCEx (dc)
    Exit Function
  End If

  oldFont = SelectObject(dc, font)

  Result = SetBkMode(dc, TRANSPARENT)
  Result = SetTextColor(dc, 0)
 
  PageLeft = 0
  PageTop = 0
  PageRight = pageWidth
  PageBottom = pageHeight
 
  Help = LongToMemoryString(5 * 4) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0)

  If StartDocEx(dc, Help) <= 0 Then
    Result = SelectObject(dc, oldFont)
    DeleteDCEx (dc)
    DeleteObject (font)
    Exit Function
  End If

  StartPageEx (dc)

  CurX = 0
  CurY = 0
  RowWidth = (0.55 * 203)
  RowHeight = (0.2 * 203)
  DrawText dc, "DATE", -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight), DRAW_TEXT_DRAW

  CurX = CurX + RowWidth
  RowWidth = (0.4 * 203)
  DrawText dc, "C.IN", -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight), DRAW_TEXT_DRAW

  CurX = CurX + RowWidth
  RowWidth = (0.4 * 203)
  DrawText dc, "C.OUT", -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight), DRAW_TEXT_DRAW

  CurX = CurX + RowWidth
  RowWidth = (0.2 * 203)
  DrawText dc, "%", -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight), DRAW_TEXT_DRAW
 
  CurX = CurX + RowWidth
  RowWidth = (0.4 * 203)
  DrawText dc, "TOTAL", -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight), DRAW_TEXT_DRAW

  CurX = 0
  CurY = CurY + RowHeight

  For I = 0 To (NumRecords - 1)
    RowWidth = (0.55 * 203)
    RowHeight = (0.15 * 203)
    DrawText dc, HistoryDate(I), -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight), DRAW_TEXT_DRAW

    CurX = CurX + RowWidth
    RowWidth = (0.4 * 203)
    DrawText dc, HistoryCashIn(I), -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight), DRAW_TEXT_DRAW

    CurX = CurX + RowWidth
    RowWidth = (0.4 * 203)
    DrawText dc, HistoryCashOut(I), -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight), DRAW_TEXT_DRAW

    CurX = CurX + RowWidth
    RowWidth = (0.2 * 203)
    DrawText dc, HistoryCommission(I), -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight), DRAW_TEXT_DRAW
   
    CurX = CurX + RowWidth
    RowWidth = (0.4 * 203)
    DrawText dc, FormatCurrency(HistoryTotal(I)), -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight), DRAW_TEXT_DRAW

    CurX = 0
    CurY = CurY + RowHeight
  Next I

  EndPageEx (dc)
   
  If EndDocEx(dc) <= 0 Then
    Result = SelectObject(dc, oldFont)
    DeleteDCEx (dc)
    DeleteObject (font)
    Exit Function
  End If

  Result = SelectObject(dc, oldFont)
  DeleteDCEx (dc)
  DeleteObject (font)
  PrintHistoryTable = True
End Function

_________________________________________________
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11837567

Well, that is good news.  I knew we should have just focused on making your function work with your code rather then try to make it more universal.

As far as the font, you can change that all you want.  Just remember that you need to delete the font object after you use it.  I don't have much time, but maybe try createing two function like this:


Public Function MyCreateFont(ByRef dc As Long, ByRef FontHandle As Long, ByVal points As Long, ByVal clr As Long, _
    ByVal isBold As Boolean, ByVal isItalic As Boolean, ByVal isStrike As Boolean, _
    ByVal isUnderline As Boolean) As Long
 
  Dim Result as Long
  Dim oldFont as Long
  Dim Help as String
 
  MyCreateFont = 0

  If isBold Then weight = 700 Else weight = 400
  Help = LongToMemoryString(-(points * GetDeviceCaps(dc, LOGPIXELSY)) / 72) ' font size
  Help = Help & LongToMemoryString(0) ' appropriate width
  Help = Help & LongToMemoryString(0) ' no escapment
  Help = Help & LongToMemoryString(0) ' no orientation
  Help = Help & LongToMemoryString(weight) ' some weight THIN=100,NORMAL=400,BOLD=700
  Help = Help & ByteToMemoryString(isItalic) ' italic
  Help = Help & ByteToMemoryString(isUnderline) ' underline
  Help = Help & ByteToMemoryString(isStrike) ' strikeout
  Help = Help & ByteToMemoryString(0) ' default charset
  Help = Help & ByteToMemoryString(0) ' default out precision
  Help = Help & ByteToMemoryString(0) ' default clip precision
  Help = Help & ByteToMemoryString(0) ' default quality
  Help = Help & ByteToMemoryString(0) ' default pitch
  Help = Help & "Tahoma" ' most popular and existing TTF
  font = CreateFontIndirect(Help)
 
  If font = 0 Then
    Exit Function
  End If

  oldFont = SelectObject(dc, font)

  Result = SetTextColor(dc, clr)

  MyCreateFont = oldFont
End Function


Public Function MyDestroyFont(ByRef dc As Long, ByRef Font as Long, ByRef oldFont as Long) As Boolean
  Dim Result as Long

  MyDestroyFont = False

  Result = SelectObject(dc, oldFont)
  DeleteObject (font)

  MyDestroyFont = True
End Function
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11837576

So pull out all that code from the other function and replace it with a call to these functions.  Notice that I left the line:

  Result = SetBkMode(dc, TRANSPARENT)

In the main function but I moved the line:

  Result = SetTextColor(dc, clr)

Technically you should probably not move the change in color either since it really isn't related to the font object.  Maybe you should move that back out.  In any case, the above probably has some syntax errors since I'm in a little bit of a rush tonight.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11837583

Also, notice that the MyCreateFont function returns the handle of the old font.  It also returns the handle of the new font.  You will need both in order to destroy (and reset) the font.  It is important that for ever font you create you make sure you destroy it before you create another font.  Otherwise you will end up with a memory leak (though that might not be a big deal depending on your application).  Still, its a good idea to always destroy the font you created after you use it.
0
 

Author Comment

by:markh51
ID: 11838461
That doesn't work !

Are you sure that you can change the font after StartDoc is called as it uses StartDoc(dc, help) and the font is part of the help structure. So, changing the font after StartDoc has been called will have no affect. Is this right or am I on the wrong track ?

I can't see a way of changing the font once the StartDoc function has been call except from calling EndDoc, then calling StartDoc again with the new font, but as you know this will create the end of page line feed problem again !

Let me know what you think.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11848834

Actually, startdoc uses help, but help is just a string.  Since eVB does not support "type" you can't pass the required type structure to all of these API functions.  So, what they do is they build a string that takes up the same space that the strucutre does and then pass that string.  What actually gets passed is the address of the memory location that contains the string and what the API does is to interpret that memory location as a structure (internally) instead of as a string.  So, what it is doing is using Help twice for different reasons.  It uses the string (help) to pass the font structure and then it uses the same variable (help) to pass a structure to the StartDoc function.  But they are unrelated.

I still think that it should work.  Maybe since I didn't have much time last night I just didn't type it in correctly.  Let me take another look at it.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11848851

Ok, first off lets take out that color stuff.  That shouldn't be in those function anyways.  So, use these functions instead.  And let me modify your code to test it....coming next....

Public Function MyCreateFont(ByRef dc As Long, ByRef FontHandle As Long, ByVal points As Long, _
    ByVal isBold As Boolean, ByVal isItalic As Boolean, ByVal isStrike As Boolean, _
    ByVal isUnderline As Boolean) As Long
 
  Dim oldFont as Long
  Dim Help as String
 
  MyCreateFont = 0

  If isBold Then weight = 700 Else weight = 400
  Help = LongToMemoryString(-(points * GetDeviceCaps(dc, LOGPIXELSY)) / 72) ' font size
  Help = Help & LongToMemoryString(0) ' appropriate width
  Help = Help & LongToMemoryString(0) ' no escapment
  Help = Help & LongToMemoryString(0) ' no orientation
  Help = Help & LongToMemoryString(weight) ' some weight THIN=100,NORMAL=400,BOLD=700
  Help = Help & ByteToMemoryString(isItalic) ' italic
  Help = Help & ByteToMemoryString(isUnderline) ' underline
  Help = Help & ByteToMemoryString(isStrike) ' strikeout
  Help = Help & ByteToMemoryString(0) ' default charset
  Help = Help & ByteToMemoryString(0) ' default out precision
  Help = Help & ByteToMemoryString(0) ' default clip precision
  Help = Help & ByteToMemoryString(0) ' default quality
  Help = Help & ByteToMemoryString(0) ' default pitch
  Help = Help & "Tahoma" ' most popular and existing TTF
  font = CreateFontIndirect(Help)
 
  If font = 0 Then
    Exit Function
  End If

  oldFont = SelectObject(dc, font)

  MyCreateFont = oldFont
End Function


Public Function MyDestroyFont(ByRef dc As Long, ByRef Font as Long, ByRef oldFont as Long) As Boolean
  Dim Result as Long

  MyDestroyFont = False

  Result = SelectObject(dc, oldFont)
  DeleteObject (font)

  MyDestroyFont = True
End Function
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11848881

Ok, and here is the function that you can try.  See how I use the font and then I destroy it before I reuse it.  Tell me what it does?


Public Function PrintHistoryTable() As Boolean

 'On Error Resume Next
 
  Dim font As Long
  Dim oldFont As Long
  Dim dc As Long
  Dim I As Long
  Dim Help As String
  Dim Result As Long
  Dim CurX As Long
  Dim CurY As Long
  Dim RowWidth As Long
  Dim RowHeight As Long

  PrintHistoryTable = False
 
  dc = GetPrinterDCEx(True)
  If dc = 0 Then Exit Function

  Result = SetBkMode(dc, TRANSPARENT)
  Result = SetTextColor(dc, 0)
 
  Help = LongToMemoryString(5 * 4) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0) & _
         LongToMemoryString(0)

  If StartDocEx(dc, Help) <= 0 Then
    Result = SelectObject(dc, oldFont)
    DeleteDCEx (dc)
    DeleteObject (font)
    Exit Function
  End If

  StartPageEx (dc)

  oldFont = MyCreateFont(dc, font, 12, True, True, False, True)

  CurX = 0
  CurY = 0
  RowWidth = (0.55 * 203)
  RowHeight = (0.2 * 203)
  DrawText dc, "DATE", -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight), DRAW_TEXT_DRAW

  CurX = CurX + RowWidth
  RowWidth = (0.4 * 203)
  DrawText dc, "C.IN", -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight), DRAW_TEXT_DRAW

  CurX = CurX + RowWidth
  RowWidth = (0.4 * 203)
  DrawText dc, "C.OUT", -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight), DRAW_TEXT_DRAW

  CurX = CurX + RowWidth
  RowWidth = (0.2 * 203)
  DrawText dc, "%", -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight), DRAW_TEXT_DRAW
 
  CurX = CurX + RowWidth
  RowWidth = (0.4 * 203)
  DrawText dc, "TOTAL", -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight), DRAW_TEXT_DRAW

  CurX = 0
  CurY = CurY + RowHeight

  MyDestroyFont (dc, font, OldFont)
 
  oldFont = MyCreateFont(dc, font, 9, False, False, False, False)

  For I = 0 To (NumRecords - 1)
    RowWidth = (0.55 * 203)
    RowHeight = (0.15 * 203)
    DrawText dc, HistoryDate(I), -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight), DRAW_TEXT_DRAW

    CurX = CurX + RowWidth
    RowWidth = (0.4 * 203)
    DrawText dc, HistoryCashIn(I), -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight), DRAW_TEXT_DRAW

    CurX = CurX + RowWidth
    RowWidth = (0.4 * 203)
    DrawText dc, HistoryCashOut(I), -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight), DRAW_TEXT_DRAW

    CurX = CurX + RowWidth
    RowWidth = (0.2 * 203)
    DrawText dc, HistoryCommission(I), -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight), DRAW_TEXT_DRAW
   
    CurX = CurX + RowWidth
    RowWidth = (0.4 * 203)
    DrawText dc, FormatCurrency(HistoryTotal(I)), -1, doRect(CurX, CurY, CurX + RowWidth, CurY + RowHeight), DRAW_TEXT_DRAW

    CurX = 0
    CurY = CurY + RowHeight
  Next I

  MyDestroyFont (dc, font, OldFont)

  EndPageEx (dc)
   
  If EndDocEx(dc) <= 0 Then
    Result = SelectObject(dc, oldFont)
    DeleteDCEx (dc)
    DeleteObject (font)
    Exit Function
  End If

  Result = SelectObject(dc, oldFont)
  DeleteDCEx (dc)
  DeleteObject (font)
  PrintHistoryTable = True
End Function
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11848906

Let me just talk a moment to throw in the answer to the meaning of life at this point.  Just in case anyone has read this thread to this point.  The answer to the meaning of life is......42.  I can't think of a better place to hide that answer then a super long thread about printing in eVB....hee hee.

This is my longest thread yet.  But I think we are close to the solution.  My fingers are crossed.

0
 

Author Comment

by:markh51
ID: 11849603
Cheers mate... I won't be able to try the code until Saturday as the printer is being repaired at the moment as it has decided to stop charging.

I'll let you know ASAP.

P.S Increasing points for all your hard work ;-)
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11851130

It was fun.  Please remember to tell me if that font change worked.  Take care.

0
 

Author Comment

by:markh51
ID: 11858168
ENLADE:

Them Functions work like a charm !

One quick question before you go... When I use the PrintText function with a point size of 12 compared to using a Point size of 12 using our function, the text prints a lot smaller for the our function.

Any ideas why ?
0
 

Author Comment

by:markh51
ID: 11858240
The point size difference seems to be about 6 points between the 2 functions !

If you can't find out why, then let me know and I'll just award you the points.

I have increased them to 400 for your extra help.

Cheers mate.
0
 
LVL 7

Accepted Solution

by:
Enlade earned 400 total points
ID: 11859347

Not sure why that is.  Maybe its because we didn't declare the weight vaiable in our font creation function (don't think so though).  Still, try declaring it:

Dim weight as Long

Maybe you should also divide by 72.0 rather then just 72 in the line:

 Help = LongToMemoryString(-(points * GetDeviceCaps(dc, LOGPIXELSY)) / 72) ' font size

That is to say, make it:

 Help = LongToMemoryString(-(points * GetDeviceCaps(dc, LOGPIXELSY)) / 72.0) ' font size

Also, make sure you compair our print output with a call to PrintText in the same test code.  Don't compare it to some old test data that you might have still sitting around.

I noticed that you are now using PrinterEX.bas instead of Prnter.bas.  It is not apparent what difference there is between the two files.  Seems the same to me, but maybe there is a difference.

In any case, I'm happy that things are working for you.  Take care.
0
 

Author Comment

by:markh51
ID: 11867949
Thanks for all your help.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now