下载地址 http://http://www.dotnetcoding.net/Resource/BarCode.Zip

Public Class BarCodeClass BarCode
Inherits System.Windows.Forms.Control



Windows Form Designer generated code#Region " Windows Form Designer generated code "


Public Sub New()Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Private Resources As System.Resources.ResourceManager


<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
components = New System.ComponentModel.Container
Me.Text = ""
End Sub

#End Region


'此软件由北京湛蓝科技有限公司提供, 欢迎访问湛蓝科技主网站(www.dotnetcoding.net)




Protected Overrides Sub OnPaint()Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)


Dim x1 As Integer = 0
Dim y1 As Integer = 0


Dim pen As New System.Drawing.Pen(Me.ForeColor)
pen.Width = 1


For index As Integer = 0 To Me.Text.Length - 1
x1 = index * Me.LetterWidth
Me.DrawLetter(e.Graphics, pen, Text.Chars(index), x1, y1, Me.StripHeight)
If Me.PrintText AndAlso index <> 0 AndAlso index <> Me.Text.Length - 1 Then

e.Graphics.DrawString(Me.Text.Substring(index, 1), Me.Font, New SolidBrush(Me.ForeColor), x1, Me.StripHeight)

End If
Next
End Sub







Public Overrides Property Text()Property Text() As String
Get
Return MyBase.Text
End Get
Set(ByVal Value As String)
Dim isValidLetter As Boolean
For index As Integer = 0 To Value.Length - 1
isValidLetter = False
For Each item As String In BarTableStrip
If Value.Substring(index, 1).ToUpper = item Then
isValidLetter = True
Exit For
End If
Next
If Not isValidLetter Then Throw New Exception("Invalid Letter for Bar Code")
Next
If Value.StartsWith("*") = False Then Value = "*" + Value
If Value.EndsWith("*") = False Then Value += "*"
MyBase.Text = Value.ToUpper
Me.Refresh()
End Set
End Property



Private mPrintText As Boolean

Public Property PrintText()Property PrintText() As Boolean
Get
Return Me.mPrintText
End Get
Set(ByVal Value As Boolean)
Me.mPrintText = Value
Me.Refresh()
End Set
End Property

Private Shared mBarTable As System.Collections.Specialized.NameValueCollection

Public Shared ReadOnly Property BarTableStrip()Property BarTableStrip() As System.Collections.Specialized.NameValueCollection
Get

If mBarTable Is Nothing Then
mBarTable = New System.Collections.Specialized.NameValueCollection

mBarTable.Add("0", "")


mBarTable.Add("0", "001100100") ' 0
mBarTable.Add("1", "100010100") ' 1
mBarTable.Add("2", "010010100") ' 2
mBarTable.Add("3", "110000100") ' 3
mBarTable.Add("4", "001010100") ' 4
mBarTable.Add("5", "101000100") ' 5
mBarTable.Add("6", "011000100") ' 6
mBarTable.Add("7", "000110100") ' 7
mBarTable.Add("8", "100100100") ' 8
mBarTable.Add("9", "010100100") ' 9
mBarTable.Add("A", "100010010") ' A
mBarTable.Add("B", "010010010") ' B
mBarTable.Add("C", "110000010") ' C
mBarTable.Add("D", "001010010") ' D
mBarTable.Add("E", "101000010") ' E
mBarTable.Add("F", "011000010") ' F
mBarTable.Add("G", "000110010") ' G
mBarTable.Add("H", "100100010") ' H
mBarTable.Add("I", "010100010") ' I
mBarTable.Add("J", "001100010") ' J
mBarTable.Add("K", "100010001") ' K
mBarTable.Add("L", "010010001") ' L
mBarTable.Add("M", "110000001") ' M
mBarTable.Add("N", "001010001") ' N
mBarTable.Add("O", "101000001") ' O
mBarTable.Add("P", "011000001") ' P
mBarTable.Add("Q", "000110001") ' Q
mBarTable.Add("R", "100100001") ' R
mBarTable.Add("S", "010100001") ' S
mBarTable.Add("T", "001100001") ' T
mBarTable.Add("U", "100011000") ' U
mBarTable.Add("V", "010011000") ' V
mBarTable.Add("W", "110001000") ' W
mBarTable.Add("X", "001011000") ' X
mBarTable.Add("Y", "101001000") ' Y
mBarTable.Add("Z", "011001000") ' Z
mBarTable.Add("-", "000111000") ' -
mBarTable.Add("%", "100101000") ' %
mBarTable.Add("$", "010101000") ' $
mBarTable.Add("*", "001101000") ' *
End If
Return mBarTable
End Get

End Property




Protected Overridable Sub DrawLetter()Sub DrawLetter(ByVal g As System.Drawing.Graphics, ByVal pen As System.Drawing.Pen, ByVal letter As Char, ByVal left As Integer, ByVal top As Integer, ByVal height As Integer)
Dim barCodeString As String = BarTableStrip.Item(letter)

Dim x1 As Integer = left
Dim y1 As Integer = top
Dim x2 As Integer
Dim y2 As Integer
For J As Integer = 1 To 5
If barCodeString.Substring(J - 1, 1) = "0" Then
' 画细线
x2 = x1 + Me.StripeScale
y2 = y1 + Me.StripHeight

Dim rec As New Rectangle(x1, y1, x2 - x1, Me.StripHeight)
Dim brush As New SolidBrush(Me.ForeColor)

g.FillRectangle(brush, rec)

x1 += Me.StripeScale
y1 = y1

Else
' 画宽线
x2 = x1 + Me.BoldLineWidth
y2 = y1 + Me.StripHeight

Dim rec As New Rectangle(x1, y1, x2 - x1, Me.StripHeight)
Dim brush As New SolidBrush(Me.ForeColor)
g.FillRectangle(brush, rec)

x1 += Me.BoldLineWidth
y1 = y1

End If
' 每个字符条码之间为窄间隙
If J = 5 Then

'X = X + intWidthXI * 3
x1 += Me.StripeScale * 3
Exit For
End If
If barCodeString.Substring(J + 4, 1) = "0" Then
'窄间隙
x1 = x1 + Me.StripeScale * 1
Else
'宽间隙
x1 = x1 + Me.StripeScale * 3
End If


Next J

End Sub




Protected ReadOnly Property SmallLineWidth()Property SmallLineWidth() As Integer
Get
Return 1 * Me.StripeScale
End Get
End Property



Protected ReadOnly Property BoldLineWidth()Property BoldLineWidth() As Integer
Get
Return 3 * Me.StripeScale
End Get
End Property

Dim mStripeScale As Integer = 2

Public Property StripeScale()Property StripeScale() As Integer
Get
Return Me.mStripeScale
End Get
Set(ByVal Value As Integer)
Me.mStripeScale = Value
Me.Refresh()
End Set
End Property



Public Shadows Property Width()Property Width() As Integer
Get

If MyBase.Width < Me.Text.Length * Me.LetterWidth Then
MyBase.Width = Me.Text.Length * Me.LetterWidth
End If

Return MyBase.Width
End Get
Set(ByVal Value As Integer)
MyBase.Width = Value
Me.Refresh()
End Set
End Property

Private mTextHeight As Integer = 0

Public Property TextHeight()Property TextHeight() As Integer
Get
If Me.PrintText AndAlso Me.mTextHeight = 0 Then
Me.mTextHeight = CInt(Me.Height / 4)
ElseIf Me.PrintText = False Then
Me.mTextHeight = 0
End If
Return Me.mTextHeight
End Get
Set(ByVal Value As Integer)
If Value > Me.Height Then
Me.mTextHeight = Me.Height
Else
Me.mTextHeight = Value
End If
Me.Refresh()
End Set
End Property


Public ReadOnly Property StripHeight()Property StripHeight() As Integer
Get
Return CInt((Me.Height - Me.TextHeight))
End Get

End Property



Public ReadOnly Property LetterWidth()Property LetterWidth() As Integer
Get
Return 10 * Me.StripeScale + 4 * 2 * Me.StripeScale
End Get
End Property




Public Shadows Property Height()Property Height() As Integer
Get
Return MyBase.Height()
End Get
Set(ByVal Value As Integer)
MyBase.Height = Value

Me.Refresh()
End Set
End Property


End Class

【推荐】100%开源!大型工业跨平台软件C++源码提供,建模,组态!
【推荐】2025 HarmonyOS 鸿蒙创新赛正式启动,百万大奖等你挑战
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步