2010-01-12 184 views
1

我有用C++編寫的具有打印功能的COM組件。此打印功能將打印機hDC作爲參數,其中包含用於打印的所有設置。以前,這是從VB6代碼調用的,Printer.hdc可以在設置Printer對象上的所有內容後生效。如何獲得打印機HDC

該代碼從VB6轉換爲VB.NET,並且我已經找出了我需要做的大部分事情。舊的Printer對象可通過Microsoft.VisualBasic.PowerPacks.Printing.Compability.VB6.Printer類獲得,但此處不支持舊的hdc屬性。

誰能告訴我如何得到這個hdc? 這個hdc與對象上的GetHdevmode()相同嗎?

回答

2

您可以從PrinterSettings.CreateMeasurementGraphics()返回的Graphics對象中取出一個。使用Graphics.GetHdc()方法。打印後不要忘記ReleaseHdc()。

+0

當我創建一個新的'PrinterSettings'對象,它實際上是使用之前設置了值的兼容性「Printer」對象的值初始化的。因此,這與調用'Printer.hdc'相同! 非常感謝! – awe 2010-01-13 12:01:26

1

Hdc與getdevmode不同,但是您可以在不使用hdc的情況下在.net中執行所有操作。如果使用舊代碼節省時間,您可以從圖形對象獲取hdc,並像nobugz的回答一樣使用它。但是,如果您有打印機的圖形對象,直接繪製到圖形對象並跳過HDC可能會更簡單。

0

下面是一個類似的方法,但是它使用了一個窗體控件suggested by Hans。如果你正在使用表單控件,這可能是一個更清潔的方法。

將Windows窗體工具箱中的PrintDocument放置到窗體中。

然後添加以下代碼以處理打印頁(作爲例子):

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 

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

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

相關問題