// 添加MSHTML引用
// 添加MSHTML引用
// 添加MSHTML引用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using mshtml;
using System.Threading;
using System.Runtime.InteropServices;
namespace ViewYZM
{
//要发信息数据结构,作为SendMessage函数的LParam参数
public struct SENDDATASTRUCT
{
public IntPtr dwData; //附加一些个人自定义标志信息,自己喜欢
public int DataLength; //信息的长度
[MarshalAs(UnmanagedType.LPStr)]
public string lpData; //要发送的信息
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//自定义的消息
public const int USER = 0x500;
public const int MSG_VEIW_YZM = USER + 1;
public IntPtr mMainHandle = IntPtr.Zero;
//消息发送API
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(
IntPtr hWnd, // 信息发住的窗口的句柄
int Msg, // 消息ID
int wParam, // 参数1
ref SENDDATASTRUCT lParam // 参数2 [MarshalAs(UnmanagedType.LPTStr)]StringBuilder lParam
);
string mWerbUrl = "http://xxxxxx";
string mYmzUrl = string.Empty;
private void Form1_Load(object sender, EventArgs e)
{
mMainHandle = this.Handle;
// http://xxxx/getCaptcha
mYmzUrl = mWerbUrl + "/webwcf/BidCmd.svc/WebCmd/getCaptcha"; // 验证码图片url
webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
webBrowser.Navigate(mWerbUrl);
//this.ptYzm.Image = null;
//this.ptYzm.WaitOnLoad = false; //设置为异步加载图片
//this.ptYzm.SizeMode = PictureBoxSizeMode.Normal;
//this.ptYzm.LoadAsync(mYmzUrl);
}
/// <summary>
/// 网页加载完毕
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void webBrowser_DocumentCompleted(object sender, EventArgs e)
{
// 网页加载完成 此时验证码图片不一定已经加载
// 为了保险,开个线程等待几秒钟,然后再获取即可
Thread thd = new Thread(ThreadViewYzm);
thd.Start();
}
private void ThreadViewYzm(object prm)
{
Thread.Sleep(1000 * 5); // 等待验证图片加载完成
SENDDATASTRUCT myData = new SENDDATASTRUCT();
SendMessage(mMainHandle, MSG_VEIW_YZM, 0, ref myData);
}
/// <summary>
/// 获取验证码方法
/// </summary>
/// <param name="WebControlName"></param>
/// <param name="ImgeTag"></param>
/// <returns></returns>
private Image GetCodeImage(WebBrowser WebControlName, HtmlElement ImgeTag)
{
HTMLDocument hdoc = (HTMLDocument)WebControlName.Document.DomDocument;
HTMLBody hbody = (HTMLBody)hdoc.body;
IHTMLControlRange hcr = (IHTMLControlRange)hbody.createControlRange();
IHTMLControlElement hImg = (IHTMLControlElement)ImgeTag.DomElement;
hcr.add(hImg);
hcr.execCommand("Copy", false, null);
Image CodeImage = Clipboard.GetImage();
return CodeImage;
}
private void btnGetYzm_Click(object sender, EventArgs e)
{
HtmlElement ImageCodeTag = webBrowser.Document.GetElementById("imgcode");
Image FinalImage = GetCodeImage(webBrowser, ImageCodeTag);
this.ptYzm.Image = FinalImage;
}
///重写窗体的消息处理函数DefWndProc,从中加入自己定义消息 MYMESSAGE 的检测的处理入口
protected override void DefWndProc(ref Message m)
{
switch (m.Msg)
{
//接收自定义消息MYMESSAGE,并显示其参数
case MSG_VEIW_YZM:
btnGetYzm_Click(null, null);
break;
default:
base.DefWndProc(ref m);
break;
}
}
}
}