各位老大,帮解答一下吧
up一下
//又是,我
//原理是:将验证码移到可见区域,拷贝验证码所在的屏幕图像,保存到位图中
//参考如下代码
uses MSHTML;
function TakeDesktop( // 获取屏幕指定区域的图像
mBitmap: TBitmap; // 输出位图
mRect: TRect // 获取的区域
): Boolean; // 返回截取是否成功
var
vDC: HDC;
begin
Result := False;
if not Assigned(mBitmap) then Exit;
mBitmap.Canvas.FillRect(mBitmap.Canvas.ClipRect);
mBitmap.Width := mRect.Right - mRect.Left;
mBitmap.Height := mRect.Bottom - mRect.Top;
mBitmap.PixelFormat := pf32bit;
vDC := GetWindowDC(GetDesktopWindow);
try
BitBlt(mBitmap.Canvas.Handle, 0, 0, mBitmap.Width, mBitmap.Height,
vDC, mRect.Left, mRect.Top, SRCCOPY);
finally
DeleteDC(vDC);
end;
Result := True;
end; { TakeDesktop }
procedure TForm1.FormCreate(Sender: TObject);
begin
WebBrowser1.Navigate('http://www.harbin.gov.cn/ztbd/sdmp/wytp.php');
end;
procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
var
vHTMLDocument2: IHTMLDocument2;
vHTMLElementCollection: IHTMLElementCollection;
vHTMLElement: IHTMLElement;
vHTMLImgElement: IHTMLImgElement;
I: Integer;
vRect: TRect;
begin
if not Assigned(TWebBrowser(Sender).Document) then Exit;
vHTMLDocument2 := TWebBrowser(Sender).Document as IHTMLDocument2;
vHTMLElementCollection := vHTMLDocument2.body.all as IHTMLElementCollection;
for I := 0 to vHTMLElementCollection.length - 1 do
begin
vHTMLElement := vHTMLElementCollection.item(I, 0) as IHTMLElement;
if SameText(vHTMLElement.tagName, 'img') then
begin
vHTMLImgElement := vHTMLElement as IHTMLImgElement;
if Pos('vimage.php', vHTMLImgElement.src) > 0 then
begin
vHTMLElement.scrollIntoView(0);
Application.ProcessMessages;
vRect.TopLeft := Point(TWebBrowser(Sender).Left, TWebBrowser(Sender).Top);
vRect.TopLeft := ClientToScreen(vRect.TopLeft); // 计算WebBrowser屏幕位置
OffsetRect(vRect, 130, 184); // 这里根据具体情况调整
vRect.Right := vRect.Left + 80;
vRect.Bottom := vRect.Top + 25;
TakeDesktop(Image1.Picture.Bitmap, vRect);
Break;
end;
end;
end;
end;
呵呵,再次谢谢 伴水清清
http://support.microsoft.com/kb/293125/en-us
http://www.harbin.gov.cn/ztbd/sdmp/wytp.php
的那个验证码的文本框我怎么写上数据呢,前面的姓名的文本框我是用
ovElements := WebBrowser1.OleObject.Document.all.tags('INPUT');
for i := 0 to (ovElements.Length - 1) do
begin
if (UpperCase(ovElements.item(i).type) = 'TEXT') and (UpperCase(ovElements.item(i).name) = 'MYNAME') then
begin
ovElements.item(i).value:='zb';
break;
end;
end;
写上的数据,但是下面那个框的类型好象有问题type=number name=number
uses Math, MSHTML;
function TakeDesktop( // 获取屏幕指定区域的图像
mBitmap: TBitmap; // 输出位图
mRect: TRect // 获取的区域
): Boolean; // 返回截取是否成功
var
vDC: HDC;
begin
Result := False;
if not Assigned(mBitmap) then Exit;
mBitmap.Canvas.FillRect(mBitmap.Canvas.ClipRect);
mBitmap.Width := mRect.Right - mRect.Left;
mBitmap.Height := mRect.Bottom - mRect.Top;
mBitmap.PixelFormat := pf32bit;
vDC := GetWindowDC(GetDesktopWindow);
try
BitBlt(mBitmap.Canvas.Handle, 0, 0, mBitmap.Width, mBitmap.Height,
vDC, mRect.Left, mRect.Top, SRCCOPY);
finally
DeleteDC(vDC);
end;
Result := True;
end; { TakeDesktop }
procedure TForm1.FormCreate(Sender: TObject);
begin
WebBrowser1.Navigate('http://www.harbin.gov.cn/ztbd/sdmp/wytp.php');
end;
function SameCanvas(mCanvasA, mCanvasB: TCanvas): Boolean; { 比较两个画布是否相同 }
var
I, J: Integer;
begin
Result := False;
if not Assigned(mCanvasA) then Exit;
if not Assigned(mCanvasB) then Exit;
for I := Min(mCanvasA.ClipRect.Left, mCanvasB.ClipRect.Left) to
Min(mCanvasA.ClipRect.Right, mCanvasB.ClipRect.Right) do
for J := Min(mCanvasA.ClipRect.Top, mCanvasB.ClipRect.Top) to
Min(mCanvasA.ClipRect.Bottom, mCanvasB.ClipRect.Bottom) do
begin
if mCanvasA.Pixels[I, J] <> clBlack then mCanvasA.Pixels[I, J] := clWhite;
if mCanvasB.Pixels[I, J] <> clBlack then mCanvasB.Pixels[I, J] := clWhite;
if mCanvasA.Pixels[I, J] <> mCanvasB.Pixels[I, J] then Exit;
end;
Result := True;
end; { SameCanvas }
procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
const
cPoints: array[0..3] of TPoint = (
(X: -5; Y: -9),
(X: -26; Y: -9),
(X: -47; Y: -9),
(X: -68; Y: -9)
);
var
vHTMLDocument2: IHTMLDocument2;
vHTMLElementCollection: IHTMLElementCollection;
vHTMLElement: IHTMLElement;
vHTMLImgElement: IHTMLImgElement;
vHTMLInputElement: IHTMLInputElement;
I, J: Integer;
vRect: TRect;
vNumber: TBitmap;
vBitmap: TBitmap;
S: string;
begin
if not Assigned(TWebBrowser(Sender).Document) then Exit;
vHTMLDocument2 := TWebBrowser(Sender).Document as IHTMLDocument2;
vHTMLElementCollection := vHTMLDocument2.body.all as IHTMLElementCollection;
for I := 0 to vHTMLElementCollection.length - 1 do
begin
vHTMLElement := vHTMLElementCollection.item(I, 0) as IHTMLElement;
if SameText(vHTMLElement.tagName, 'img') then
begin
vHTMLImgElement := vHTMLElement as IHTMLImgElement;
if Pos('vimage.php', vHTMLImgElement.src) > 0 then
begin
vHTMLElement.scrollIntoView(0);
Application.ProcessMessages;
vRect.TopLeft := Point(TWebBrowser(Sender).Left, TWebBrowser(Sender).Top);
vRect.TopLeft := ClientToScreen(vRect.TopLeft); // 计算WebBrowser屏幕位置
OffsetRect(vRect, 130, 184); // 这里根据具体情况调整
vRect.Right := vRect.Left + 80;
vRect.Bottom := vRect.Top + 25;
TakeDesktop(Image1.Picture.Bitmap, vRect);
Break;
end;
end;
end;
vNumber := TBitmap.Create;
vBitmap := TBitmap.Create;
try
vBitmap.Width := 8;
vBitmap.Height := 10;
S := '';
for J := 0 to 3 do
begin
vBitmap.Canvas.FillRect(vBitmap.Canvas.ClipRect);
vBitmap.Canvas.Draw(cPoints[J].X, cPoints[J].Y, Image1.Picture.Graphic);
for I := 0 to 9 do
begin
vNumber.Canvas.FillRect(vNumber.Canvas.ClipRect);
ImageList1.GetBitmap(I, vNumber);
if SameCanvas(vBitmap.Canvas, vNumber.Canvas) then
begin
S := S + IntToStr(I);
Break;
end;
end;
end;
finally
vBitmap.Free;
vNumber.Free;
end;
vHTMLInputElement := (vHTMLDocument2 as IHTMLDocument3
).getElementById('number') as IHTMLInputElement;
if Assigned(vHTMLInputElement) then vHTMLInputElement.value := S;
end;