• 按键公众号 :
按键精灵电脑版
立即下载

软件版本:2014.06
软件大小:22.9M
更新时间:2021-12-03

按键精灵安卓版
立即下载

软件版本:3.7.2
软件大小:46.2M
更新时间:2023-05-10

按键精灵iOS版
立即下载

软件版本:1.8.0
软件大小:29.2M
更新时间:2023-03-21

按键手机助手
立即下载

软件版本:3.8.0
软件大小:262M
更新时间:2023-05-30

快捷导航

登录 后使用快捷导航
没有帐号? 注册

发新话题 回复该主题

[笨蛋熊] 【按键X】鼠标拖动定位窗口,附送窗口SPY工具源代码 [复制链接]

1#
我们在开发游戏脚本的时候,经常需要管理想要操作的窗口,一般来说,这种功能都是使用窗口查找和遍历来实现的。
但偶尔也有需求是希望由用户来自行选择想要操作的窗口,这种功能用按键实现起来难度不小,今天就给大家展示一下怎么做。

基于按键X的星月扩展库提供了一些补充插件,来强化按键精灵的功能,对星月扩展库感兴趣的小伙伴可以看原贴:http://bbs.anjian.com/showtopic-664233-1.aspx



最终实现效果如下:






通过查询星月扩展库帮助文档,我们可以知道使用 [url=]Spy_Create[/url] 命令可以创建一个 Spy 控件,这个控件的功能就是提供一个拖动定位窗口的能力。



它有5个参数,第一个参数是希望把这个控件创建到什么控件里面,可以指定为窗口句柄,或者其他控件的句柄(有时候会希望把Spy控件创建到选项卡控件里面)

第二个参数和第三个参数是控件创建的坐标,因为不是所见即所得设计的,计算坐标很麻烦,这里教大家一种简单的方法,创建一个标签控件,然后把Spy控件创建到标签控件里面,然后这两个参数设置为0,这样拖动标签控件的位置,就可以给Spy控件定位了。

第四个参数和第五个参数用于接收事件的标签控件,Spy控件操作的时候,会通知按键精灵窗口,通过设置标签控件的值并点击的方式,这样就可以和按键精灵X实现数据交换了,第四个参数负责鼠标拖动后松开的事件(最终定位的窗口);第五个参数负责鼠标移动时窗口变化的事件(划过窗口)。

需要注意的是,这里的 Spy 控件支持识别子窗口,所以鼠标划过一个按钮、输入框之类的,都会接收到事件,然后鼠标松开最终定位的窗口和划过的窗口,得到的都是子窗口句柄,如果要定位子窗口,这个句柄直接用就可以了,如果要定位游戏主窗口,可以用 [url=]Window_GetRoot[/url] 命令获取顶级窗口句柄。



接下来我们做一个最小范例,来体会一下这个控件的用法:



第一步:创建4个标签控件,第二个和第三个标签控件的显示属性设置为否,第一个标签控件的标题清空,注意不要太小,不然放不下Spy控件,第四个标签控件不用特别改,弄大一点方便显示内容,如下图所示





第二步:添加星月扩展库的xyapi定义:
  1. // Spy控件库
  2. Declare Function Spy_Create Lib "xyapi.dll" (ByVal hParent As Long, ByVal x As Long, ByVal y As Long, ByVal Event_Over As Long, ByVal Event_Move As Long) As Long
  3. Declare Sub Spy_Destroy Lib "xyapi.dll" (ByVal hSpy As Long)

  4. // 窗口库
  5. Declare Function Window_GetCaption Lib "xyapi.dll" (ByVal hWnd As Long) As String
  6. Declare Function Window_GetClassName Lib "xyapi.dll" (ByVal hWnd As Long) As String
  7. Declare Function Window_GetRoot Lib "xyapi.dll" (ByVal hWnd As Long) As Long
  8. Declare Function Window_E2H Lib "xyapi.dll" (ByVal Parent As Long, ByVal ClassName As String, ByVal Eid As Long) As Long
  9. Declare Function Window_H2E Lib "xyapi.dll" (ByVal Parent As Long, ByVal ClassName As String, ByVal Eid As Long) As Long
  10. Declare Function Window_Scan Lib "xyapi.dll" (ByVal Mask As Long, ByVal ClassName As String, ByVal Caption As String, ByVal Style As Long, ByVal ExStyle As Long, ByVal PID As Long, ByVal Thread As Long, ByVal bk As Any, ByVal Param As Long) As Long
  11. Declare Function Window_List Lib "xyapi.dll" (ByVal Mask As Long, ByVal ClassName As String, ByVal Caption As String, ByVal Style As Long, ByVal ExStyle As Long, ByVal PID As Long, ByVal Thread As Long) As String
  12. Declare Function Window_Find Lib "xyapi.dll" (ByVal Mask As Long, ByVal ClassName As String, ByVal Caption As String, ByVal Style As Long, ByVal ExStyle As Long, ByVal PID As Long, ByVal Thread As Long) As Long
  13. Declare Function Window_ScanChild Lib "xyapi.dll" (ByVal Parent As Long, ByVal Mask As Long, ByVal ClassName As String, ByVal Caption As String, ByVal Style As Long, ByVal ExStyle As Long, ByVal PID As Long, ByVal Thread As Long, ByVal bk As Any, ByVal Param As Long) As Long
  14. Declare Function Window_ListChild Lib "xyapi.dll" (ByVal Parent As Long, ByVal Mask As Long, ByVal ClassName As String, ByVal Caption As String, ByVal Style As Long, ByVal ExStyle As Long, ByVal PID As Long, ByVal Thread As Long) As String
  15. Declare Function Window_FindChild Lib "xyapi.dll" (ByVal Parent As Long, ByVal Mask As Long, ByVal ClassName As String, ByVal Caption As String, ByVal Style As Long, ByVal ExStyle As Long, ByVal PID As Long, ByVal Thread As Long) As Long
  16. Declare Function Window_GetPID Lib "xyapi.dll" (ByVal hWin As Long) As Long
  17. Declare Function Window_GetTID Lib "xyapi.dll" (ByVal hWin As Long) As Long
  18. Declare Function Window_GetPath Lib "xyapi.dll" (ByVal hWin As Long) As String
  19. Declare Function Window_FormPoint Lib "xyapi.dll" (ByVal x As Long, ByVal y As Long) As Long
  20. Declare Function Window_FormMouse Lib "xyapi.dll" () As Long
  21. Declare Function Window_ConvS2C Lib "xyapi.dll" (ByVal hWin As Long, ByVal x As Long, ByVal y As Long) As Long
  22. Declare Function Window_ConvC2S Lib "xyapi.dll" (ByVal hWin As Long, ByVal x As Long, ByVal y As Long) As Long
复制代码
第三步:添加 xyapi.dll 到附件目录。



第四步:在窗口的载入完成事件和销毁事件里写入代码,创建和销毁 Spy 控件:
  1. Global SpyHandle
  2. Event Form1.LoadOver
  3.     SpyHandle = Spy_Create(Form1.Label1.hWnd, 0, 0, Form1.Label2.hWnd, Form1.Label3.hWnd)
  4.     TracePrint SpyHandle
  5. End Event
  6. Event Form1.UnLoad
  7.     Spy_Destroy(SpyHandle)
  8. End Event
复制代码
这里 Spy_Create 命令用于创建 Spy控件,Spy_Destroy用于销毁 Spy控件,全局变量 SpyHandle 用于保存 Spy控件的句柄。



运行脚本,展示效果如下:



现在 Spy 控件可以操作和拖动了,但是不会有任何反应,因为我们还没有处理它的事件。



第五步:完善 Spy 控件的事件,这里我们用比较简单的方法直接把数据输出出来,实际上能做的还有很多,比如实现一个 Spy 工具,后面我们会继续讲:
  1. // Event_Spy_Over
  2. Event Form1.Label2.Click
  3.     Dim G_Spy_hCon = CLng(Form1.Label2.Caption)
  4.     Dim G_Spy_hWin = Window_GetRoot(G_Spy_hCon)
  5.     MsgBox "窗口句柄:" & G_Spy_hWin & "\r\n" & "控件句柄:" & G_Spy_hCon
  6. End Event

  7. // Event_Spy_Move
  8. Event Form1.Label3.Click
  9.     Dim hWnd = CLng(Form1.Label3.Caption)
  10.     Form1.Label4.Caption = "窗口句柄:" & hWnd
  11. End Event
复制代码
再次运行,拖动 Spy 控件,现在可以响应事件了。





接下来直接上 Spy 控件的源代码:
  1. // Spy控件库
  2. Declare Function Spy_Create Lib "xyapi.dll" (ByVal hParent As Long, ByVal x As Long, ByVal y As Long, ByVal Event_Over As Long, ByVal Event_Move As Long) As Long
  3. Declare Sub Spy_Destroy Lib "xyapi.dll" (ByVal hSpy As Long)

  4. // 剪贴板库
  5. Declare Function Clip_GetText Lib "xyapi.dll" () As String
  6. Declare Function Clip_SetText Lib "xyapi.dll" (ByVal sText As String, ByVal uSize As Long) As Long

  7. // 窗口库
  8. Declare Function Window_GetCaption Lib "xyapi.dll" (ByVal hWnd As Long) As String
  9. Declare Function Window_GetClassName Lib "xyapi.dll" (ByVal hWnd As Long) As String
  10. Declare Function Window_GetRoot Lib "xyapi.dll" (ByVal hWnd As Long) As Long
  11. Declare Function Window_E2H Lib "xyapi.dll" (ByVal Parent As Long, ByVal ClassName As String, ByVal Eid As Long) As Long
  12. Declare Function Window_H2E Lib "xyapi.dll" (ByVal Parent As Long, ByVal ClassName As String, ByVal Eid As Long) As Long
  13. Declare Function Window_Scan Lib "xyapi.dll" (ByVal Mask As Long, ByVal ClassName As String, ByVal Caption As String, ByVal Style As Long, ByVal ExStyle As Long, ByVal PID As Long, ByVal Thread As Long, ByVal bk As Any, ByVal Param As Long) As Long
  14. Declare Function Window_List Lib "xyapi.dll" (ByVal Mask As Long, ByVal ClassName As String, ByVal Caption As String, ByVal Style As Long, ByVal ExStyle As Long, ByVal PID As Long, ByVal Thread As Long) As String
  15. Declare Function Window_Find Lib "xyapi.dll" (ByVal Mask As Long, ByVal ClassName As String, ByVal Caption As String, ByVal Style As Long, ByVal ExStyle As Long, ByVal PID As Long, ByVal Thread As Long) As Long
  16. Declare Function Window_ScanChild Lib "xyapi.dll" (ByVal Parent As Long, ByVal Mask As Long, ByVal ClassName As String, ByVal Caption As String, ByVal Style As Long, ByVal ExStyle As Long, ByVal PID As Long, ByVal Thread As Long, ByVal bk As Any, ByVal Param As Long) As Long
  17. Declare Function Window_ListChild Lib "xyapi.dll" (ByVal Parent As Long, ByVal Mask As Long, ByVal ClassName As String, ByVal Caption As String, ByVal Style As Long, ByVal ExStyle As Long, ByVal PID As Long, ByVal Thread As Long) As String
  18. Declare Function Window_FindChild Lib "xyapi.dll" (ByVal Parent As Long, ByVal Mask As Long, ByVal ClassName As String, ByVal Caption As String, ByVal Style As Long, ByVal ExStyle As Long, ByVal PID As Long, ByVal Thread As Long) As Long
  19. Declare Function Window_GetPID Lib "xyapi.dll" (ByVal hWin As Long) As Long
  20. Declare Function Window_GetTID Lib "xyapi.dll" (ByVal hWin As Long) As Long
  21. Declare Function Window_GetPath Lib "xyapi.dll" (ByVal hWin As Long) As String
  22. Declare Function Window_FormPoint Lib "xyapi.dll" (ByVal x As Long, ByVal y As Long) As Long
  23. Declare Function Window_FormMouse Lib "xyapi.dll" () As Long
  24. Declare Function Window_ConvS2C Lib "xyapi.dll" (ByVal hWin As Long, ByVal x As Long, ByVal y As Long) As Long
  25. Declare Function Window_ConvC2S Lib "xyapi.dll" (ByVal hWin As Long, ByVal x As Long, ByVal y As Long) As Long





  26. // 窗口加载
  27. Global SpyHandle
  28. Event Form1.LoadOver
  29.     Form1.Grid1.SetItemText(1 , 0, "根窗口句柄")
  30.     Form1.Grid1.SetItemText(2 , 0, "根窗口标题")
  31.     Form1.Grid1.SetItemText(3 , 0, "根窗口类名")
  32.     Form1.Grid1.SetItemText(4 , 0, "根窗口类枚举")
  33.     Form1.Grid1.SetItemText(5 , 0, "父窗口句柄")
  34.     Form1.Grid1.SetItemText(6 , 0, "父窗口标题")
  35.     Form1.Grid1.SetItemText(7 , 0, "父窗口类名")
  36.     Form1.Grid1.SetItemText(8 , 0, "父窗口类枚举")
  37.     Form1.Grid1.SetItemText(9 , 0, "所属进程 PID")
  38.     Form1.Grid1.SetItemText(10, 0, "所属线程 TID")
  39.     SpyHandle = Spy_Create(Form1.Label3.hWnd, 0, 0, Form1.Label1.hWnd, Form1.Label2.hWnd)
  40.     TracePrint SpyHandle
  41. End Event
  42. Event Form1.UnLoad
  43.     Spy_Destroy(SpyHandle)
  44. End Event

  45. // Event_Spy_Over
  46. Declare Function GetParent Lib "user32" Alias "GetParent" (ByVal hwnd As Long) As Long
  47. Global G_Spy_hCon, G_Spy_hWin
  48. Event Form1.Label1.Click
  49.     G_Spy_hCon = CLng(Form1.Label1.Caption)
  50.     G_Spy_hWin = Window_GetRoot(G_Spy_hCon)
  51.     Dim hParent = GetParent(G_Spy_hCon)
  52.     Dim sClassName = Window_GetClassName(G_Spy_hCon)
  53.     Form1.Grid1.SetItemText(1 , 1, G_Spy_hWin)
  54.     Form1.Grid1.SetItemText(2 , 1, Window_GetCaption(G_Spy_hWin))
  55.     Form1.Grid1.SetItemText(3 , 1, Window_GetClassName(G_Spy_hWin))
  56.     Form1.Grid1.SetItemText(4 , 1, Window_H2E(G_Spy_hWin, sClassName, G_Spy_hCon))
  57.     Form1.Grid1.SetItemText(5 , 1, hParent)
  58.     Form1.Grid1.SetItemText(6 , 1, Window_GetCaption(hParent))
  59.     Form1.Grid1.SetItemText(7 , 1, Window_GetClassName(hParent))
  60.     Form1.Grid1.SetItemText(8 , 1, Window_H2E(hParent, sClassName, G_Spy_hCon))
  61.     Form1.Grid1.SetItemText(9 , 1, Window_GetPID(G_Spy_hCon))
  62.     Form1.Grid1.SetItemText(10, 1, Window_GetTID(G_Spy_hCon))
  63.     Form1.InputBox5.Text = """" & Window_GetClassName(G_Spy_hWin) & """"
  64.     Form1.InputBox6.Text = """" & Window_GetCaption(G_Spy_hWin) & """"
  65.     Form1.InputBox15.Text = """" & Window_GetClassName(G_Spy_hCon) & """"
  66.     Form1.InputBox16.Text = Window_H2E(G_Spy_hWin, sClassName, G_Spy_hCon)
  67.     Form1.InputBox11.Text = """" & Window_GetClassName(G_Spy_hCon) & """"
  68.     Form1.InputBox12.Text = """" & Window_GetCaption(G_Spy_hCon) & """"
  69. End Event

  70. // Event_Spy_Move
  71. Event Form1.Label2.Click
  72.     Dim hWnd = CLng(Form1.Label2.Caption)
  73.     Form1.InputBox1.Text = hWnd
  74.     Form1.InputBox2.Text = Window_GetCaption(hWnd)
  75.     Form1.InputBox3.Text = Window_GetClassName(hWnd)
  76.     Form1.InputBox4.Text = Window_GetPath(hWnd)
  77. End Event

  78. // 重新获取控件枚举值
  79. Event Form1.Button1.Click
  80.     Form1.InputBox16.Text = Window_H2E(G_Spy_hWin, Form1.InputBox15.Text, G_Spy_hCon)
  81. End Event

  82. // 生成代码
  83. Event Form1.Button2.Click
  84.     // 生成查找窗口代码
  85.     Dim Script = "Dim hWin = Window_Find("
  86.     Dim Flag = iif(CBool(Form1.CheckBox1.Value), CLng(Form1.ComboBox1.GetItemData(Form1.ComboBox1.ListIndex)), 0)
  87.     Flag = Flag + iif(CBool(Form1.CheckBox2.Value), CLng(Form1.ComboBox2.GetItemData(Form1.ComboBox2.ListIndex)), 0)
  88.     Flag = Flag + iif(CBool(Form1.CheckBox3.Value), CLng(Form1.ComboBox3.GetItemData(Form1.ComboBox3.ListIndex)), 0)
  89.     Flag = Flag + iif(CBool(Form1.CheckBox4.Value), CLng(Form1.ComboBox4.GetItemData(Form1.ComboBox4.ListIndex)), 0)
  90.     Flag = Flag + iif(CBool(Form1.CheckBox5.Value), 1, 0)
  91.     Flag = Flag + iif(CBool(Form1.CheckBox6.Value), 2, 0)
  92.     Script = Script & Flag & ", "
  93.     Script = Script & iif(CBool(Form1.CheckBox1.Value), Form1.InputBox5.Text, """""") & ", "
  94.     Script = Script & iif(CBool(Form1.CheckBox2.Value), Form1.InputBox6.Text, """""") & ", "
  95.     Script = Script & iif(CBool(Form1.CheckBox3.Value), Form1.InputBox7.Text, "0") & ", "
  96.     Script = Script & iif(CBool(Form1.CheckBox4.Value), Form1.InputBox8.Text, "0") & ", "
  97.     Script = Script & iif(CBool(Form1.CheckBox5.Value), Form1.InputBox9.Text, "0") & ", "
  98.     Script = Script & iif(CBool(Form1.CheckBox6.Value), Form1.InputBox10.Text, "0") & ")\r\n"
  99.     // 生成查找控件代码
  100.     If Form1.CheckBox11.Value = 0 Then
  101.         Select Case Form1.TabControl2.Tab
  102.             Case 0
  103.                 Script = Script & "Dim hCon = Window_E2H(hWin, "
  104.                 Script = Script & Form1.InputBox15.Text & ", "
  105.                 Script = Script & Form1.InputBox16.Text & ")\r\n"
  106.             Case 1
  107.                 Script = Script & "Dim hCon = Window_FindChild(hWin, "
  108.                 Flag = iif(CBool(Form1.CheckBox7.Value), CLng(Form1.ComboBox5.GetItemData(Form1.ComboBox5.ListIndex)), 0)
  109.                 Flag = Flag + iif(CBool(Form1.CheckBox8.Value), CLng(Form1.ComboBox6.GetItemData(Form1.ComboBox6.ListIndex)), 0)
  110.                 Flag = Flag + iif(CBool(Form1.CheckBox9.Value), CLng(Form1.ComboBox7.GetItemData(Form1.ComboBox7.ListIndex)), 0)
  111.                 Flag = Flag + iif(CBool(Form1.CheckBox10.Value), CLng(Form1.ComboBox8.GetItemData(Form1.ComboBox8.ListIndex)), 0)
  112.                 Script = Script & Flag & ", "
  113.                 Script = Script & iif(CBool(Form1.CheckBox7.Value), Form1.InputBox11.Text, """""") & ", "
  114.                 Script = Script & iif(CBool(Form1.CheckBox8.Value), Form1.InputBox12.Text, """""") & ", "
  115.                 Script = Script & iif(CBool(Form1.CheckBox9.Value), Form1.InputBox13.Text, "0") & ", "
  116.                 Script = Script & iif(CBool(Form1.CheckBox10.Value), Form1.InputBox14.Text, "0") & ", 0, 0)\r\n"
  117.         End Select
  118.     End If
  119.     Form1.InputBox17.Text = Script
  120. End Event

  121. // 复制代码
  122. Event Form1.Button3.Click
  123.     Clip_SetText(Form1.InputBox17.Text, 0)
  124.     MsgBox "代码复制成功!"
  125. End Event
复制代码
其实大同小异,无非是控件用的多了一点,获取的信息比刚才的最小范例丰富了一点,各位可以对照 xyapi 的文档查看每个函数的功能,整体上这段代码还是很简单的,入门即可实现这些效果。



Spy工具成品小精灵下载:

spy.zip (, 下载次数:300)




Spy控件最小演示工程下载:

Spymini.zip (, 下载次数:195)




Spy工具完整源代码下载:

spytool.zip (, 下载次数:268)

2#

请问按键x哪里下载?
谢谢

3#


发新话题 回复该主题