'-----------------------------------------------------------
' Function : [GetTitleActiveApp]
' Type : [SNIPPET]
' Autor : [The Swash]
' DateTime : [31/03/2010]
'-----------------------------------------------------------
Option Explicit
'User32 Lib Apis
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
'SendMessage Constants
Const WM_GETTEXT = &HD
Const WM_GETTEXTLENGTH = &HE
Public Function GetTitleActiveApp() As String
Dim hRet As Long
Dim hSpace As Long
Dim sBuffer As String
hRet = GetForegroundWindow
If hRet <> 0 Then
hSpace = SendMessage(hRet, WM_GETTEXTLENGTH, 0&, 0&) + 1
If hSpace > 0 Then
sBuffer = Space$(hSpace)
Call SendMessage(hRet, WM_GETTEXT, hSpace, sBuffer)
End If
End If
GetTitleActiveApp = Trim(sBuffer)
End Function
Call:
MsgBox GetTitleActiveApp
Muy interesante... pero se podria acortar si damos un buffer grande y trabajamos con el valor que retorna WM_GETTEXT:
CitarThe return value is the number of TCHARs copied, not including the terminating null character.
Por lo tanto:
'USER32
Private Declare Function GetForegroundWindow Lib "USER32" () As Long
Private Declare Function SendMessageW Lib "USER32" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Long) As Long
Private Const WM_GETTEXT = &HD
Public Function GetWindowText(ByVal lhWnd As Long) As String
GetWindowText = Space$(512)
GetWindowText = Left$(GetWindowText, SendMessageW(lhWnd, WM_GETTEXT, ByVal 512&, StrPtr(GetWindowText)))
End Function
MsgBox GetWindowText(GetForegroundWindow)
Tu forma seria un mas optima, ya que crea el buffer necesario, ni mas grande ni mas pequeño...
Gracias por tu comentario amigo Karcrack, siempre te gustan los codes pequeñitos :silbar:
Salu2 :D
jaja, Karcrack es Minimalista :xD
Cita de: Karcrack en 31 Marzo 2010, 20:30 PM
Tu forma seria un mas optima, ya que crea el buffer necesario, ni mas grande ni mas pequeño...
Option Explicit
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Function WindowTexto() As String
Dim Texto As String: Texto = Space(256)
Dim ret As Long: ret = GetWindowText(GetForegroundWindow(), Texto, 256)
WindowTexto = Left$(Texto, ret)
End Function
@Hasseds: No es necesario crear una nueva varible para el buffer puedes trabajar con WindowTexto. Ademas, sigue habiendo el mismo prolema... El titulo puede ser mucho mas pequeño que 256 o mas grande... Por lo tanto no es optimo
@SkullByte: Si! :-* Minimalismo :-* :xD :xD
Por supuesto, tranquilo, es solo una muestra con GetForegroundWindow directamente dentro de GetWindowText,, y devuelve el buffer, igual te tu code,
Preferí no usar WindowTexto y declarar ret porque no me gusta usar un string para un dato que es Long.
Cita de: Hasseds en 1 Abril 2010, 12:48 PM
Por supuesto, tranquilo, es solo una muestra con GetForegroundWindow directamente dentro de GetWindowText,, y devuelve el buffer, igual te tu code,
Me refiero a que para ser optimo tendrias que usar GetWindowTextLenght()
Cita de: Hasseds en 1 Abril 2010, 12:48 PM
Preferí no usar WindowTexto y declarar ret porque no me gusta usar un string para un dato que es Long.
No entiendo a que te refieres... digo hacer esto:
Public Function WindowTexto() As String
WindowTexto = Space$(256)
WindowTexto = Left$(Texto, GetWindowText(GetForegroundWindow(), WindowTexto, 256))
End Function
Ahora te entendí, me estaba llendo para el carajo, quedó minimalista !
con GetWindowTextLength ?
Option Explicit
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Function WindowTexto() As String
WindowTexto = Space$(GetWindowTextLength(GetForegroundWindow) + 1)
WindowTexto = Left$(WindowTexto, GetWindowText(GetForegroundWindow(), WindowTexto, Len(WindowTexto)))
End Function
Exacto, de esta forma no te pasas o te quedas corto con el Buffer... Aunque hay que usar un API mas... pero bueno, depende de las necesidades que tengas :P
Saludos