想用delphi做一个抽奖程序,功能是六位数随机滚动,按下某个键后,停留一组数字。没什么delphi基础,请高人们给指点一下应怎么做。
解决方案 »
- 难道发现Delphi bug了?关于THashedStringlist的问题
- 如何让程序 安全删除 指定u盘 ??? 急!
- 主表中输入不了数据?
- 如何判断当前系统的鼠标设置是正常还是左手习惯?(左右键与正常相反)
- 用过lame_enc.dll的请指教
- delphi做的窗体DLL,vc++调用,我的接口函数用什么数据类型好??
- delphi7下如何做一个ftp程序(含客户端与服务器端)
- 显示器上一毫米的长度转化为像素点数
- Format string 用法求解
- 动态增减数据显示字段???
- 用BDE打开Access数据库需要密码,Database login 是什么?
- 请问如何在delphi中实现dos重定向
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Timer1Timer(Sender: TObject);
var
i:Integer;
begin
Randomize;
i := Random(999999);
Label1.Caption := Format('%.6d',[i]);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
Timer1.Enabled := False;
end;end.
1.六位随机数可以重复,且不是事先输入备选,而是纯系统随机产生。(也想了解一下要是事先有备选随机数,且已经抽出过的中奖号要排除应怎么做)
2.系统还要实现自动保存抽出过的所有号,想用sql做表保存。
代码如下:
unit1的code,窗体上有六个panel来显示抽出来的随机数,一个popmenu用来初始化抽奖数unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Menus;type
TForm1 = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
Panel4: TPanel;
Panel5: TPanel;
Panel6: TPanel;
PopupMenu1: TPopupMenu;
N1: TMenuItem;
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure N1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
bStop: boolean;
procedure GetRandomNumber;
function IfCanGetNumber: Boolean;
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses Unit2;{$R *.dfm}procedure TForm1.GetRandomNumber;
function getRandomStr(istr: integer): Integer;
begin
Randomize;
Result := Random(istr);
end; function ifValid(str: String;strList_NO,strList_User: TstringList): Boolean;
var
i: integer;
begin
Result := true;
if (strList_NO.IndexOf(str)<>-1) or (strList_User.IndexOf(str)=-1) then
begin
Result := false;
Exit;
end;
end;
var
i,iRandom: integer;
iStr: string;
strList_all,strList_exists: TStringList;
begin
strList_all := TstringList.Create;
strList_exists := TstringList.Create;
try
//加载全部人员
strList_all.LoadFromFile(ExtractFilePath(application.ExeName)+'all_user');
//加载已经抽过的
if fileexists(ExtractFilePath(application.ExeName)+'exists_user') then
strList_exists.LoadFromFile(ExtractFilePath(application.ExeName)+'exists_user'); bStop := false;
while true do
begin
iRandom := getRandomStr(strList_all.Count);
if iRandom = strList_all.count then iRandom := strList_all.count-1;
iStr := strList_all[iRandom];
Panel1.caption := copy(iStr,1,1);
Panel2.caption := copy(iStr,2,1);
Panel3.caption := copy(iStr,3,1);
Panel4.caption := copy(iStr,4,1);
Panel5.caption := copy(iStr,5,1);
Panel6.caption := copy(iStr,6,1);
sleep(100);
Application.ProcessMessages;
while not ifValid(iStr,strList_exists,strList_all) do
begin
iRandom := getRandomStr(strList_all.Count);
if iRandom = strList_all.count then iRandom := strList_all.count-1;
iStr := strList_all[iRandom];
Panel1.caption := copy(iStr,1,1);
Panel2.caption := copy(iStr,2,1);
Panel3.caption := copy(iStr,3,1);
Panel4.caption := copy(iStr,4,1);
Panel5.caption := copy(iStr,5,1);
Panel6.caption := copy(iStr,6,1);
sleep(100);
Application.ProcessMessages;
end;
if bStop then
begin
//再次查看是否有重复的
while not ifValid(iStr,strList_exists,strList_all) do
begin
iRandom := getRandomStr(strList_all.Count);
if iRandom = strList_all.count then iRandom := strList_all.count-1;
iStr := strList_all[iRandom];
Panel1.caption := copy(iStr,1,1);
Panel2.caption := copy(iStr,2,1);
Panel3.caption := copy(iStr,3,1);
Panel4.caption := copy(iStr,4,1);
Panel5.caption := copy(iStr,5,1);
Panel6.caption := copy(iStr,6,1);
sleep(100);
Application.ProcessMessages;
end;
strList_exists.Add(istr);
strList_exists.SaveToFile(ExtractFilePath(application.ExeName)+'exists_user');
break;
end;
end;
finally
strList_exists.Free;
strList_all.Free;
end;
end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key=32 then
begin
if not IfCanGetNumber then Exit;
if bstop then
begin
bStop := false;
GetRandomNumber;
end else
begin
bstop := true;
end;
end;
end;function TForm1.IfCanGetNumber: Boolean;
begin
Result := true;
if not FileExists(ExtractFilePath(application.ExeName)+'all_user') then
begin
Result := false;
MessageBox(handle,'请先初始化编号','提示',MB_ICONERROR);
Exit;
end;
end;procedure TForm1.N1Click(Sender: TObject);
begin
if bstop then
begin
if not assigned(Form2) then
Form2 := TForm2.Create(Application);
Form2.ShowModal;
Form2.Free;
Form2 := Nil;
end else
begin
Messagebox(handle,'停止其它操作后再初始化','提示',MB_ICONERROR);
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
bStop := true;
end;end.unit2的 code:unit Unit2;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm2 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form2: TForm2;implementation{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
function InsertStrNo(sNO: String): String;
var
i: integer;
begin
for i:= 1 to 6-Length(sNO) do
begin
insert('0',sNO,i);
end;
result := sNO;
end;
var
i: Integer;
str_b,str_e: String;
strList: TStringList;
begin
if Trim(Edit1.Text)='' then
begin
MessageBox(handle,'请输入起始号码','系统提示',MB_ICONINFORMATION);
Edit1.SetFocus;
Exit;
end;
if Trim(Edit2.Text)='' then
begin
MessageBox(handle,'请输入终止号码','系统提示',MB_ICONINFORMATION);
Edit2.SetFocus;
Exit;
end;
str_b := trim(Edit1.Text);
str_e := trim(Edit2.Text);
strList := TstringList.Create;
try
try
if fileexists(ExtractFilePath(application.ExeName)+'all_user') then deletefile(ExtractFilePath(application.ExeName)+'all_user');
if fileexists(ExtractFilePath(application.ExeName)+'exits_user') then deletefile(ExtractFilePath(application.ExeName)+'four_user');
strList.Clear;
for i:= strtoint(str_b) to strtoint(str_e) do
begin
strList.Add(InsertStrNo(IntToStr(i)));
end;
except
MessageBox(handle,'初始化编号出错','系统提示',MB_ICONERROR);
end;
strList.SaveToFile(ExtractFilePath(application.ExeName)+'all_user');
MessageBox(handle,'初始化编号完毕','系统提示',MB_ICONINFORMATION);
Close;
finally
strList.Free;
end;end;end.
嗯。很簡單的程序,用TIMER+Random即可實現請參考阿三的代碼即可實現你的需求...
因为它每次重新开始,得到的数都是一样的。
并没有做到真正的随机。
建议用线程循环,手动终止的方式。