2013-03-26 201 views
0

這是一個小型計費軟件的代碼,必須在熱敏打印機上進行打印。以下是我的代碼,適用於激光打印機。我想知道這個代碼是否適用於熱敏打印機,或者是否應該專門爲這些打印機更改代碼。如果這麼好心幫助我一些代碼。日Thnx提前:)使用vb.net從datagridview打印到熱敏打印機

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage 
    mRow = 0 
    Try 
     Dim big, small As Font 
     big = New Font(Font.FontFamily, 20, FontStyle.Bold) 
     small = New Font(Font.FontFamily, 10, FontStyle.Bold) 
     newpage = True 
     With DGVView 
      Dim fmt As StringFormat = New StringFormat(StringFormatFlags.FitBlackBox) 
      fmt.LineAlignment = StringAlignment.Near 
      fmt.Trimming = StringTrimming.None 
      Dim y As Single = e.MarginBounds.Top 
      Do While mRow < .RowCount 
       Dim row As DataGridViewRow = .Rows(mRow) 
       Dim x As Single = e.MarginBounds.Left 
       Dim h As Single = 0 
       For Each cell As DataGridViewCell In row.Cells 
        Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height) 
        e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height) 
        If (newpage = True) Then 
         PrintDocument1.PrinterSettings.DefaultPageSettings.PaperSize = New PaperSize("custom format", 300, 300) 
         'e.Graphics.DrawString("    AYYA NADAR JANAKI AMMAL COLLEGE", big, Brushes.Black, 5, 50) 
         e.Graphics.DrawString(DGVView.Columns(cell.ColumnIndex).HeaderText, small, Brushes.Black, rc, fmt) 
        Else 
         e.Graphics.DrawString(DGVView.Rows(cell.RowIndex - 1).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt) 
        End If 
        x += rc.Width 
        h = Math.Max(h, rc.Height) 
       Next 
       newpage = False 
       y += h 
       mRow += 1 
       If y + h > e.MarginBounds.Bottom Then 
        e.HasMorePages = True 
        mRow -= 1 
        newpage = True 
        Exit Sub 
       End If 
      Loop 

      mRow = 0 
     End With 
    Catch 
     MsgBox("Unexpected Error Occured. Sorry for the inconvenience") 
    End Try 
+0

嘗試它會不會更容易? – LarsTech 2013-03-26 17:45:05

回答

0

的圖形方法是設備獨立的(該graphics class「封裝一個GDI +繪圖表面」) - 使用它們時,他們依靠打印機設備驅動程序的打印機,所以沒有你贏了不必改變你的代碼。

聲明:您有時會發現不同設備的行爲稍有不同,所以最簡單的方法就是嘗試!

+0

嘗試過,併成功 謝謝frnz :) – 2013-03-28 08:49:57

相關問題