不知道是系统原因还是代码原因,求指教
procedure GetRandomCheckCode(var img: Timage; var codeStr: string);
const
constStr = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
var
I, J, K: Integer;
vPoint: TPoint;
vLeft: Integer;
X, Y: Integer;
vChaos: Integer;
begin
codeStr := '';
for J := 1 to 4 do
begin
Randomize;
K := StrToInt(Format('%0.1d', [Random(Length(constStr))]));
codeStr := codeStr + Trim(constStr[K]);
end;
if (Length(codeStr) = 3) then
begin
Randomize;
K := StrToInt(Format('%0.1d', [Random(Length(constStr))]));
codeStr := codeStr + Trim(constStr[K]);
end;
vLeft := 5;
img.picture := nil;
{ 设置背景颜色 }
img.Canvas.Brush.Color := $FEEEE2; // clAqua;
img.Canvas.FillRect(Rect(0, 0, img.Width, img.Height));
for I := 1 to Length(codeStr) do
begin
with img do
begin
vChaos := 100;
for J := 0 to vChaos do
begin
X := Random(img.Width);
Y := Random(img.Height);
img.Canvas.Pen.Color := clOlive;
img.Canvas.Pen.Style := psDot;
img.Canvas.Pixels[X, Y] := RGB($C0, $C0, $C0); { 噪点颜色 }
end;
Canvas.Font.Size := Random(10) + 11;
Canvas.Font.Color := RGB(Random(256) and $C0, Random(256) and $C0,
Random(256) and $C0);
Canvas.Font.Style := [fsBold];
Canvas.Font.Name := Screen.Fonts[11];
vPoint.X := Random(4) + vLeft;
vPoint.Y := Random(5);
Canvas.TextOut(vPoint.X, vPoint.Y, codeStr[I]);
vLeft := vPoint.X + Canvas.TextWidth(codeStr[I]);
end;
end;
end;
2 个解决方案
#1
K := StrToInt(Format('%0.1d', [Random(Length(constStr))]));
这个有点问题!就是可能返回0值,那么 constStr[K]; 就会出现问题了
这个有点问题!就是可能返回0值,那么 constStr[K]; 就会出现问题了
#2
CNPACK中有这个可参考一二
#1
K := StrToInt(Format('%0.1d', [Random(Length(constStr))]));
这个有点问题!就是可能返回0值,那么 constStr[K]; 就会出现问题了
这个有点问题!就是可能返回0值,那么 constStr[K]; 就会出现问题了
#2
CNPACK中有这个可参考一二