随机生成验证码的一个演示
上次讨论了验证码的去噪(https://bbs.bccn.net/thread-510181-3-1.html),有人提出怎么生成验证码,我就做试着做了个验证码生成的演示程序,供大家参考。下面是代码:
程序代码:
PUBLIC oform1 oform1=NEWOBJECT("form1") oform1.Show RETURN ************************************************** *-- Form: form1 (d:\documents\visual foxpro 项目\logondemo.scx) *-- ParentClass: form *-- BaseClass: form *-- Time Stamp: 10/16/22 07:49:07 PM * DEFINE CLASS form1 AS form Top = 0 Left = 0 DoCreate = .T. Caption = "Form1" Name = "Form1" ADD OBJECT label1 AS label WITH ; Caption = "用户名", ; Height = 16, ; Left = 110, ; Top = 30, ; Width = 38, ; Name = "Label1" ADD OBJECT text1 AS textbox WITH ; Height = 20, ; Left = 160, ; Top = 26, ; Width = 100, ; Name = "Text1" ADD OBJECT label2 AS label WITH ; Caption = "密 码", ; Height = 16, ; Left = 110, ; Top = 60, ; Width = 38, ; Name = "Label2" ADD OBJECT text2 AS textbox WITH ; Height = 20, ; Left = 160, ; Top = 56, ; Width = 100, ; PasswordChar = "*", ; Name = "Text2" ADD OBJECT container1 AS container WITH ; Top = 148, ; Left = 100, ; Width = 180, ; Height = 80, ; BackStyle = 1, ; SpecialEffect = 2, ; BackColor = RGB(240,240,240), ; Name = "Container1" ADD OBJECT label3 AS label WITH ; Caption = "验证码", ; Height = 16, ; Left = 110, ; Top = 90, ; Width = 38, ; Name = "Label3" ADD OBJECT text3 AS textbox WITH ; Height = 20, ; Left = 160, ; Top = 85, ; Width = 100, ; Name = "Text3" ADD OBJECT label4 AS label WITH ; AutoSize = .T., ; Caption = "点击显示验证码", ; Height = 16, ; Left = 100, ; Top = 128, ; Width = 86, ; Name = "Label4" PROCEDURE container1.Click *hwnd = GetActiveWindow() thisform.Cls() hwnd = thisform.HWnd noldfontsize=thisform.fontsize noldforecolor=thisform.foreColor thisform.FontSize=32 hDC = GetDC (hwnd) RAND(-1) *****************************生成随机干扰码************************************* SET CLASSLIB TO HOME()+"FFC\_gdiplus.vcx" FOR i=1 TO 500 lncolor=INT(RAND()*1000000) str=BINTOC(lncolor,"brs") locolor=CREATEOBJECT("gpcolor",ASC(SUBSTR(str,1,2)),INT(ASC(SUBSTR(str,3,2))/255),INT(ASC(SUBSTR(str,5,2))/255),255) lopen=CREATEOBJECT("gppen",locolor,1) lobrush=CREATEOBJECT("gpsolidbrush",locolor) lographics=CREATEOBJECT("gpgraphics") lographics.createfromhwnd(thisform.HWnd) DO WHILE .t. nleft=INT(RAND()*200)+this.left ntop=INT(RAND()*100)+this.Top IF nleft<this.Width+this.left-5 AND ntop<this.Height+this.top-5 EXIT ELSE loop ENDIF ENDDO lographics.fillrectangle(lobrush,nleft,ntop,5,5) ENDFOR *****************************生成随机干扰码************************************* *****************************生成随机验证码************************************* lpString = randomstr(4) FOR i=1 TO 4 thisform.forecolor=INT(RAND()*1000000) TextOut(hDC, this.left+20+40*(i-1),this.top+20, SUBSTR(lpString,i,1),1) && ENDFOR *****************************生成随机验证码************************************* ReleaseDC (hwnd, hDC) thisform.FontSize=noldfontsize thisform.foreColor=noldforecolor ENDPROC PROCEDURE container1.Init DECLARE INTEGER GetDC IN user32 INTEGER hwnd DECLARE INTEGER ReleaseDC IN user32; INTEGER hwnd, INTEGER hdc DECLARE INTEGER GetActiveWindow IN user32 DECLARE INTEGER TextOut IN gdi32; INTEGER hdc,; INTEGER x,; INTEGER y,; STRING lpString,; INTEGER nCount ENDPROC ENDDEFINE * *-- EndDefine: form1 **************************************************
randomstr.prg
程序代码:
*CreateString(tnLength) PARAMETERS tnlength LOCAL lcText, lcChar, x, n lcText = "" FOR n = 1 TO tnLength x = INT(RAND() * 36) lcChar = IIF(x < 10, TRANSFORM(x), CHR(x + 55)) lcText = lcText + lcChar ENDFOR RETURN lcText
由于是直接在form上画,程序运行有点慢,如果在内存上画,速度会快很多!有兴趣的兄弟可以自己改一下。