473,545 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Common Dialog printer hdc

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.S howPrinter

RichTextBox1.Se lPrint(CommonDi alog1.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 , RightMarginWidt h, BottomMarginHei ght)
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 NextCharPositio n As Long
Dim r As Long

' Start a print job to get a valid Printer.hDC
Printer.Print Space(1)
Printer.ScaleMo de = 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
NextCharPositio n = SendMessage(RTF .hWnd, EM_FORMATRANGE, True,
fr)
If NextCharPositio n >= TextLength Then Exit Do 'If done then
exit
fr.chrg.cpMin = NextCharPositio n ' 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.h dc 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
2 22262
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.Se lPrint(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**********@ma ilbox.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.S howPrinter

RichTextBox1.Se lPrint(CommonDi alog1.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 , RightMarginWidt h, BottomMarginHei ght)
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 NextCharPositio n As Long
Dim r As Long

' Start a print job to get a valid Printer.hDC
Printer.Print Space(1)
Printer.ScaleMo de = 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
NextCharPositio n = SendMessage(RTF .hWnd, EM_FORMATRANGE, True,
fr)
If NextCharPositio n >= TextLength Then Exit Do 'If done then
exit
fr.chrg.cpMin = NextCharPositio n ' 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.h dc 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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6490
by: John Lauwers | last post by:
Hello, Is there a way to move the commondialog to a specific position ? Greets John
0
3536
by: Rich Sweeny | last post by:
Hi, I am working in Mac OSX 10.3.7 with SDK 1.4_2. When my Print Dialog box comes up just about everything is disabled except selecting from the 4 printers I have and being able to set the page margins. The dialog has 3 panes. The first pane, 'General', I can select my printer but the property button for the printer is disabled along with the...
23
6960
by: George | last post by:
Is there a way to customize the open file common dialog? I am trying to modify the button text so I can create a delete file common dialog. I need the same functionality of the open file common dialog but just need to change the button text from "open" to "delete". Any ideas? Thanks
3
20965
by: Mike | last post by:
Hi, Is there anyway to print from VB.NET or C# and bypass the printer dialog box? Thanks Mike
11
9863
by: pamelafluente | last post by:
I am doing my own PrintDialog, and have placed there a combo with the printer names, as in the PrintDialog provided by VB.NET. Here is the question: how do I open the native windows printer dialog for the current printer, so that my current PrintDocument.PrinterSettings will be set according to the User choices ? Thanks -Pamela
1
3122
devonknows
by: devonknows | last post by:
Good afternoon, ive got a common dialog which calles the print dialog, it prints perfectly, its prints my ListBox contents right, but when i click cancel on the print screen its just Prints it anyway and if i put the .DialogCancel = True in when i cancel it just causes an error and causes the debug menu you to come up, any help on this subject...
2
22103
by: Brad Pears | last post by:
I have some sample code that uses the print dialog, print preview and a print direct options. If I select print preview and then click the printer icon from that, the document prints. If I select the print directly option, it also prints right away to the defauilt printer. However, if I use the printer dialog control to print and I click...
1
2736
by: cmos1981 | last post by:
Visual Basic 6.3 - i am auto printing a screen using vb. i dont want the print dialog box to appear. the page should just print to the default printer. alternatively if i could use "sendkeys" to dialog box this would be okay but vb code waits for a reply to the print dialog box.. The line I am getting an error on is:...
3
9810
by: ARC | last post by:
Hello all, Using the following command, is there any way to get the printer dialog to open? I most packages, when you click print, it will open the printer dialog, however Access sends the report without the dialog. I was hoping there would be another option, other than acNormal, but there doesn't appear to be: DoCmd.OpenReport...
0
7467
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7807
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7419
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5326
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3450
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1879
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
703
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.