elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Únete al Grupo Steam elhacker.NET


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP)
| | | |-+  Programación Visual Basic (Moderadores: LeandroA, seba123neo)
| | | | |-+  Spy++
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Spy++  (Leído 2,019 veces)
Piratex

Desconectado Desconectado

Mensajes: 3


Ver Perfil
Spy++
« en: 22 Marzo 2015, 12:37 pm »

Hola a todos.

Si tengo una aplicación como por ejemplo la calculadora donde todos los botones son la clase "Button", como puedo recorrer todos los botones y saber en cada momento en el botón que estoy.

Muchas gracias por anticipado.


En línea

1010.c0der.1010

Desconectado Desconectado

Mensajes: 2


Ver Perfil
Re: Spy++
« Respuesta #1 en: 23 Marzo 2015, 04:48 am »

Hola a todos.

Si tengo una aplicación como por ejemplo la calculadora donde todos los botones son la clase "Button", como puedo recorrer todos los botones y saber en cada momento en el botón que estoy.

Muchas gracias por anticipado.

No te comprendo pero puedes crear un array de botones y asi recorrer todos, a medida que vas agregando los botones te puede ir creando button(1), button(2), button(3), etc., asi mas facilmente puedes identificarlo y con un bucle puedes recorrer todos los botones.

Tambien puedes usar el evento focus para saber en que botón estas.

Código
  1. Private Sub Button1_GotFocus()
  2.    Label1 = "El foco lo tiene: " & Button1.Caption
  3. End Sub
  4.  


En línea

okik


Desconectado Desconectado

Mensajes: 462


Ver Perfil
Re: Spy++
« Respuesta #2 en: 23 Marzo 2015, 14:44 pm »

Hola

Por decucción, como has titulado el asunto como "Spy++", supongo que te refieres a la Calculadora de Windows accediendo a ella desde tu apliación.  En Spy++ al hacer clic en "+" correspondiente a la Calculadora se despliega un árbol donde aparecen todos los handles y textos de cada botón. De modo que cada botón tiene un Handle (un número aleatório identificativo) y una etiqueta o texto "1","2", "Bin","hyp", "=", etc.

Para recorrer los botones primero se obtiene el handle de la calculadora con la declaración API FindWindow, después el handle del PRIMER  botón con FindWindowEx, y para ir recorriendo los siguientes botones se usa GetWindow y el comando GW_HWNDNEXT.

Para saber si estás en un determinado botón obtienes su etiquieta o texto con GetWindowText, así si estás en "5" obtienes "5".



Aquí te dejo un plantilla de ejemplo,  que recorre todos los botones e introduce el nombre de cada uno en un ListBox. Además, al pasar el cursor por los botones de la calculadora muestra en un Label el botón donde se encuentra el cursor.

Necesitas un Botón, un Label , un ListBox y un control Timer


En un Form:
EN VB6
Código
  1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
  2. (ByVal hwnd As Long, _
  3. ByVal wMsg As Long, _
  4. ByVal wParam As Long, _
  5. lParam As Any) As Long
  6. Private Const WM_SYSCOMMAND = &H112
  7. Private Const SC_CLOSE = &HF060&
  8.  
  9. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
  10. (ByVal lpClassName As String, _
  11. ByVal lpWindowName As String) As Long
  12.  
  13. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
  14. (ByVal hWnd1 As Long, _
  15. ByVal hWnd2 As Long, _
  16. ByVal lpsz1 As String, _
  17. ByVal lpsz2 As String) As Long
  18.  
  19. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
  20. (ByVal hwnd As Long, _
  21. ByVal lpString As String, _
  22. ByVal cch As Long) As Long
  23.  
  24. Private Type POINTAPI
  25. x As Long
  26. y As Long
  27. End Type
  28.  
  29. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
  30. Private Declare Function WindowFromPointXY Lib "user32" Alias "WindowFromPoint" _
  31. (ByVal xPoint As Long, ByVal yPoint As Long) As Long
  32.  
  33. Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
  34.  
  35. 'Para recorrer todos los botones
  36. Private Declare Function GetWindow Lib "user32" _
  37. (ByVal hwnd As Long, _
  38. ByVal wCmd As Long) As Long
  39. Private Const GW_CHILD = 5
  40. Private Const GW_HWNDFIRST = 0
  41. Private Const GW_HWNDLAST = 1
  42. Private Const GW_HWNDNEXT = 2
  43. Private Const GW_HWNDPREV = 3
  44. Private Const GW_MAX = 5
  45. Private Const GW_OWNER = 4
  46.  
  47. Private Sub Command1_Click()
  48. Shell "Calc", vbNormalFocus
  49. Timer1.Enabled = True
  50. RecorrerBotones
  51. End Sub
  52.  
  53. Public Function TextoBoton() As String
  54. Dim pt32 As POINTAPI
  55. Dim ptx As Long
  56. Dim pty As Long
  57. Dim x As Long
  58. Dim hw As Long
  59. Dim hWndParent As Long
  60. Dim TextWindow As String * 100
  61. Call GetCursorPos(pt32)                ' obtiene la posición del cursor
  62. ptx = pt32.x
  63. pty = pt32.y
  64.    hw = WindowFromPointXY(ptx, pty) ' Obtiene el handle debajo del cursor
  65.    hWndParent = GetParent(hw) ' Consigue el handle padre de una ventana
  66.    x = GetWindowText(hWndParent, TextWindow, 100) 'Obtiene el texto de la ventana
  67. If Left(TextWindow, x) = "Calculadora" Then
  68.    x = GetWindowText(hw, TextWindow, 100) 'Obtiene el texto de un botón
  69.    TextoBoton = Left(TextWindow, x)
  70. End If
  71.  
  72. End Function
  73.  
  74. Private Sub Form_Load()
  75. Timer1.Enabled = False
  76. Command1.Caption = "Abrir calculadora"
  77. End Sub
  78.  
  79. Private Sub Form_Unload(Cancel As Integer)
  80. Dim hw As Long
  81. hw = FindWindow("SciCalc", vbNullString)
  82. SendMessage hw, WM_SYSCOMMAND, SC_CLOSE, &H0 'Cierra la calculadora
  83. End
  84. End Sub
  85.  
  86. Private Sub Timer1_Timer()
  87. 'Para obtener el texto del botón donde se encuentra el cursor
  88. Label1.Caption = TextoBoton
  89.  
  90. Me.Caption = TextoBoton
  91.  
  92. End Sub
  93.  
  94. Public Sub RecorrerBotones()
  95. Dim hw As Long
  96. Dim x As Long
  97. Dim TextWindow As String * 100
  98. hw = FindWindow("SciCalc", vbNullString)
  99. hw = FindWindowEx(hw, &H0, "Button", vbNullString) 'obtiene el primer handle de un botón
  100.    Do While Left(TextWindow, x) <> "C "
  101.        hw = GetWindow(hw, GW_HWNDNEXT) '<--- Esto permite obtener y pasar al siguiente Handle de la lista
  102.        x = GetWindowText(hw, TextWindow, 100) 'Obtiene el texto de un botón
  103.        If x > 0 Then
  104.        List1.AddItem Left(TextWindow, x)
  105.        End If
  106.        DoEvents
  107.    Loop
  108. End Sub



EN VB.NET
Código
  1. Imports VB = Microsoft.VisualBasic
  2. Public Class Form1
  3.    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
  4.        (ByVal hWnd As Integer, _
  5.         ByVal wMsg As Integer, _
  6.         ByVal wParam As Integer, _
  7.         ByRef lParam As Object) As Integer
  8.  
  9.    Private Const WM_SYSCOMMAND = &H112
  10.    Private Const SC_CLOSE = &HF060&
  11.  
  12.  
  13.    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
  14. (ByVal lpClassName As String, _
  15. ByVal lpWindowName As String) As Integer
  16.  
  17.    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
  18.    (ByVal hWnd1 As Integer, _
  19.    ByVal hWnd2 As Integer, _
  20.    ByVal lpsz1 As String, _
  21.    ByVal lpsz2 As String) As Integer
  22.  
  23.    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
  24.    (ByVal hwnd As Integer, _
  25.    ByVal lpString As String, _
  26.    ByVal cch As Integer) As Integer
  27.  
  28.    Structure POINTAPI
  29.        Dim X As Integer
  30.        Dim Y As Integer
  31.    End Structure
  32.  
  33.    Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As POINTAPI) As Integer
  34.    Private Declare Function WindowFromPointXY Lib "user32" Alias "WindowFromPoint" _
  35.    (ByVal xPoint As Integer, ByVal yPoint As Integer) As Integer
  36.  
  37.    Private Declare Function GetParent Lib "user32" (ByVal hwnd As Integer) As Integer
  38.    'Para recorrer todos los botones
  39.    Private Declare Function GetWindow Lib "user32" _
  40.    (ByVal hwnd As Integer, _
  41.    ByVal wCmd As Integer) As Integer
  42.    Private Const GW_CHILD = 5
  43.    Private Const GW_HWNDFIRST = 0
  44.    Private Const GW_HWNDLAST = 1
  45.    Private Const GW_HWNDNEXT = 2
  46.    Private Const GW_HWNDPREV = 3
  47.    Private Const GW_MAX = 5
  48.    Private Const GW_OWNER = 4
  49.  
  50.  
  51.    Public Function TextoBoton() As String
  52.        Dim pt32 As POINTAPI
  53.        Dim ptx As Long
  54.        Dim pty As Long
  55.        Dim x As Long
  56.        Dim hw As Long
  57.        Dim hWndParent As Long
  58.        Dim TextWindow As String
  59.        TextoBoton = ""
  60.        TextWindow = Space(100)
  61.        Call GetCursorPos(pt32)                ' obtiene la posición del cursor
  62.        ptx = pt32.X
  63.        pty = pt32.Y
  64.        hw = WindowFromPointXY(ptx, pty) ' Obtiene el handle debajo del cursor
  65.        hWndParent = GetParent(hw) ' Consigue el handle padre de una ventana
  66.        x = GetWindowText(hWndParent, TextWindow, 100) 'Obtiene el texto de la ventana
  67.        If VB.Left(TextWindow, x) = "Calculadora" Then
  68.            x = GetWindowText(hw, TextWindow, 100) 'Obtiene el texto de un botón
  69.            TextoBoton = VB.Left(TextWindow, x)
  70.        End If
  71.        Return TextoBoton
  72.    End Function
  73.  
  74.  
  75.    Public Sub RecorrerBotones()
  76.        Dim hw As Long
  77.        Dim x As Long
  78.        Dim TextWindow As String
  79.        TextWindow = Space(100)
  80.        hw = FindWindow("SciCalc", vbNullString)
  81.        hw = FindWindowEx(hw, &H0, "Button", vbNullString) 'obtiene el primer handle de un botón
  82.        Do While VB.Left(TextWindow, x) <> "C "
  83.            hw = GetWindow(hw, GW_HWNDNEXT) '<--- Esto permite obtener y pasar al siguiente Handle de la lista
  84.            x = GetWindowText(hw, TextWindow, 100) 'Obtiene el texto de un botón
  85.            If x > 0 Then
  86.                ListBox1.Items.Add(VB.Left(TextWindow, x))
  87.            End If
  88.            Application.DoEvents()
  89.        Loop
  90.    End Sub
  91.  
  92.    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  93.        Dim hw As Long
  94.        hw = FindWindow("SciCalc", vbNullString)
  95.        SendMessage(hw, WM_SYSCOMMAND, SC_CLOSE, &H0) 'Cierra la calculadora
  96.        End
  97.    End Sub
  98.    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  99.        Timer1.Enabled = False
  100.        Timer1.Interval = 1
  101.        Button1.Text = "Abrir calculadora"
  102.    End Sub
  103.  
  104.    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  105.        Shell("Calc", vbNormalFocus)
  106.        Timer1.Enabled = True
  107.        RecorrerBotones()
  108.    End Sub
  109.  
  110.    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  111.        'Para obtener el texto del botón donde se encuentra el cursor
  112.        Label1.Text = TextoBoton()
  113.        Me.Text = TextoBoton()
  114.    End Sub
  115. End Class


« Última modificación: 23 Marzo 2015, 14:56 pm por okik » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines