English 中文(简体)
How can I get the printer HDC
原标题:

I have a COM component written in C++ that has a Print function. This print function takes the Printer hDC as a parameter that includes all settings to use for the print. Previously, this was called from VB6 code, and Printer.hdc would work here after setting everything on the Printer object.

The code was converted from VB6 to VB.NET, and I have figured out most of things that I need to do. The old Printer object is available through the Microsoft.VisualBasic.PowerPacks.Printing.Compability.VB6.Printer class, but the old hdc property is not supported here.

Can anyone tell me how to get this hdc? Is this hdc the same as GetHdevmode() on a System.Drawing.Printing.PrinterSettings object?

最佳回答

You can get one out of the Graphics object returned by PrinterSettings.CreateMeasurementGraphics(). Use the Graphics.GetHdc() method. Don t forget ReleaseHdc() after printing.

问题回答

Hdc is not the same as getdevmode, but you can do everything in .net without using hdc. If it saves time using the old code, you can get the hdc from the graphics object and use it as in nobugz s answer. But if you have a graphics object for the printer, it might be simpler to draw directly to the graphics object and skip the hdc altogether.

Here s a similar approach to the one suggested by Hans but it uses a form control. If you are using a form control anyway, this might be a cleaner approach.

Place a PrintDocument from the Windows Forms toolbox to your form.

Then add the following code to handle the print page (as an example):

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
  Dim printerhdc As IntPtr = e.Graphics.GetHdc()

    Do whatever you need to do to get the right image
  XYZ.Load file(currentpagenumber)
  XYZ.Render(printerhdc.ToInt64, 25, 25, Width, Height)

  CurrentPageNumber += 1

  If CurrentPageNumber < TotalPageCount Then
   e.HasMorePages = True
  Else
   e.HasMorePages = False
  End If
  e.Graphics.ReleaseHdc(printerhdc)
End Sub

...

 Gather all the files you need and put their names in an arraylist.
 Then issue the print command
PrintDocument1.Print

  You ve just printed your files

source: http://www.vbforums.com/showthread.php?247493-Good-ol-Printer-hDC

(source: http://www.vbforums.com/showthread.php?247493-Good-ol-Printer-hDC)





相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签