2012年5月25日 星期五

vbnet 製作QRcode報表

引用來源
--
  1. Imports System.Data.OleDb
  2. Imports System.Drawing.Printing
  3. Public Class Form1
  4. Inherits System.Windows.Forms.Form
  5. #Region " Windows Form Designer generated code "
  6. Public Sub New()
  7. MyBase.New()
  8. 'This call is required by the Windows Form Designer.
  9. InitializeComponent()
  10. 'Add any initialization after the InitializeComponent() call
  11. End Sub
  12. 'Form overrides dispose to clean up the component list.
  13. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  14. If disposing Then
  15. If Not (components Is Nothing) Then
  16. components.Dispose()
  17. End If
  18. End If
  19. MyBase.Dispose(disposing)
  20. End Sub
  21. 'Required by the Windows Form Designer
  22. Private components As System.ComponentModel.IContainer
  23. 'NOTE: The following procedure is required by the Windows Form Designer
  24. 'It can be modified using the Windows Form Designer.
  25. 'Do not modify it using the code editor.
  26. Friend WithEvents button1 As System.Windows.Forms.Button
  27. Friend WithEvents _c1BarCode As C1.Win.C1BarCode.C1QRCode
  28. Private Sub InitializeComponent()
  29. Me.button1 = New System.Windows.Forms.Button()
  30. Me._c1BarCode = New C1.Win.C1BarCode.C1QRCode()
  31. Me.SuspendLayout()
  32. '
  33. 'button1
  34. '
  35. Me.button1.Location = New System.Drawing.Point(8, 9)
  36. Me.button1.Name = "button1"
  37. Me.button1.Size = New System.Drawing.Size(112, 37)
  38. Me.button1.TabIndex = 3
  39. Me.button1.Text = "Show Document"
  40. '
  41. '_c1BarCode
  42. '
  43. Me._c1BarCode.Location = New System.Drawing.Point(128, 9)
  44. Me._c1BarCode.Name = "_c1BarCode"
  45. Me._c1BarCode.Size = New System.Drawing.Size(75, 27)
  46. Me._c1BarCode.TabIndex = 2
  47. Me._c1BarCode.Text = "c1BarCode1"
  48. Me._c1BarCode.Visible = False
  49. '
  50. 'Form1
  51. '
  52. Me.AutoScaleBaseSize = New System.Drawing.Size(5, 15)
  53. Me.ClientSize = New System.Drawing.Size(224, 45)
  54. Me.Controls.Add(Me.button1)
  55. Me.Controls.Add(Me._c1BarCode)
  56. Me.Name = "Form1"
  57. Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
  58. Me.Text = "C1QrCode"
  59. Me.ResumeLayout(False)
  60. End Sub
  61. #End Region
  62. Dim _dt As New DataTable()
  63. Dim _item As Integer
  64. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  65. ' get some data for the report
  66. Dim sql As String = "select ProductID,ProductName from Products order by productid asc"
  67. Dim conn As String = GetConnectionString()
  68. Dim da As New OleDbDataAdapter(sql, conn)
  69. da.Fill(_dt)
  70. End Sub
  71. Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
  72. ' create PrintDocument
  73. Dim printDoc As New PrintDocument()
  74. AddHandler printDoc.BeginPrint, New PrintEventHandler(AddressOf Me._beginPrint)
  75. AddHandler printDoc.PrintPage, New PrintPageEventHandler(AddressOf Me._printPage)
  76. ' show preview
  77. Dim dlg As New PrintPreviewDialog()
  78. dlg.Document = printDoc
  79. dlg.ShowDialog()
  80. End Sub
  81. Private Sub _beginPrint(ByVal sender As Object, ByVal e As PrintEventArgs)
  82. _item = 0
  83. End Sub
  84. Private Sub _printPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
  85. Dim g As Graphics = e.Graphics
  86. Dim f As New Font("Tahoma", 12.0!)
  87. Dim rcPage As RectangleF = New RectangleF(e.MarginBounds.X, e.MarginBounds.Y, e.MarginBounds.Width, e.MarginBounds.Height)
  88. Dim rc1 As RectangleF = rcPage
  89. ' rc1.Height = 30
  90. rc1.Width = 100
  91. rc1.Height = 80
  92. Dim rc2 As RectangleF = rc1
  93. rc2.Offset(rc1.Width, 0)
  94. rc2.Width = 300
  95. rc2.Height = 80
  96. Dim rc3 As RectangleF = rc2
  97. rc3.Offset(rc2.Width, 0)
  98. rc3.Width = 80
  99. rc3.Height = 80
  100. ' show header at the top of the page
  101. g.DrawString("Product ID", f, Brushes.Black, CType(rc1, RectangleF))
  102. g.DrawString("Name", f, Brushes.Black, CType(rc2, RectangleF))
  103. g.DrawString("Code", f, Brushes.Black, CType(rc3, RectangleF))
  104. rc1.Y = (rc1.Y + 60)
  105. rc2.Y = (rc2.Y + 60)
  106. rc3.Y = (rc3.Y + 60)
  107. ' loop through rows until done (or until out of room)
  108. Do While ((rc1.Bottom <= rcPage.Bottom) AndAlso (Me._item < Me._dt.Rows.Count))
  109. Dim row1 As DataRow = Me._dt.Rows.Item(_item)
  110. 'Dim text1 As String = String.Format("{0:0000}", row1.Item("ProductID"))
  111. Dim text1 As String = String.Format("{0:00000}", row1.Item(0))
  112. ' Dim text2 As String = CType(row1.Item("ProductName"), String)
  113. Dim text2 As String = CType(row1.Item(1), String)
  114. g.DrawString(text1, f, Brushes.Black, CType(rc1, RectangleF))
  115. g.DrawString(text2, f, Brushes.Black, CType(rc2, RectangleF))
  116. Me._c1BarCode.Text = text1
  117. g.DrawImage(Me._c1BarCode.Image, rc3)
  118. rc1.Y = (rc1.Y + (rc1.Height + 60))
  119. rc2.Y = (rc2.Y + (rc2.Height + 60))
  120. rc3.Y = (rc3.Y + (rc3.Height + 60))
  121. Me._item += 1
  122. Loop
  123. ' continue if necessary
  124. e.HasMorePages = (Me._item < (Me._dt.Rows.Count - 1))
  125. End Sub
  126. Private Function GetConnectionString() As String
  127. Dim conn As String = "provider=microsoft.jet.oledb.4.0;data source=" & Application.StartupPath & "\Northwind.mdb" & ""
  128. Return conn
  129. End Function 
End Class

沒有留言:

張貼留言