做一程式,登陸界面用image控制元件顯示驗證碼,驗證碼由下面的函式生成并繪制,在有的系統下顯示成亂碼

不知道是系統原因還是代碼原因,求指教
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;
uj5u.com熱心網友回復:
K := StrToInt(Format('%0.1d', [Random(Length(constStr))]));這個有點問題!就是可能回傳0值,那么 constStr[K]; 就會出現問題了
uj5u.com熱心網友回復:
CNPACK中有這個可參考一二轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/131902.html
