天天看點

VB,NET 驗證碼控件

驗證碼是用來防止程式反複登陸系統造成的損失的一個實用的小措施,

而采用驗證碼控件能夠很友善的使用驗證碼

下面就簡單講述一下一個簡單的驗證碼控件的開發過程。

  一個驗證碼控件的組成部分如下: 1.輸入框,用于輸入驗證碼  在這裡使用textbox 2.圖檔框,用于顯示驗證碼  在這裡使用panel       3.标志框,用于顯示驗證碼是否正确,在這裡用label顯示 以上是控件部分,簡便起見,控件名全部采用預設值,實際應用中請自行修改;下面說一下代碼中的部分,

首先是私有字段 包括: 1._Ispass as boolean                                                         邏輯型變量,訓示驗證是否通過。 2._Key as string                                                                 字元串型變量,存儲驗證碼, 3.KeyString as string()                                                       字元串數組,存儲0-9,A-Z。a-z,共62個字元。     然後是屬性, 這裡隻有一個隻讀的boolean型,IsPass                           訓示是否通過驗證。     一個事件 KeyVerification                                               通過驗證時觸發     兩個方法: 1. Public Sub Refresh                                                       重新整理驗證碼 2.Private Sub Textbox1_changed                                    textbox内容變更時觸發,判斷驗證碼是否一緻

代碼如下 : Public Class Verification

    Private _IsPass As Boolean

    Private _Key As String = ""

    Public Event PassVerification(sender As Object, e As EventArgs)

    Dim KeyString As String() = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",

        "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",

        "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" _

       , "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "n", "m", "o",

       "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}

    Public ReadOnly Property IsPass As Boolean

        Get

            Return _IsPass

        End Get

    End Property

    Public Sub Refersh(sender As Object, e As EventArgs) Handles TextBox1.GotFocus, Panel1.Click, Label1.Click

        'Dim Key As String = ""

        _Key = ""

        Randomize()

        For i = 0 To 3

            _Key += KeyString(Int(Rnd() * 52 + 1))

        Next

        Dim g As Graphics = Me.Panel1.CreateGraphics

        g.Clear(SystemColors.Control)

        g.DrawString(Mid(_Key, 1, 1), New Font("宋體", 20, FontStyle.Bold), Brushes.Red, New Point(2, 2))

        g.DrawString(Mid(_Key, 2, 1), New Font("宋體", 20, FontStyle.Bold), Brushes.Red, New Point(22, 2))

        g.DrawString(Mid(_Key, 3, 1), New Font("宋體", 20, FontStyle.Bold), Brushes.Red, New Point(42, 2))

        g.DrawString(Mid(_Key, 4, 1), New Font("宋體", 20, FontStyle.Bold), Brushes.Red, New Point(62, 2))

        '噪聲線

        g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))

        g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))

        g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))

        g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))

        g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))

        g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))

        g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))

        g.DrawLine(New Pen(Brushes.DarkOrchid), New Point(Int(Rnd() * 100), Int(Rnd() * 40)), New Point(Int(Rnd() * 100), Int(Rnd() * 40)))

        g.Dispose()

    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

        If TextBox1.Text.ToLower = _Key.ToLower Then

            Label1.Text = "√"

            Label1.ForeColor = Color.Green

            RaiseEvent PassVerification(Me, New EventArgs)

        Else

            Label1.Text = "×"

            Label1.ForeColor = Color.Red

        End If

    End Sub

End Class

這段代碼十分簡陋,

在實際應用中,各位可以采用一些手段來提高這個控件的防識别功能,并提高性能, 比如說在繪制驗證碼的可以旋轉,扭曲,變換字型粗細,顔色,變換噪聲線顔色,粗細,添加噪聲點,等等方法

在驗證中也可以通過下列代碼來提高一點點性能     Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

    if Textbox1.text.length <> _key.length then return-----------------------------------------------------------------------------添加這一句來使長度不等于驗證碼的跳過判斷相等            If TextBox1.Text.ToLower = _Key.ToLower Then

            Label1.Text = "√"

            Label1.ForeColor = Color.Green

            RaiseEvent PassVerification(Me, New EventArgs)

        Else

            Label1.Text = "×"

            Label1.ForeColor = Color.Red

        End If

    End Sub                                                  ------------------------------------------------------------------------------------By Lzx  AT 16-1-16