By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,247 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,247 IT Pros & Developers. It's quick & easy.

Common Dialog printer hdc

P: n/a
Hello,

I'm trying to print the content of a RichTextBox from my VB 6 app.
What I want is that the CommonDialog shows up, the user selects a
printer and the content of the RichTextBox prints to the selected
printer. I'm not concerned about the text format.

What I tried is something like this

CommonDialog1.ShowPrinter

RichTextBox1.SelPrint(CommonDialog1.hdc)

The code did not work, it produced runtime error 32001 - invalid hdc.

I then searched in the Knowledge Base and found some code from
Microsoft for printing RichTextBox contents. However, it looks like
this:

----------------------------------------------------------------------------
Public Sub PrintRTF(RTF As RichTextBox, LeftMarginWidth As Long, _
TopMarginHeight, RightMarginWidth, BottomMarginHeight)
Dim LeftOffset As Long, TopOffset As Long
Dim LeftMargin As Long, TopMargin As Long
Dim RightMargin As Long, BottomMargin As Long
Dim fr As FormatRange
Dim rcDrawTo As Rect
Dim rcPage As Rect
Dim TextLength As Long
Dim NextCharPosition As Long
Dim r As Long

' Start a print job to get a valid Printer.hDC
Printer.Print Space(1)
Printer.ScaleMode = vbTwips

' Get the offsett to the printable area on the page in twips
' some code here
' Calculate the Left, Top, Right, and Bottom margins
' some code here
' Set printable area rect
' some code here
' Set rect in which to print (relative to printable area)
' some code here

' Set up the print instructions
fr.hdc = Printer.hdc ' Use the same DC for measuring and
rendering
fr.hdcTarget = Printer.hdc ' Point at printer hDC
fr.rc = rcDrawTo ' Indicate the area on page to draw to
fr.rcPage = rcPage ' Indicate entire size of page
fr.chrg.cpMin = 0 ' Indicate start of text through
fr.chrg.cpMax = -1 ' end of the text

' Get length of text in RTF
TextLength = Len(RTF.Text)

' Loop printing each page until done
Do
' Print the page by sending EM_FORMATRANGE message
NextCharPosition = SendMessage(RTF.hWnd, EM_FORMATRANGE, True,
fr)
If NextCharPosition >= TextLength Then Exit Do 'If done then
exit
fr.chrg.cpMin = NextCharPosition ' Starting position for next
page
Printer.NewPage ' Move on to next page
Printer.Print Space(1) ' Re-initialize hDC
fr.hdc = Printer.hdc
fr.hdcTarget = Printer.hdc
Loop

' Commit the print job
Printer.EndDoc

' Allow the RTF to free up memory
r = SendMessage(RTF.hWnd, EM_FORMATRANGE, False, ByVal CLng(0))
End Sub
------------------------------------------------------------------------------

This code works but it takes the printer hdc from the Printer object.

Looking at the values of the hdcs the CommonDialog1.hdc always
contains 0, the Printer.hdc always contains some long number
(obviously a valid printer hdc).

Am I making a mistake? or is the printer dialog useless for selecting
a printer? How can I get the hdc of the selected printer from the
CommonDialog?

patrick
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
What makes you think that the hDC of the PrintDialog
(a glorified MessageBox)
is the same as the hDC of the current Printer?

This helpful little bit comes from the VB5 Help Files

<snip>
The SelPrint method does not print text from the RichTextBox control.
Rather, it sends a copy of formatted text to a device which can print
the text. For example, you can send the text to the Printer object
using code as follows:

RichTextBox1.SelPrint(Printer.hDC)

Notice that the hDC property of the Printer object is used to specify
the device context argument of the SelPrint method.

Note If you use the Printer object as the destination of the text
from the RichTextBox control, you must first initialize the device
context of the Printer object by printing something like a zero-length
string.

</snip>

The CommonDialog stuff is totally misleading

On 3 Jul 2003 06:26:33 -0700, Pa**********@mailbox.tu-dresden.de
(Patrick Herb) wrote:
Hello,

I'm trying to print the content of a RichTextBox from my VB 6 app.
What I want is that the CommonDialog shows up, the user selects a
printer and the content of the RichTextBox prints to the selected
printer. I'm not concerned about the text format.

What I tried is something like this

CommonDialog1.ShowPrinter

RichTextBox1.SelPrint(CommonDialog1.hdc)

The code did not work, it produced runtime error 32001 - invalid hdc.

I then searched in the Knowledge Base and found some code from
Microsoft for printing RichTextBox contents. However, it looks like
this:

----------------------------------------------------------------------------
Public Sub PrintRTF(RTF As RichTextBox, LeftMarginWidth As Long, _
TopMarginHeight, RightMarginWidth, BottomMarginHeight)
Dim LeftOffset As Long, TopOffset As Long
Dim LeftMargin As Long, TopMargin As Long
Dim RightMargin As Long, BottomMargin As Long
Dim fr As FormatRange
Dim rcDrawTo As Rect
Dim rcPage As Rect
Dim TextLength As Long
Dim NextCharPosition As Long
Dim r As Long

' Start a print job to get a valid Printer.hDC
Printer.Print Space(1)
Printer.ScaleMode = vbTwips

' Get the offsett to the printable area on the page in twips
' some code here
' Calculate the Left, Top, Right, and Bottom margins
' some code here
' Set printable area rect
' some code here
' Set rect in which to print (relative to printable area)
' some code here

' Set up the print instructions
fr.hdc = Printer.hdc ' Use the same DC for measuring and
rendering
fr.hdcTarget = Printer.hdc ' Point at printer hDC
fr.rc = rcDrawTo ' Indicate the area on page to draw to
fr.rcPage = rcPage ' Indicate entire size of page
fr.chrg.cpMin = 0 ' Indicate start of text through
fr.chrg.cpMax = -1 ' end of the text

' Get length of text in RTF
TextLength = Len(RTF.Text)

' Loop printing each page until done
Do
' Print the page by sending EM_FORMATRANGE message
NextCharPosition = SendMessage(RTF.hWnd, EM_FORMATRANGE, True,
fr)
If NextCharPosition >= TextLength Then Exit Do 'If done then
exit
fr.chrg.cpMin = NextCharPosition ' Starting position for next
page
Printer.NewPage ' Move on to next page
Printer.Print Space(1) ' Re-initialize hDC
fr.hdc = Printer.hdc
fr.hdcTarget = Printer.hdc
Loop

' Commit the print job
Printer.EndDoc

' Allow the RTF to free up memory
r = SendMessage(RTF.hWnd, EM_FORMATRANGE, False, ByVal CLng(0))
End Sub
------------------------------------------------------------------------------

This code works but it takes the printer hdc from the Printer object.

Looking at the values of the hdcs the CommonDialog1.hdc always
contains 0, the Printer.hdc always contains some long number
(obviously a valid printer hdc).

Am I making a mistake? or is the printer dialog useless for selecting
a printer? How can I get the hdc of the selected printer from the
CommonDialog?

patrick


Jul 17 '05 #2

P: n/a
> Am I making a mistake? or is the printer dialog useless for selecting
a printer? How can I get the hdc of the selected printer from the
CommonDialog?


Unfortunately, the printer dialog IS pretty useless for selecting a
printer. I've spent weeks to solve this problem, and ended up creating
my own dialog for it (That's very simple - you just need to write the
printer names in the printer collection to a drop-down list, then see
which one the user selected, and print to the printer with that name.
The problem is the look-and-feel, and all printer-specific options
which are about impossible in VB).

The only way I found how the printer dialog will do anything is if you
set the "PrinterDefault" property - then it will change the default
printer. In this case, you can just use the Printer object in VB

Robert
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.