因為只在這裡看過丁丁的程式碼,VB6的沒看過,所以分享一下。
這只是最基礎的讀取血量。元件:一個標籤跟一個計時器做成的讀取功能。
我的目標是僅瞭解VB6語法及函數的人就能看註釋知道為什麼寫這程式碼。
不過我幾乎十年沒碰VB又不是讀電腦的科系,如果註釋有錯誤請告知,謝謝。
當然,我知道有流程圖才容易懂軟體製作時的思考模式,
如果有人要討論我再做吧!沒有就當作一個紀錄留著就好。
P.S.沒有限制回文才看得到,請不要無意義的推文,有問題或建議再來討論。
否則就讓它慢慢沉下去就好,謝謝。
Option Explicit '強制宣告所有的變數。
'API宣告
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long '搜尋視窗
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal HWnd As Long, lpdwProcessId As Long) As Long '取得程序識別碼
Private Const PROCESS_ALL_ACCESS = &H1F0FFF '指定訪問程序的方法
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long '打開程序
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long '關閉程序
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long '讀取記憶體
Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long '寫入記憶體
'變數宣告
Dim hProcess As Long '程序控制碼
Dim BassAdd As Long '全局基址
Dim Base1C As Long '動態基址
Dim ChBase As Long '人物基址
Dim Hp As Long '當前血量
Dim HpMax As Long '最大血量
Private Sub Form_Load()
Call OpenCheck '確認遊戲
End Sub
Private Sub Timer1_Timer()
Call DataLoad '讀取遊戲資料
End Sub
Private Sub OpenCheck() '確認遊戲是否啟動
Dim HWnd As Long '視窗控制碼
HWnd = FindWindow(vbNullString, "完美世界") '取得視窗控制碼
If HWnd = 0 Then
MsgBox "遊戲未啟動"
Exit Sub
End If
Dim ProcessID As Long '程序識別碼
Call GetWindowThreadProcessId(HWnd, ProcessID) '取得程序識別碼
hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, ProcessID) '取得程序控制碼
If ProcessID = 0 Then
Debug.Print "無法取得程序控制碼"
Exit Sub
End If
End Sub
Private Sub DataLoad() '讀取遊戲資料
ReadProcessMemory hProcess, ByVal &HA571E0, BassAdd, 4, 0& '全局基址
ReadProcessMemory hProcess, ByVal BassAdd + &H1C, Base1C, 4, 0& '動態基址
ReadProcessMemory hProcess, ByVal Base1C + &H34, ChBase, 4, 0& '人物基址
ReadProcessMemory hProcess, ByVal ChBase + &H494, Hp, 4, 0& '當前血量
ReadProcessMemory hProcess, ByVal ChBase + &H4DC, HpMax, 4, 0& '最大血量
HpLabel.Caption = Hp & "/" & HpMax
End Sub |