想用delphi做一个抽奖程序,功能是六位数随机滚动,按下某个键后,停留一组数字。没什么delphi基础,请高人们给指点一下应怎么做。

解决方案 »

  1.   

     思路很简单,就是在一个定时器里生成随机的6位数,定时器开始Enable为 false。点击 开始时 Enable 变为true.  点击结束时Enable 为 false. 定时器的interval 设置10MS左右就可以看到滚动效果。
      

  2.   


    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.
      

  3.   

    在盒子上给你找到了一个:http://www.2ccc.com/article.asp?articleid=5563
      

  4.   

    简单直接用随机数来做就可以,用timer来控制循环跳动
      

  5.   

    不好意思,我说的不是太清楚,补充一下:
    1.六位随机数可以重复,且不是事先输入备选,而是纯系统随机产生。(也想了解一下要是事先有备选随机数,且已经抽出过的中奖号要排除应怎么做)
    2.系统还要实现自动保存抽出过的所有号,想用sql做表保存。
      

  6.   

    简单弄了一个,没有用到线程,最好用线程来实现:
    代码如下:
    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.
      

  7.   


    嗯。很簡單的程序,用TIMER+Random即可實現請參考阿三的代碼即可實現你的需求...
      

  8.   

    用timer 控制停/抽  在sql语句后在加上 order by newid()这样就可以随机选了
      

  9.   

    我做过一个,原理是载入TXT的,如果要防止再次中奖,把中奖的名单给删了,就行了,用线程滚动,
      

  10.   

    这个 Random 不好
    因为它每次重新开始,得到的数都是一样的。
    并没有做到真正的随机。
    建议用线程循环,手动终止的方式。
      

  11.   

    就是在一个定时器里生成随机的6位数,定时器开始Enable为 false。点击 开始时 Enable 变为true. 点击结束时Enable 为定时器的interval 设置10MS左右就可以看到滚动效果。