1. Option Explicit
' funciones Api
Private Declare Function SendMessageAsLong Lib "user32" _
Alias "SendMessageA" ( _
ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long _
) As Long
Private Declare Function SendMessageAsString Lib "user32" _
Alias "SendMessageA" ( _
ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As String _
) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
' Constante para los mensajes
' Recupera en un buffer el texto de la linea
Private Const EM_GETLINE As Long = &HC4
' Recupera la cantidad de lineas
Private Const EM_GETLINECOUNT As Long = &HBA
' Hwnd de la ventana
Dim ret As Long
Public Function Obtener_Texto(Hwnd_Ventana As Long) As String
Dim i As Long
Dim n As Long
Dim temp As String
' Cantidad de lineas
n = SendMessageAsLong(Hwnd_Ventana, EM_GETLINECOUNT, 0, 0)
' Recorre todas las lineas y obtiene el texto de _
la linea actual con la función Leer_Linea
For i = 1 To n
temp = temp & Leer_Linea(Hwnd_Ventana, i - 1) & vbCrLf
Next
2. Obtener_Texto = temp
End Function
' Retorna el texto de la linea indicada
Private Function Leer_Linea(Hwnd_Ventana As Long, Numero_Linea As Long) As String
Const MAX_CHAR_PER_LINE As Long = 80
Dim Lo As Integer
Dim Hi As Integer
Dim ret As Long
Dim Buffer As String
Lo = MAX_CHAR_PER_LINE And (255)
Hi = Int(MAX_CHAR_PER_LINE / 256)
Buffer = Chr$(Lo) + Chr$(Hi) + Space$(MAX_CHAR_PER_LINE - 2)
' Recupera el texto en el Buffer, de la linea indicada en Numero_Linea.
ret = SendMessageAsString(Hwnd_Ventana, EM_GETLINE, Numero_Linea, Buffer)
' Retorna la linea
Leer_Linea = Left$(Buffer, ret)
End Function
' Botón para recuperar el hwnd
Private Sub Command1_Click()
' Recupera el Hwnd del bloc de notas ( Handle del Edit )
ret = FindWindowEx(FindWindow(vbNullString, "Sin título - bloc de notas"), _
0, "Edit", vbNullString)
If ret = 0 Then
MsgBox "Error. El Hwnd es 0 ", vbCritical
Else
MsgBox "Hwnd del Edit del Notepad es: " & ret, vbInformation
Command2.Enabled = True
Command1.Enabled = False
End If
End Sub
' Botón que recupera el texto
Private Sub Command2_Click()
If ret = 0 Then
MsgBox "error. El Hwnd es 0", vbCritical
Else
' Muestra el texto del otepad en el textbox
Text1 = Obtener_Texto(ret)
End If
End Sub
Private Sub Form_Load()
3. Command1.Caption = " Obtener Hwnd del bloc de notas "
Command2.Caption = " Obtener el Texto "
Command2.Enabled = False
MsgBox " 1 - Abrir el Bloc de notas. " & vbNewLine & _
" 2 - Dejarle el caption -->> Sin Título - Bloc de notas " & vbNewLine &
_
" 3 - Escribir algún texto y luego presionar el botón [ Obtener Hwnd del
bloc de notas ]" & vbNewLine & _
" 4 - Presionar el botón [ Obtener texto ]", vbInformation
End Sub