I've written a program in VB6 that similates a spyrograph toy - you choose the radius of the outer gear, radius of the inner gear, and the offset (the hole where you put the pen), and a pattern is drawn in a picture box. You can choose drawing color, background color, etc., and it allows you to overlay patterns - like when you draw a pattern with one gear, then change gears and draw another on top of it. Works great.
Now I'm trying to add a print function. I'm using PSet() to print to the printer:
VB Code:
Original
- VB Code |
|
|
|
Sub PrintRoullette()
Dim pi, r, R1, R2, i
For i = 0 To UBound(prtR1) - 1
R1 = prtR1(i) ' these 3 arrays and prtPen() are populated each
R2 = prtR2(i) ' time the DrawRoullette() sub is called - they're
r = prtR(i) ' the gear radius' and offset slider values
pi = 4 * Atn(1)
't represents time, X & Y are the coordinates of each point drawn
Dim Loop1, Loop2
Dim t, X, Y As Double
Dim Rotations As Integer
If Int(R1 / R2) = R1 / R2 Then
Rotations = 1
Else
Rotations = Abs(R2 / 10)
If Int(R2 / 10) <> R2 / 10 Then Rotations = 10 * Rotations
End If
Rotations = Rotations + 1
For Loop1 = 1 To Rotations
For Loop2 = 0 To 2 * pi Step pi / (4 * 360)
t = Loop1 * 2 * pi + Loop2
X = (R1 + R2) * Cos(t) - (R2 + r) * Cos(((R1 + R2) / R2) * t)
Y = (R1 + R2) * Sin(t) - (R2 + r) * Sin(((R1 + R2) / R2) * t)
Printer.PSet (Printer.ScaleWidth / 2 + X, Printer.ScaleHeight _
/ 2 + Y), prtPen(i)
Next
DoEvents
Next
Next
End Sub
This is pretty much the same as the DrawRoullette() sub that draws the patterns on the screen. The DrawRoullette() sub draws a pattern in a picture box while loading the prtR1, prtR2, prtR, and prtPen arrays with the current drawing settings (gear radius, etc.). each time the sub is called, it reDims the arrays and adds another element to them.
The print button code is:
VB Code:
Original
- VB Code |
|
|
|
Private Sub cmdPrint_Click()
SetLargePrinterScale Picture1
PrintRoullette
Printer.EndDoc
End Sub
It first sets the printer's scale properties with the SetLargePrinterScale() sub:
VB Code:
Original
- VB Code |
|
|
|
Sub SetLargePrinterScale(obj As Object)
Dim Owid As Single ' object's scale width
Dim Ohgt As Single ' object's scale height
Dim Pwid As Single ' printer's scale width
Dim Phgt As Single ' printer's scale height
Dim Xmid As Single ' middle of page (hor.)
Dim Ymid As Single ' middle of page (vert.)
Dim S As Single
Owid = obj.ScaleX(obj.ScaleWidth, obj.ScaleMode, vbPixels)
Ohgt = obj.ScaleY(obj.ScaleHeight, obj.ScaleMode, vbPixels)
Pwid = Printer.ScaleX(Printer.ScaleWidth, Printer.ScaleMode, vbPixels)
Phgt = Printer.ScaleY(Printer.ScaleHeight, Printer.ScaleMode, vbPixels)
If Ohgt / Owid > Phgt / Pwid Then
S = Phgt / Ohgt
Else
S = Pwid / Owid
End If
Pwid = obj.ScaleX(Pwid, vbPixels, obj.ScaleMode) / S
Phgt = obj.ScaleY(Phgt, vbPixels, obj.ScaleMode) / S
Xmid = obj.ScaleLeft + obj.ScaleWidth / 2
Ymid = obj.ScaleTop + obj.ScaleHeight / 2
Printer.Scale (Xmid - Pwid / 2, Ymid - Phgt / 2)-(Xmid + Pwid / 2, Ymid + Phgt / 2)
End Sub
It then calls the PrintRoullette sub, which draws the pattern to the printer using prtR1(0), prtR2(0), etc., then repeats for each additional array element, overlaying the patterns.
cmdPrint_Click() then calls Printer.EndDoc to send the job to the printer.
This worked great when I was using it from within the VB environment. When I compiled the project, closed VB, and ran the stand-alone exe, I could print one pattern at a time, but if I tried to overlay patterns, that is, drawing more than one pattern to the printer before issuing the Printer.EndDoc command, I get a runtime error '482'.
I've researched the problem, and it seems to be a bug in VB6 - installing service pack 5 was recommended, and I installed SP5 and SP6 - no help.
I reloaded my printer drivers (a clue I got from one of the forums), and after that, I couldn't even print overlaid patterns from within VB - This is going downhill. I've since downloaded the latest drivers, but that didn't help.
I've found that I can overlay patterns if they are extremely simple, so this may be a matter of giving the printer more than it can handle, or perhaps this is a timeout issue?
Has anyone had similar experiences? Can this be done? If not, how do I simply print an image from a picture box? I'd rather do it the way I'm doing it (the resolution is awesome) but I've gotta do what works...
joe