The functions to be implemented are shown in the following picture. The specific details are as follows:
Now I will introduce some other functions of the software. I hope everyone will benefit.
Simulator search products
When brushing orders, you cannot directly get a product URL and enter the purchase page. You have to simulate an artificial search.
There are two difficulties in this process:
1) Asynchronous loading of product list; 2) Turn pages and take screenshots;
In the garden, I no longer play with a big sword in front of Guan Gong.
Directly upload the key code:
i: Search for products and turn pages
public bool? SearchProduct(TaskDetailModel taskDetailData){bool? result = null;bool isIndex = true;bool isList = true;WebBrowserTask.Instance.SetProperties();WebBrowserTask.Instance.ClearDocumentCompleted();WebBrowserTask.Instance.DocumentCompleted += (wbSenderSearch, wbESearch) =>{System.Windows.Forms.WebBrowser currentWB = wbSenderSearch as System.Windows.Forms.WebBrowser;System.Windows.Forms.HtmlDocument currentDoc = currentWB.Document;mshtml.HTMLDocument currentDom = currentDoc.DomDocument as mshtml.HTMLDocument;String wbUrl = wbESearch.Url.ToString();if (currentWB.ReadyState == System.Windows.Forms.WebBrowserReadyState.Complete){#region Home Search if (wbUrl.Contains("www.taobao.com")){if (isIndex == true){isIndex = false;taskDetailData.DetailRemark = String.Format(@"Enter keywords""{0}""Search for product...", taskDetailData.TaskName);Func<bool> func = () =>{bool asynctag = false;System.Threading.Thread.Sleep(5000);asynctag = true;return asynctag;};func.BeginInvoke((ar) =>{bool asyncresult = func.EndInvoke(ar);if (asyncresult){System.Windows.Forms.HtmlElement heee = currentDoc.GetElementById("J_SearchTab");String classname = heee.GetAttribute("classname");System.Windows.Forms.HtmlElement hitem = heee.Children[0];System.Windows.Forms.HtmlElementCollection heclis = hitem.Children;System.Windows.Forms.HtmlElement li1 = heclis[0];System.Windows.Forms.HtmlElement li2 = heclis[1];System.Windows.Forms.HtmlElement li3 = heclis[2];foreach (System.Windows.Forms.HtmlElement li in heclis){String liclass = li.GetAttribute("classname");if (liclass.Contains("selected")){System.Windows.Forms.HtmlElement q = currentDoc.GetElementById("q");System.Windows.Forms.HtmlElement btnsearch = currentDoc.GetElementById("J_TSearchForm").Children[0].Children[0];if (q != null && btnsearch != null){q.Focus();q.SetAttribute("value", taskDetailData.TaskName);btnsearch.Focus();string savePath = Path.Combine(UserData.WorkBenchDirectory, taskDetailData.TaskDetailCode, String.Format("Search Submission.jpg", ""));CaptureImage.CaptureWebPageArea(currentDom, savePath);btnsearch.InvokeMember("click");}}}}}},null);}}#endregion Home Search#region Product List if (wbUrl.Contains("s.taobao.com")){if (isList == true){isList = false;Func<bool> func = () =>{bool asynctag = false;asynctag = true;return asynctag;};func.BeginInvoke((ar) =>{bool asyncresult = func.EndInvoke(ar);if (asyncresult){//Analyze the product per page String clickProductURL = TurningAndParsePage(currentDoc, taskDetailData);result = true;}},null);}}#endregion Product List}}; //DocumentCompleted End System.Windows.Application.Current.Dispatcher.Invoke(new System.Action(() =>{WebBrowserTask.Instance.Navigate("https://www.taobao.com/");}));for (int i = 0; i < 120; i++){System.Threading.Thread.Sleep(1000);if (result != null){break;}}return result;} ii: Because each page is loaded abnormally, choose the appropriate time to take a screenshot of the web page and capture the entire web page:
/*Because the control is included, if it is called in the thread, the Invoke method must be used with System.Windows.Application.Current.Dispatcher.Invoke(new System.Action(() =>{//htmlDoc.Window.ScrollTo(new System.Drawing.Point(5000, htmlDoc.Body.ScrollRectangle.Height));string savePath = string.Format(@"D:/{0}.jpg", Guid.NewGuid().ToString());CaptureImage.CaptureWebPage(webBrowserTask, savePath);}), System.Windows.Threading.DispatcherPriority.Background);*//// <summary>//// Intercept the entire web page//// </summary>//// <param name="web"></param>//// <param name="savePath"></param>public static void CaptureWebPage(System.Windows.Forms.WebBrowser web, String savePath){Rectangle body = web.Document.Body.ScrollRectangle;Rectangle docRectangle = new Rectangle(){Location = new Point(0, 0),Size = new Size(body.Width, body.Height)};web.Dock = DockStyle.None;web.Width = docRectangle.Width;web.Height = docRectangle.Height;Rectangle imgRectangle = docRectangle; using (Bitmap bitmap = new Bitmap(imgRectangle.Width, imgRectangle.Height)){IViewObject ivo = web.Document.DomDocument as IViewObject;using (Graphics g = Graphics.FromImage(bitmap)){IntPtr hdc = g.GetHdc();ivo.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, hdc, ref imgRectangle, ref docRectangle, IntPtr.Zero, 0);g.ReleaseHdc(hdc);}bitmap.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);bitmap.Dispose();}} Snap a certain area of the web page:
/// <summary>//// Intercept part of the web page /// </summary>/// <param name="htmlDom"></param>/// <param name="savePath"></param>public static void CaptureWebPageArea(mshtml.HTMLDocument htmlDom, String savePath){String saveDir = System.IO.Path.GetDirectoryName(savePath);if (!System.IO.Directory.Exists(saveDir)){System.IO.Directory.CreateDirectory(saveDir);}Rectangle docRectangle = new Rectangle(){Location = new Point(0, 0),//Size = new Size(htmlDom.body.offsetWidth, htmlDom.body.offsetHeight)Size = new Size((int)System.Windows.SystemParameters.PrimaryScreenWidth, (int)System.Windows.SystemParameters.PrimaryScreenHeight)};Rectangle imgRectangle = docRectangle; using (Bitmap bitmap = new Bitmap(imgRectangle.Width, imgRectangle.Height)){IViewObject ivo = htmlDom as IViewObject;using (Graphics g = Graphics.FromImage(bitmap)){IntPtr hdc = g.GetHdc();ivo.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, hdc, ref imgRectangle, ref docRectangle, IntPtr.Zero, 0);g.ReleaseHdc(hdc);}bitmap.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);bitmap.Dispose();}} There are many interesting snippets in this code. Friends who are interested will find out.