2013-03-26 203 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

相关问题