構造HTML源代碼: procedure TFrmMain.makehtml; var liststr1,liststr2,checkNum1,checkNum2:string; htmlFile:TextFile; i,j,who,part1,part2,num1,probility:integer; begin liststr1:=''; liststr2:=''; htmlbody:='<HTML><meta http-equiv="Content-Type" content="text/html; charset=gb2312">'; htmlbody:=htmlbody+'<BODY background="bgpaper.gif"><TITLE>投票系統</TITLE><p align="center"> </p>'; htmlbody:=htmlbody+'<INPUT TYPE=hidden NAME="LinkURL" VALUE="">'; htmlbody:=htmlbody+'<TABLE width="80%" align="center" border="1" bordercolor="#339966"><TR bordercolor="#FFFFFF">'; htmlbody:=htmlbody+'<TD ALIGN=center COLSPAN=2>'; 。。。。。。。。。。。。 //構造投票選項――――――――――――――――――――― for j:=1 to 23 do begin checklist1[j]:=false; checklist2[j]:=false; end; RandSeed:= inttime; Randomize; //根據設置的投票選擇概率來確定本次是否選擇目標選項 if factor = 0 then part1 := Random(10) else part1 := Random(11); num1:=part1; while (part1>0) do begin who := Random(23)+1; for j:=1 to 23 do begin if checklist1[who] then break else begin checklist1[who]:=true; part1:=part1-1; end; end; end; probility := Random(100)+1; if factor = 0 then checklist2[2]:=true else if (probility Mod factor) = 0 then checklist2[2]:=true; if checklist2[2] then part2:=(10-num1)-1 else part2:=(10-num1); while (part2 > 0 ) do begin who := Random(23)+1; for j:=1 to 23 do begin if checklist2[who] then // break else begin checklist2[who]:=true; part2:=part2-1; end; end; end; for j:=1 to 23 do begin if checklist1[j] then begin liststr1:=liststr1+checkedlist1[j]; checkNum1:=checkNum1+inttostr(j)+','; end else liststr1:=liststr1+uncheckedlist1[j]; if checklist2[j] then begin liststr2:=liststr2+checkedlist2[j]; checkNum2:=checkNum2+inttostr(j)+','; end else liststr2:=liststr2+uncheckedlist2[j]; end; RichEdit1.Lines.Add('第'+inttostr((times div 2)+1)+'次投票結果:'); RichEdit1.Lines.Add('第一組選中序號('+inttostr(num1)+'個):'+checkNum1); RichEdit1.Lines.Add('第二組選中序號('+inttostr((10-num1))+'個):'+checkNum2); htmlbody:=htmlbody+liststr2; htmlbody:=htmlbody+'<INPUT TYPE=hidden NAME="CHOICE 2" VALUE="23"><TR bgcolor="#E7E8E3" bordercolor="#FFFFFF"><TD bgcolor="#CCCCCC"><TD ALIGN=center><INPUT name="sub" TYPE=submit VALUE="提交"></TABLE></FORM></BODY></HTML>'; end;
把生成的HTML載入Webbrowser,生成網頁 表格 2procedure TFrmMain.loadHtml; var URL,v:OleVariant; HTMLDocument:ihtmldocument2; begin try makehtml; except on e:exception do //showmessage('出錯!'+e.Message); //exit; end; HTMLDocument := (WebBrowser1.Document as IHTMLDocument2); v := VarArrayCreate([0, 0], varVariant); v[0] := htmlbody; // Here's your HTML string HTMLDocument.Write(PSafeArray(TVarData(v).VArray)); HTMLDocument.Close; end;
然後模擬點擊網頁的提交按鈕,提交表單數據: 表格 3procedure TFrmMain.submitForm; var obj:OleVariant; doc: ihtmldocument2; begin doc:=(WebBrowser1 .Document as IHTMLDocument2); if doc <> nil then begin //RichEdit1.text:=doc.body.innerText; obj:=doc.all.item('sub',0); obj.click; end; end;
設置代理服務器: 表格 4function TFrmcheckproxy.ChangeProxy(const Proxy, Port,ByPass: string; const bEnabled: boolean = True): boolean; var reg: Tregistry; info: INTERNET_PROXY_INFO; Fproxy:string; begin Result := False; FProxy:=Format('%s:%s',[Proxy,Port]); reg := Tregistry.Create; try reg.RootKey := HKEY_CURRENT_USER; if reg.OpenKey('SoftwareMicrosoftWindowsCurrentVersionInternet Settings', True) then begin reg.Writestring('ProxyServer', Fproxy); reg.WriteInteger('ProxyEnable', integer(bEnabled)); info.dwAccessType := INTERNET_OPEN_TYPE_PROXY; info.lpszProxy := pchar(proxy); info.lpszProxyBypass := pchar(ByPass); InternetSetOption(nil, INTERNET_OPTION_PROXY, @info, SizeOf(Info)); InternetSetOption(nil, INTERNET_OPTION_SETTINGS_CHANGED, nil, 0); // InternetSetOption(nil, INTERNET_OPTION_REFRESH, nil, 0); // Sendmessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0); Result:=True; end finally reg.CloseKey; reg.free; end; end;
最後界面: 圖表 1 主界面(使用了一個界面的第三方包-SUIPackage) 圖表 2圖表 1 主界面,同時查看投票過程和結果(使用了一個界面的第三方包-SUIPackage)