作者: Form1程式碼
Public Class Form1
Dim Rec_Count As Integer
Dim p_no As Single
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Single
ToolStripComboBox1.Items.Clear()
For i = 0 To Printing.PrinterSettings.InstalledPrinters.Count - 1
ToolStripComboBox1.Items.Add(Printing.PrinterSettings.InstalledPrinters.Item(i).ToString)
Next
ToolStripComboBox1.Text = PrintDialog1.PrinterSettings.PrinterName
'以下只是為了測試方便,可以省略
Randomize()
Dim n As Single, f As Single
For i = 0 To 100
Dim s As String = "00" & Format(i, "##")
n = 1 + Int(Rnd(1) * 500)
f = 1 + Int(Rnd(1) * 101)
DataGridView1.Rows.Add("阿哉" & s.Substring(s.Length - 2, 2), "00-1234" & 567 + f, "賣燜挖縣麥擱燜挖鄉賣歌債燜挖鎮" & n & "號" & f & "樓")
Next
End Sub
Private Sub ToolStripComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripComboBox1.SelectedIndexChanged
Dim i As Single, k As Single = 0
PrintDialog1.PrinterSettings.PrinterName = ToolStripComboBox1.Text
ToolStripComboBox2.Items.Clear()
For i = 0 To PrintDialog1.PrinterSettings.PaperSources.Count - 1
'ToolStripComboBox2.Items.Add(PrintDialog1.PrinterSettings.PaperSources.Item(i).SourceName)
ToolStripComboBox2.Items.Add(PrintDialog1.PrinterSettings.PaperSizes.Item(i).PaperName)
If Mid(PrintDialog1.PrinterSettings.PaperSizes.Item(i).PaperName, 1, 2) = "A4" Then k = i
Next
ToolStripComboBox2.SelectedIndex = k
PrintDocument1.PrinterSettings.PrinterName = ToolStripComboBox1.Text
End Sub
Private Sub ToolStripComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripComboBox2.SelectedIndexChanged
PrintDocument1.DefaultPageSettings.PaperSize = PrintDialog1.PrinterSettings.PaperSizes.Item(ToolStripComboBox2.SelectedIndex)
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
Rec_Count = 0
p_no = 0
PrintDocument1.PrinterSettings.Copies = 1
PrintDocument1.DocumentName = "通訊錄"
PrintDocument1.PrinterSettings.PrinterName = ToolStripComboBox1.Text
PrintDocument1.DefaultPageSettings.PaperSource = PrintDialog1.PrinterSettings.PaperSources.Item(ToolStripComboBox2.SelectedIndex)
PrintDocument1.DefaultPageSettings.Landscape = True
PrintDocument1.Print()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim blackPen As New Pen(Color.Black, 0.2)
Dim inSidePen As New Pen(Color.Black, 0.6)
Dim tPos As Single = 40
Dim lPos As Single = 3
Dim BoxH = 25 + (30 * 24)
Dim i As Single, j As Single, x As Single, y As Single, k As Single, s As String, s1 As String
Dim d1 As DateTime = Now.Date
Dim drawFormat As New StringFormat
p_no += 1
d1 = Now.Date
e.Graphics.DrawString("印 表 : " & Format(d1.Year - 1911, "000") & "年" & Format(d1.Month, "00") & "月" & Format(d1.Day, "00") & "日", New System.Drawing.Font("新細明體", 8), Brushes.Black, e.PageBounds.Width - 200, 5, drawFormat)
e.Graphics.DrawString("頁 數 : " & p_no, New System.Drawing.Font("新細明體", 8), Brushes.Black, e.PageBounds.Width - 200, 16, drawFormat)
Dim pf As Font = New System.Drawing.Font("新細明體", 8)
x = lPos
y = tPos
drawFormat.Alignment = StringAlignment.Center
e.Graphics.DrawString("通訊錄", New System.Drawing.Font("標楷體", 15), Brushes.Black, e.PageBounds.Width / 2, 3, drawFormat)
e.Graphics.DrawRectangle(inSidePen, lPos, tPos, e.PageBounds.Width - 45, BoxH + 2)
e.Graphics.DrawRectangle(blackPen, lPos + 1, tPos + 1, e.PageBounds.Width - 47, BoxH)
e.Graphics.DrawLine(blackPen, lPos + 1, y + 23, lPos + e.PageBounds.Width - 46, y + 22)
e.Graphics.DrawString("NO.", pf, Brushes.Black, x + 10, y + 8, drawFormat)
e.Graphics.DrawLine(blackPen, x + 20, y + 1, x + 20, y + 1 + BoxH)
e.Graphics.DrawString("姓名", pf, Brushes.Black, x + 47, y + 8, drawFormat)
e.Graphics.DrawLine(blackPen, x + 75, y + 1, x + 75, y + 1 + BoxH)
e.Graphics.DrawString("電話號碼", pf, Brushes.Black, x + 111, y + 8, drawFormat)
e.Graphics.DrawLine(blackPen, x + 147, y + 1, x + 147, y + 1 + BoxH)
e.Graphics.DrawString("住 址", pf, Brushes.Black, x + 170, y + 8, drawFormat)
tPos = tPos + 25
For k = 0 To 29
y = tPos + k * 24
e.Graphics.DrawString(k + (p_no - 1) * 30 + 1, pf, Brushes.Black, x + 10, y + 6, drawFormat)
e.Graphics.DrawLine(blackPen, lPos + 1, y, lPos + e.PageBounds.Width - 46, y)
Next
drawFormat.Alignment = StringAlignment.Near
For i = 0 To 29
drawFormat.Alignment = StringAlignment.Far
y = tPos + i * 24
e.Graphics.DrawString(DataGridView1.Rows(Rec_Count).Cells(0).Value, pf, Brushes.Black, x + 23, y + 6, New StringFormat)
e.Graphics.DrawString(DataGridView1.Rows(Rec_Count).Cells(1).Value, pf, Brushes.Black, x + 78, y + 6, New StringFormat)
e.Graphics.DrawString(DataGridView1.Rows(Rec_Count).Cells(2).Value, pf, Brushes.Black, x + 150, y + 6, New StringFormat)
If Rec_Count < DataGridView1.Rows.Count - 1 Then
e.HasMorePages = True
Else
e.HasMorePages = False
Exit For
End If
Rec_Count += 1
Next
End Sub
End Class
|