关于 VB.NET 好像是 VB 2005 的前身,这我也搞不太清楚
总之大家都习惯把 VB 分为两阶段,一个是 古老的 VB6 另外一个就是 VB.NET
至于该称呼 VB.NET 还是 2005, 2008, 2010 这我就不知道
昨天又试了很多次,终于找到如何让 WebBrowser 中的图片被点下去时
VB 中可以一起被触发
先要用这两行来让 程序被触发时,可以一起触发自己设定的函数
不过这次作的更新器模拟只有用到 Navigating 被触发的时候
复制程式
AddHandler wb_back.Navigating, AddressOf local_event_wb_back_nav
AddHandler wb_back.Document.Click, AddressOf local_event_wb_back_click
然后在函数内可以用 wb_back.Document.GetElementById(Name) 来取得特定 ID 的物件
wb_back.Document.GetElementById(Name).GetAttribute(Attr) 则可以取得 属性,例如 Checkbox 勾了没
wb_back.Document.InvokeScript(Name, Obj) 则可以触发 网页中的 JavaScript,也能取得特定函数的值
wb_back.Document.ActiveElement.Name 则可以取得目前物件的名称
有了以上方法,我就写了第二版的 模拟CSO更新器
底下的程式码在 Form1 (frm_main)
Form1 :复制程式
Public Class frm_main
'---------- Local structures ----------
Structure struc_prg
Dim pack As Collection
Dim curInd As Long
Dim accum_total, accum_max As Long
Dim cur_val, cur_max As Long
Dim total_val, total_max As Long
End Structure
'---------- Local variables ----------
'Files & Folders
Dim dataDir As String = System.IO.Directory.GetCurrentDirectory & "\HTML\"
'Status
Dim stat_check_obj_ind As Integer
Dim stat_wb_complete As Integer
'Progress
Dim prg As struc_prg
'---------- Local objects ----------
Dim WithEvents wb_back As New WebBrowser
Private Sub frm_main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i, i2 As Integer
'----- Variables -----
'Status
stat_wb_complete = 0
'Size : Pack (@@@ Test @@@)
prg.pack = New Collection
Dim arrPack(,) = {{"Server Info", 100, "伺服器讯息"}, {"Map", 3000, "地图档"}, {"Amxx", 888, "插件平台"}, {"Plugin", 1999, "插件"}}
For i = 0 To arrPack.GetUpperBound(0)
For i2 = 0 To arrPack.GetUpperBound(1)
prg.pack.Add(arrPack(i, i2))
Next
Next
For i = 1 To prg.pack.Count / 3
prg.accum_max += prg.pack.Item(i * 3 - 1)
Next
prg.curInd = 0
'----- Form -----
'Size
Me.Size = New Size(704, 466)
'Style
Me.FormBorderStyle = 0
Me.BackColor = Color.White
'----- Sub -----
'Build objects
local_build_obj()
End Sub
Private Sub timer_check_obj_stat_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer_check_obj_stat.Tick
Dim flag As Boolean
Select Case stat_check_obj_ind
Case 1 'Web browser(Back)
If wb_back.ReadyState = WebBrowserReadyState.Complete Then
flag = True
wb_back.Visible = True
stat_wb_complete = 1
local_new_pack() 'Test @@@
End If
End Select
If flag Then timer_check_obj_stat.Enabled = False
End Sub
Private Sub timer_simul_prg_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer_simul_prg.Tick
'Test @@@
Dim objArr(1) As Object
Randomize()
prg.cur_val += (Fix(50 * Rnd()) + 30)
If prg.cur_val >= prg.pack.Item(prg.curInd * 3 - 1) Then
prg.accum_total += prg.pack.Item(prg.curInd * 3 - 1)
timer_simul_prg.Enabled = False
local_new_pack()
Exit Sub
End If
Randomize()
timer_simul_prg.Interval = IIf(Fix(10 * Rnd()) = 0, 500, Fix(50 * Rnd()) + 1)
'Set current progress
objArr(0) = CObj(prg.cur_val) 'Value
objArr(1) = CObj(prg.cur_max) 'Max
local_wb_callScript("SetCurrentProgress", objArr)
'Status
objArr(0) = CObj(prg.pack.Item(prg.curInd * 3 - 2) & " 安装中...(" & prg.cur_val & "/" & prg.cur_max & ")") 'Value
local_wb_callScript("SetStatusText", objArr)
End Sub
Private Sub local_build_obj()
Dim arrList As New List(Of Control)
Dim arrSize As New List(Of Size)
Dim arrPoint As New List(Of Point)
Dim i As Integer
arrList.Add(wb_back)
arrSize.Add(New Size(688, 430))
arrPoint.Add(New Point(0, 0))
'--- Web browser ---
For i = 0 To arrList.Count - 1
arrList(i).Visible = False
arrList(i).Parent = Me
arrList(i).Size = arrSize(i)
arrList(i).Location = arrPoint(i)
wb_back.ScrollBarsEnabled = False
'wb_back.ScriptErrorsSuppressed = True
Next i
'Write files
Dim tempPath As String = dataDir & "133.html"
My.Computer.FileSystem.WriteAllText(tempPath, My.Resources._133, False)
'Navigate URL
wb_back.Navigate("file://" & tempPath)
local_new_objcheck(1)
'Add handler
AddHandler wb_back.Navigating, AddressOf local_event_wb_back_nav
AddHandler wb_back.Document.Click, AddressOf local_event_wb_back_click
End Sub
Private Sub local_new_objcheck(ByRef ind As Integer)
stat_check_obj_ind = ind
timer_check_obj_stat.Enabled = True
End Sub
Private Sub local_event_wb_back_nav()
If stat_wb_complete = 0 Then Exit Sub
Select Case wb_back.Document.ActiveElement.Id
Case "startGame"
If wb_back.Document.GetElementById("agreement").GetAttribute("checked") = True Then
If wb_back.Document.InvokeScript("get_CanStart") = 1 Then
MsgBox("Game started.", MsgBoxStyle.Information, "Test")
End
End If
End If
Case "exitGame"
If wb_back.Document.InvokeScript("get_CanStart") = 0 Then
If MsgBox("更新作业进行中.请问要结束吗?", MsgBoxStyle.Exclamation + MsgBoxStyle.OkCancel, "CSOLauncher(Simulation)") = MsgBoxResult.Ok Then
End
End If
Else
End
End If
Case "closeGame"
If wb_back.Document.InvokeScript("get_CanStart") = 0 Then
If MsgBox("更新作业进行中.请问要结束吗?", MsgBoxStyle.Exclamation + MsgBoxStyle.OkCancel, "CSOLauncher(Simulation)") = MsgBoxResult.Ok Then
End
End If
Else
End
End If
End Select
End Sub
Private Sub local_event_wb_back_click()
Select Case wb_back.Document.ActiveElement.Name
Case ""
Case "agreement"
End Select
End Sub
Private Sub local_renew_finishprg()
local_wb_callScript("OnAllDone", Nothing)
End Sub
Private Sub local_wb_callScript(ByRef s As String, ByRef objArr As Object)
wb_back.Document.InvokeScript(s, objArr)
End Sub
Private Sub local_new_pack() 'Test @@@
If prg.curInd = prg.pack.Count / 3 Then local_renew_finishprg() : Exit Sub
Dim ObjArr(1) As Object
prg.curInd += 1
'Status
ObjArr(0) = CObj("正在安装" & prg.pack.Item(prg.curInd * 3) & "...")
local_wb_callScript("SetStatusText", ObjArr)
prg.cur_val = 0
prg.cur_max = prg.pack.Item(prg.curInd * 3 - 1)
'Reset total progress
ObjArr(0) = CObj(prg.accum_total)
ObjArr(1) = CObj(prg.accum_max)
local_wb_callScript("SetTotalProgress", ObjArr)
'Reset current progress
ObjArr(0) = CObj(0)
ObjArr(1) = CObj(prg.cur_max)
local_wb_callScript("SetCurrentProgress", ObjArr)
timer_simul_prg.Enabled = True
End Sub
End Class
必须加入
Timer : timer_check_obj_stat (Interval = 100, Enabled=0) , timer_simul_prg (Interval = 1000, Enabled=0)
必须加入
Resource : _133 (Files)
必须在程式执行资料夹放上 HTML资料夹(可以从底下的附件中找到)
Resource : _133 如下,其中被我修改了一点点
Resource : _133 :复制程式
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;" />
<title>Counter-Strike Online</title>
<style>
body {
background-color : gray;
margin : 0;
padding : 0;
font:12px Tahoma,Dotum,络遗,Gulim,Verdana,sans-serif,serif;
}
.FrameBody {background-color: transparent}
img {border:0;}
#Wrapper01 {position:relative;width:688px;height:430px;background:url(bg01.jpg) no-repeat;}
#Wrapper02 {position:relative;width:688px;height:430px;background:url(bg02.jpg) no-repeat;}
#NoticeFrame {position:absolute; left:8; top:185; width:671px; height:236px; z-index:3}
#Install {position:absolute;top:145px;left:8px;width:671px;height:44px;}
#BarTotalDiv {width:671px;height:6px;margin:0 0 0 0px;}
#BarNowDiv {width:671px;height:6px;margin:5px 0 0 0px;}
#InstallTxt {margin:6px 0 0 0;color:#FFFFFF;}
#InstallTxt #InstallTxtPer {font-weight:bold;color:#C2B49A;float:right}
.BtClose {position:absolute; left:673; top:6; width:13px; height:14px; z-index:3}
.BtClose a {width:13;height:14px;text-indent:-9000px;display:block;}
#Wrapper01 .BtClose {background:url(close_n.jpg) no-repeat;}
#Wrapper02 .BtClose {background:url(close_n.jpg) no-repeat;}
.BtGamestart {position:absolute; left:419px; top:115; width:154px; height:28px; z-index:1}
.BtGamestart a {width:154px;height:28px;text-indent:-9000px;display:block;}
.BtGamestart a:hover {width:154px;height:28px;text-indent:-9000px;display:block;}
.BtGamestart_d {position:absolute; left:419px; top:115; width:154px; height:28px; z-index:1}
.BtGamestart_d a {width:154px;height:28px;text-indent:-9000px;display:block;}
.BtGamestart_d a:hover {width:154px;height:28px;text-indent:-9000px;display:block;}
#Wrapper01 .BtGamestart_d {background:url(start_d.jpg) no-repeat;}
#Wrapper02 .BtGamestart_d {background:url(start_d.jpg) no-repeat;}
#Wrapper01 .BtGamestart {background:url(start_n.jpg) no-repeat;}
#Wrapper02 .BtGamestart {background:url(start_n.jpg) no-repeat;}
#Wrapper01 .BtGamestart a:hover{background:url(start_o.jpg) no-repeat;}
#Wrapper02 .BtGamestart a:hover{background:url(start_o.jpg) no-repeat;}
.BtExit {position:absolute; left:585; top:115; width:93px; height:28px; z-index:2}
.BtExit a {width:93px;height:28px;text-indent:-9000px;display:block;}
.BtExit a:hover {width:93;height:28;text-indent:-9000px;display:block;}
#Wrapper01 .BtExit {background:url(exit_n.jpg) no-repeat;}
#Wrapper02 .BtExit {background:url(exit_o.jpg) no-repeat;}
.UserInfo{position:absolute;left:427px;top:63px;color:#c9f3ff;font:14px;}
</style>
<script language="javascript">
<!--
var CanStart;
function SetNoticeUrl(url)
{
document.getElementById("NoticeFrame").src = url;
//alert(NoticeFrame.src);
}
function _onGameStart()
{
}
function get_CanStart()
{
return CanStart
}
function _null() {}
function OnAllDone()
{
var e = document.getElementById("startGame");
if(agreement.checked)
{
e.parentElement.className = "BtGamestart";
e.onclick = _onGameStart;
}
CanStart=1
SetCurrentProgress(1, 1);
SetTotalProgress(1, 1);
lblStatus.innerText = "";
InstallTxtPer.innerText = "更新完成,请点选开始游戏按扭";
//external.FlashWindow();
}
function checkAgreement()
{
if(agreement.checked)
{
var barTotal = document.getElementById("barTotal");
if(barTotal.width == 671)
{
var sb = document.getElementById("startGame");
sb.parentElement.className = "BtGamestart";
sb.onclick = _onGameStart;
}
}
else
{
var e = document.getElementById("startGame");
e.parentElement.className = "BtGamestart_d";
e.onclick = _null;
}
}
function SetTotalProgress(cur, total)
{
//lblTotal.innerText = cur.toString() + "/" + total.toString();
var lblTotal = document.getElementById("InstallTxtPer");
if (lblTotal != null)
{
lblTotal.innerText = "进度 " + Math.round((cur/total * 100.0)) + "%";
}
var barTotal = document.getElementById("barTotal");
if (barTotal != null)
{
barTotal.width = (cur/total) * barTotal.parentElement.offsetWidth;
}
}
function SetCurrentProgress(cur, total)
{
barCurrent.width = cur/total * barCurrent.parentElement.offsetWidth;
}
var g_output = "";
function Output(s)
{
g_output += s + "<br>";
}
function SetStatusText(name)
{g_output
//lblCurrent.innerText = name + " (" + cur + "/" + total + ")";
lblStatus.innerText = name;
//barCurrent.width = 0;
}
// -->
</script>
</head>
<body oncontextmenu="return false" onselectstart="return false;">
<script language="javascript">
<!--
var tag = '<div id="{0}">'
document.write(tag.replace('{0}', Math.random() < 0.5 ? 'Wrapper01' : 'Wrapper02'));
//-->
</script>
<div id="Install">
<div id="BarTotalDiv"><img id="barTotal" src="bar_total.gif" width="0%" height="6"></div>
<div id="BarNowDiv"><img id="barCurrent" src="bar_now.gif" width="0%" height="6"></div>
<div id="InstallTxt"><span id="InstallTxtPer">进度 0%</span><span id="lblStatus">Now Downloading...</span></div>
</div>
<span class="BtClose"><a href="javascript:_null()" id="closeGame">Close</a></span>
<span class="BtGamestart_d"><a href="javascript:_null()" id="startGame">Start</a></span>
<span class="BtExit"><a href="javascript:_null()" id="exitGame">Exit</a></span>
<span class="UserInfo"><a href="http://tw.beanfun.com/cso/support_06.aspx" target="_blank">[游戏服务合约书]</a></span>
<span style="position:absolute;right:30px;top:63px;color:#ffffff;font:14px;"><input name="agreement" type="checkbox" value="0" onClick="checkAgreement()">我已阅读并同意</span>
<span style="position:absolute;right:30px;top:83px;color:#b4b4b4;">我已阅读并同意游戏服务合约书</span>
<iframe src="http://tw.beanfun.com/cso/www/game_login.aspx" id="NoticeFrame" frameborder="0" width="100%" height="100%" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="true"></iframe>
</body>
</html>
可以下载看看,我已经把 进度列 等全部模拟完成
接下来你只要去修改 操控进度列的函数(在程式中),就能轻易达到更新的效果
不过这全拜 CSO 更新器是以 "HTML" 方式来执行所赐,所以在程式中,其实只用了一个 WebBrowser 而已
那些 Image 全部都是在 WebBrowser 里面,因此位置、大小、触发程序 都已经被 HTML码设定好了
VB程式中只需要 拦截触发,呼叫函数,还有判断函数的值就行了,那些进度列的样式都已经被设定好了,真方便
现在已经几乎与 CSO 更新器一模一样,只差进度列更新时不太一样
CSOLauncher_Simulation 程式下载: