Using the WebBrowser control is really convenient to implement, but it is less flexible than sending packets in a direct group. For the Three Kingdoms web game, WebBrowser can theoretically implement the desired function if it is used well (I only implement the automatic "referral" on the whole point ",
The principle is the same ).
You can use the packet capture tool to check which packages are available during the game.
Some web games are Flash interfaces, such as happy farm in the school. WebBrowser is not suitable for auxiliary tools for this game .. NET also has the Flash control, but this control function is very weak, a lot of Flash is not available. The only method that comes up with is
There are original group packages.
WebBrowser has a disadvantage that it can only exist in the main thread. When the webBrowser processes data, the entire program is busy. clicking the program does not respond (not responding ). When the program is running, reduce the time required to process data in webBrowser as much as possible,
WebBrowser updates data. Others are implemented in other threads.
WebBrowser. Navigate (strURL );
You can obtain the string content through webBrowser:
WebBrowser. Document. Body. InnerHtml;
The "game guide" is actually a link:
<A class = topbutton-guide title = game guide href = "javascript: MM_iframePost (guide. nation);"> game guide </A>
You only need to execute the "javascript: MM_iframePost (guide. nation);" script in the code, which is equivalent to clicking the "game guide" in the game ".
You can use HtmlElement. InvokeMember to execute the script.
...
HtmlElementCollection aHtml = webBrowser. Document. GetElementsByTagName ("");
DoClick ("game guide ");
Private void DoClick (string name)
{
Foreach (HtmlElement h in aHtml)
{
If (h. InnerText! = Null & h. InnerText. Contains (name ))
{
H. InvokeMember ("click ");
// Wait until the webBrowser refresh is complete
WaitForEventCompleted (1000 );
Break;
}
Else
{
Continue;
}
}
}
Private void WaitForEventCompleted (int time)
{
AutoResetEvent. Reset ();
While (autoResetEvent. WaitOne (time, false) = false)
{
Application. DoEvents ();
}
}
After clicking the corresponding link, the webBrowser must refresh the data accordingly. Therefore, after the operation, you must wait until the webBrowser updates the data. Each time data is updated, the webBrowser_DocumentCompleted event is not executed once,
It seems that it was three times, but I found the cause, and now I forgot it. The code is not stable yet. You can capture the packet to investigate the cause.
Regularly execute the "", and use DoCallback and Invoke between different threads:
Private void TimerDoFamous ()
{
While (true)
{
If (DateTime. Now. Second = 10 & DateTime. Now. Minute = 2)
{
If (webBrowser. InvokeRequired)
{
DoCallback d = new DoCallback (DoFamousGeneral );
This. Invoke (d );
D = new DoCallback (Wait );
This. Invoke (d );
D = new DoCallback (DoAnotherGeneral );
This. Invoke (d );
}
Else
{
DoCallback d = new DoCallback (DoFamousGeneral );
This. Invoke (d );
D = new DoCallback (Wait );
This. Invoke (d );
D = new DoCallback (DoAnotherGeneral );
This. Invoke (d );
}
}
}
}
How to get the desired html element from webBrowser. Document. Body. InnerHtml using a regular expression. The regular expression can also be used to obtain all the data of the current town, such as the number of resources and the building level.
Obtain the regular expression of the number of iron ore:
// Iron
Public const string IronNowPat = @ "<SPAN id = iron_now> d * </SPAN> ";
Public const string IronNowHeader