Die Community zu .NET und Classic VB.
Menü

Tipp-Upload: VB.NET 0131: Barcode 39 horizontal + vertikal VB2005

 von 

Über den Tipp  

Dieser Tippvorschlag ist noch unbewertet.

Der Vorschlag ist in den folgenden Kategorien zu finden:

  • Drucker

Dem Tippvorschlag wurden folgende Schlüsselwörter zugeordnet:
Barcode 39, Drucker, VB2005

Damit er übernommen werden kann, müssen noch Änderungen daran vorgenommen werden. Sofern Sie der Autor sind, können Sie sich anmelden, um die Liste einzusehen.

Der Vorschlag wurde erstellt am: 12.10.2007 15:19.
Die letzte Aktualisierung erfolgte am 12.10.2007 15:19.

Zurück zur Übersicht

Beschreibung  

Barcode 39 für VB2005, horizontal und vertikal, Textierung 0, 90, 180 und 270 Grad

Schwierigkeitsgrad

Schwierigkeitsgrad 2

Verwendete API-Aufrufe:

Download:

Download des Beispielprojektes [35,63 KB]

' Dieser Source stammt von http://www.activevb.de
' und kann frei verwendet werden. Für eventuelle Schäden
' wird nicht gehaftet.

' Um Fehler oder Fragen zu klären, nutzen Sie bitte unser Forum.
' Ansonsten viel Spaß und Erfolg mit diesem Source!
'
' Beachten Sie, das vom Designer generierter Code hier ausgeblendet wird.
' In den Zip-Dateien ist er jedoch zu finden.

' ------- Anfang Projektdatei Barcode39_VB2005.vbproj  -------
' ---------------- Anfang Datei clsBarcode.vb ----------------
' --------------------------------------------------------
'  Barcode erstellen horizontal und vertikal unter VB2005
'  basierend auf Tip 040 in ActiveVB für .NET
'
'  Autor       peter.k.sauer@web.de
'  created     16.09.2007
'  update      12.10.2007
' --------------------------------------------------------

Public Class clsBarcode

    ''' <summary>
    ''' Barcode horizontal ausgeben
    ''' </summary>
    ''' <param name="Barcode">Barcodeinhalt</param>
    ''' <param name="Gr">Graphics (Printer)</param>
    ''' <param name="mLeft">Abstand links</param>
    ''' <param name="mTop">Abstand oben</param>
    ''' <param name="mWidth">Breite Barcode</param>
    ''' <param name="mHeight">Höhe Barcode</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function MD_Barcode39H(ByVal Barcode As String, _
                        ByVal Gr As System.Drawing.Graphics, _
                        ByVal mLeft As Single, ByVal mTop As Single, _
                        ByVal mWidth As Single, _
                        ByVal mHeight As Single) As Boolean

        Dim Nbar As Single, Wbar As Single
        Dim Qbar As Single, NextBar As Single
        Dim CountX As Single, CountY As Single
        Dim Parts As Single, Pix As Single, BarCodePlus As String
        Dim Stripes As String, BarType As String
        Dim Mx As Single, My1 As Single, Sx As Single, Sy As Single

        Const Nratio As Integer = 20
        Const Wratio As Integer = 55
        Const Qratio As Integer = 35

        ' Dim g As System.Drawing.Graphics
        Dim pB As New System.Drawing.SolidBrush (System.Drawing.Color.Black)
        Dim pW As New System.Drawing.SolidBrush (System.Drawing.Color.White)
        Dim color As System.Drawing.SolidBrush

        Try

            ' Get control size and location properties.
            Sx = mLeft
            Sy = mTop
            Mx = mWidth
            My1 = mHeight

            ' g = PaintObj.CreateGraphics()
            Gr.PageUnit = GraphicsUnit.Millimeter

            ' Calculate actual and relative pixels values.
            Parts = (Barcode.Length + 2) * ((6 * Nratio) + (3 * Wratio) + (1 * Qratio))
            Pix = (Mx / Parts)
            Nbar = (20 * Pix)
            Wbar = (55 * Pix)
            Qbar = (35 * Pix)

            ' Initialize bar index and color.
            NextBar = Sx
            color = pW

            ' Pad each end of string with start/stop characters
            BarCodePlus = "*" & Barcode.ToUpper & "*"

            ' Walk through each character of the barcode contents
            For CountX = 0 To BarCodePlus.Length - 1

                ' Get Barcode 1/0 string for indexed character.
                Stripes = MD_BC39(BarCodePlus.Substring(CountX, 1))

                For CountY = 0 To 8

                    ' For each 1/0, draw a wide/narrow bar.
                    BarType = Stripes.Substring(CountY, 1)

                    ' Toggle the color (black/white).
                    If color Is pW Then
                        color = pB

                    Else

                        color = pW
                    End If

                    Select Case BarType

                        Case "1"

                            ' Draw a wide bar.
                            Gr.FillRectangle(color, NextBar, Sy, Wbar, My1)
                            NextBar = NextBar + Wbar

                        Case "0"

                            ' Draw a narrow bar.
                            Gr.FillRectangle(color, NextBar, Sy, Nbar, My1)
                            NextBar = NextBar + Nbar

                    End Select

                Next CountY

                ' Toggle the color (black/white).
                If color Is pW Then
                    color = pB

                Else

                    color = pW
                End If

                ' Draw intermediate "quiet" bar.
                Gr.FillRectangle(color, NextBar, Sy, Qbar, My1)
                NextBar = NextBar + Qbar
            Next CountX

        Catch ex As Exception

            MessageBox.Show(ex.StackTrace)

        End Try

    End Function

    ''' <summary>
    ''' Barcode vertikal ausgeben
    ''' </summary>
    ''' <param name="Barcode">Barcodeinhalt</param>
    ''' <param name="Gr">Graphics (Printer)</param>
    ''' <param name="mLeft">Abstand links</param>
    ''' <param name="mTop">Abstand oben</param>
    ''' <param name="mWidth">Breite/Höhe Barcode</param>
    ''' <param name="mHeight">Höhe/Breite Barcode</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function MD_Barcode39V(ByVal Barcode As String, _
                        ByVal Gr As System.Drawing.Graphics, _
                        ByVal mLeft As Single, ByVal mTop As Single, _
                        ByVal mWidth As Single, _
                        ByVal mHeight As Single) As Boolean

        Dim Nbar As Single, Wbar As Single
        Dim Qbar As Single, NextBar As Single
        Dim CountX As Single, CountY As Single
        Dim Parts As Single, Pix As Single, BarCodePlus As String
        Dim Stripes As String, BarType As String
        Dim Mx As Single, My1 As Single, Sx As Single, Sy As Single

        Const Nratio As Integer = 20
        Const Wratio As Integer = 55
        Const Qratio As Integer = 35

        ' Dim g As System.Drawing.Graphics
        Dim pB As New System.Drawing.SolidBrush (System.Drawing.Color.Black)
        Dim pW As New System.Drawing.SolidBrush (System.Drawing.Color.White)
        Dim color As System.Drawing.SolidBrush

        Try

            ' Get control size and location properties.
            Sx = mLeft
            Sy = mTop
            Mx = mWidth
            My1 = mHeight

            ' g = PaintObj.CreateGraphics()
            Gr.PageUnit = GraphicsUnit.Millimeter

            ' Calculate actual and relative pixels values.
            Parts = (Barcode.Length + 2) * ((6 * Nratio) + (3 * Wratio) + (1 * Qratio))
            Pix = (Mx / Parts)
            Nbar = (20 * Pix)
            Wbar = (55 * Pix)
            Qbar = (35 * Pix)

            ' Initialize bar index and color.
            NextBar = Sx
            color = pW

            ' Pad each end of string with start/stop characters
            BarCodePlus = "*" & Barcode.ToUpper & "*"

            ' Walk through each character of the barcode contents
            For CountX = 0 To BarCodePlus.Length - 1

                ' Get Barcode 1/0 string for indexed character.
                Stripes = MD_BC39(BarCodePlus.Substring(CountX, 1))

                For CountY = 0 To 8

                    ' For each 1/0, draw a wide/narrow bar.
                    BarType = Stripes.Substring(CountY, 1)

                    ' Toggle the color (black/white).
                    If color Is pW Then
                        color = pB

                    Else

                        color = pW
                    End If

                    Select Case BarType

                        Case "1"

                            ' Draw a wide bar.
                            Gr.FillRectangle(color, Sy, NextBar, My1, Wbar)
                            NextBar = NextBar + Wbar

                        Case "0"

                            ' Draw a narrow bar.
                            Gr.FillRectangle(color, Sy, NextBar, My1, Nbar)
                            NextBar = NextBar + Nbar

                    End Select

                Next CountY

                ' Toggle the color (black/white).
                If color Is pW Then
                    color = pB

                Else

                    color = pW
                End If

                ' Draw intermediate "quiet" bar.
                Gr.FillRectangle(color, NextBar, Sy, Qbar, My1)
                NextBar = NextBar + Qbar
            Next CountX

        Catch ex As Exception

            MessageBox.Show(ex.StackTrace)

        End Try

    End Function

    ''' <summary>
    ''' liefert die Codierung pro Zeichen
    ''' </summary>
    ''' <param name="CharCode"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function MD_BC39(ByVal CharCode As String) As String

        Try

            Dim BC39(90) As String

            BC39(32) = "011000100" ' space
            BC39(36) = "010101000" ' $
            BC39(37) = "000101010" ' %
            BC39(42) = "010010100" ' * Start/Stop
            BC39(43) = "010001010" ' +
            BC39(45) = "010000101" ' |
            BC39(46) = "110000100" ' .
            BC39(47) = "010100010" ' /
            BC39(48) = "000110100" ' 0
            BC39(49) = "100100001" ' 1
            BC39(50) = "001100001" ' 2
            BC39(51) = "101100000" ' 3
            BC39(52) = "000110001" ' 4
            BC39(53) = "100110000" ' 5
            BC39(54) = "001110000" ' 6
            BC39(55) = "000100101" ' 7
            BC39(56) = "100100100" ' 8
            BC39(57) = "001100100" ' 9
            BC39(65) = "100001001" ' A
            BC39(66) = "001001001" ' B
            BC39(67) = "101001000" ' C
            BC39(68) = "000011001" ' D
            BC39(69) = "100011000" ' E
            BC39(70) = "001011000" ' F
            BC39(71) = "000001101" ' G
            BC39(72) = "100001100" ' H
            BC39(73) = "001001100" ' I
            BC39(74) = "000011100" ' J
            BC39(75) = "100000011" ' K
            BC39(76) = "001000011" ' L
            BC39(77) = "101000010" ' M
            BC39(78) = "000010011" ' N
            BC39(79) = "100010010" ' O
            BC39(80) = "001010010" ' P
            BC39(81) = "000000111" ' Q
            BC39(82) = "100000110" ' R
            BC39(83) = "001000110" ' S
            BC39(84) = "000010110" ' T
            BC39(85) = "110000001" ' U
            BC39(86) = "011000001" ' V
            BC39(87) = "111000000" ' W
            BC39(88) = "010010001" ' X
            BC39(89) = "110010000" ' Y
            BC39(90) = "011010000" ' Z

            Dim c As Char = Convert.ToChar(CharCode)

            Return BC39(Convert.ToInt32(c))

        Catch ex As Exception

            Return ""

        End Try

    End Function

End Class

' ----------------- Ende Datei clsBarcode.vb -----------------
' ------------------ Anfang Datei Form1.vb  ------------------
' ----------------------------------------------
'  Barcode 3/9 horizontal und vertikal ausgeben
'  Textíerung 0, 90, 180 und 270 Grad
' ----------------------------------------------

'  Controls: CheckBox1, Button1

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, _
                    ByVal e As System.EventArgs) Handles MyBase.Load

        CheckBox1.Text = "Preview"
        CheckBox1.Checked = True

        Button1.Text = "Print/Preview"

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, _
                    ByVal e As System.EventArgs) Handles Button1.Click

        Dim PrinterName As String = "HP DeskJet 3600 series"

        ' Drucker auswählen
        Using PD As New PrintDialog
            PD.PrinterSettings.PrinterName = PrinterName

            Dim Result As DialogResult = PD.ShowDialog(Me)

            If Result = Windows.Forms.DialogResult.Cancel Then

                Exit Sub

            End If

            PrinterName = PD.PrinterSettings.PrinterName
        End Using

        ' Barcode ausgeben
        Dim cBC As New prnBarcode
        Dim AsPreview As Boolean = CheckBox1.Checked

        cBC.Print(PrinterName, AsPreview)

    End Sub

End Class

' ------------------- Ende Datei Form1.vb  -------------------
' ---------------- Anfang Datei prnBarcode.vb ----------------
' -------------------------------------------------
'
'  Barcode ausgeben über Printdocument mit Preview
'
' -------------------------------------------------
Imports System.Drawing.Printing

Public Class prnBarcode

    ' Dokumentenobject zum Druck
    Private WithEvents Doc As New PrintDocument

    ' Druckvorschau
    Private WithEvents PrnPreview As New PrintPreviewDialog

    Public Sub New()

    End Sub

    ''' <summary>
    ''' Druckausgabe starten
    ''' </summary>
    ''' <param name="PrinterName">Name des Druckers</param>
    ''' <param name="AsPreview">Ausgabe als Vorschau</param>
    ''' <remarks></remarks>
    Public Sub Print(ByVal PrinterName As String, _
                         Optional ByVal AsPreview As Boolean = False)

        Doc.PrinterSettings.PrinterName = PrinterName
        Doc.PrinterSettings.PrintFileName = "Barcode_Test"

        If AsPreview Then
            PrnPreview.Document = Doc
            PrnPreview.ShowDialog()

        Else

            Doc.Print()
        End If

    End Sub

    ''' <summary>
    ''' Preview Zoom 100% und Maximize
    ''' </summary>
    Private Sub PrnPreview_Load(ByVal sender As Object, _
                ByVal e As System.EventArgs) Handles PrnPreview.Load

        PrnPreview.WindowState = FormWindowState.Maximized
        PrnPreview.PrintPreviewControl.Zoom = 1.0
        PrnPreview.PrintPreviewControl.AutoZoom = False

    End Sub

    ''' <summary>
    ''' Seite ausgeben
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Doc_PrintPage(ByVal sender As Object, _
                ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
                Handles Doc.PrintPage

        Dim Gr As Graphics = e.Graphics

        Gr.PageUnit = GraphicsUnit.Millimeter

        Dim cBC As New clsBarcode

        Dim x As Single = 20
        Dim y As Single = 30
        Dim w As Single = 90
        Dim h As Single = 15
        Dim BC As String = "3SAB0123456789-001"

        ' Barcode und Text horizontal
        cBC.MD_Barcode39H(BC, Gr, x, y, w, h)

        Using Ft As New Font("System", 12, FontStyle.Bold)

            ' Text 000 Grad
            Dim b As Single = (w - Gr.MeasureString(BC, Ft).Width) / 2

            Gr.DrawString(BC, Ft, Brushes.Black, x + b, y + h + 2)

            ' Text 180 Grad
            Dim x1 As Single = x + b + Gr.MeasureString(BC, Ft).Width
            Dim y1 As Single = y - 2

            Gr.TranslateTransform(x1, y1)
            Gr.RotateTransform(180)
            Gr.DrawString(BC, Ft, Brushes.Black, 0, 0)
            Gr.RotateTransform(180)
            Gr.TranslateTransform(x1 * -1, y1 * -1)
        End Using

        ' Barcode und Text vertikal
        x = 80
        y = 60
        cBC.MD_Barcode39V(BC, Gr, x, y, w, h)

        Using Ft As New Font("System", 12, FontStyle.Bold)

            ' Text 090 Grad
            Dim b As Single = (w - Gr.MeasureString(BC, Ft).Width) / 2
            Dim x1 As Single = y - 2
            Dim y1 As Single = x + b

            Gr.TranslateTransform(x1, y1)
            Gr.RotateTransform(90)
            Gr.DrawString(BC, Ft, Brushes.Black, 0, 0)
            Gr.RotateTransform(270)
            Gr.TranslateTransform(x1 * -1, y1 * -1)

            ' Text 270 Grad
            x1 = y + h + 2
            y1 = x + b + Gr.MeasureString(BC, Ft).Width
            Gr.TranslateTransform(x1, y1)
            Gr.RotateTransform(270)
            Gr.DrawString(BC, Ft, Brushes.Black, 0, 0)
            Gr.RotateTransform(90)
            Gr.TranslateTransform(x1 * -1, y1 * -1)
        End Using

    End Sub

End Class

' ----------------- Ende Datei prnBarcode.vb -----------------
' -------- Ende Projektdatei Barcode39_VB2005.vbproj  --------

	

Diskussion  

Diese Funktion ermöglicht es, Fragen, die die Veröffentlichung des Tipps betreffen, zu klären, oder Anregungen und Verbesserungsvorschläge einzubringen. Nach der Veröffentlichung des Tipps werden diese Beiträge nicht weiter verlinkt. Allgemeine Fragen zum Inhalt sollten daher hier nicht geklärt werden.
Folgende Diskussionen existieren bereits

Barcode 39 - Spatzenkanonier 08.03.2008 20:06

Um eine Diskussion eröffnen zu können, müssen sie angemeldet sein.