| 
	
		|  Autor | Tema: [SNIPPET] GetTitleActiveApp (VB6)  (Leído 6,076 veces) |  
	| 
			| 
					
						| The Swash 
								
								 Desconectado 
								Mensajes: 194
								
								 
								Programmer
								
								
								
								
								
								     | 
 
Call:'-----------------------------------------------------------' 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
 
 
 |  
						| 
								|  |  
								| « Última modificación: 31 Marzo 2010, 18:37 pm por The Swash » |  En línea | 
 
 |  |  |  | 
			| 
					
						| Karcrack 
								       
								
								 Desconectado 
								Mensajes: 2.416
								
								 
								Se siente observado ¬¬'
								
								
								
								
								
								   | 
 
Muy interesante... pero se podria acortar si damos un buffer grande y trabajamos con el valor que retorna WM_GETTEXT: The return value is the number of TCHARs copied, not including the terminating null character. Por lo tanto: 'USER32Private Declare Function GetForegroundWindow Lib "USER32" () As LongPrivate 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...
 
 |  
						| 
								|  |  
								|  |  En línea | 
 
 |  |  |  | 
			| 
					
						| The Swash 
								
								 Desconectado 
								Mensajes: 194
								
								 
								Programmer
								
								
								
								
								
								     | 
 
Gracias por tu comentario amigo Karcrack, siempre te gustan los codes pequeñitos    Salu2    |  
						| 
								|  |  
								|  |  En línea | 
 
 |  |  |  | 
			| 
					
						| wh0! 
								
								 Desconectado 
								Mensajes: 10
								
								   | 
 
jaja, Karcrack es Minimalista    |  
						| 
								|  |  
								|  |  En línea | 
 
 |  |  |  | 
			| 
					
						| Hasseds 
								
								 Desconectado 
								Mensajes: 145
								
								   | 
 
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
 
 
 |  
						| 
								|  |  
								|  |  En línea | 
 
 Sergio Desanti |  |  |  | 
			| 
					
						| Karcrack 
								       
								
								 Desconectado 
								Mensajes: 2.416
								
								 
								Se siente observado ¬¬'
								
								
								
								
								
								   | 
 
@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       |  
						| 
								|  |  
								|  |  En línea | 
 
 |  |  |  | 
			| 
					
						| Hasseds 
								
								 Desconectado 
								Mensajes: 145
								
								   | 
 
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.
 
 
 
 |  
						| 
								|  |  
								|  |  En línea | 
 
 Sergio Desanti |  |  |  | 
			| 
					
						| Karcrack 
								       
								
								 Desconectado 
								Mensajes: 2.416
								
								 
								Se siente observado ¬¬'
								
								
								
								
								
								   | 
 
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() 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
 |  
						| 
								|  |  
								|  |  En línea | 
 
 |  |  |  | 
			| 
					
						| Hasseds 
								
								 Desconectado 
								Mensajes: 145
								
								   | 
 
Ahora te entendí, me estaba llendo para el carajo, quedó minimalista ! |  
						| 
								|  |  
								|  |  En línea | 
 
 Sergio Desanti |  |  |  | 
			| 
					
						| Hasseds 
								
								 Desconectado 
								Mensajes: 145
								
								   | 
 
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
 
 
 |  
						| 
								|  |  
								|  |  En línea | 
 
 Sergio Desanti |  |  |  |  |  
 
	
 
 
				
					
						| Mensajes similares |  
						|  | Asunto | Iniciado por | Respuestas | Vistas | Último mensaje |  
						|   |   | [m][SNIPPET] IsUserAnAdmin? Programación Visual Basic
 | Karcrack | 5 | 3,377 |  30 Julio 2010, 17:38 pm por Karcrack
 |  
						|   |   | [SNIPPET] Decimal a Romano Programación Visual Basic
 | Karcrack | 4 | 2,562 |  17 Diciembre 2010, 14:41 pm por 79137913
 |  
						|   |   | [SNIPPET-VB6] DrawGraph - Dibujar sobre controles. Programación Visual Basic
 | F3B14N | 0 | 1,812 |  12 Marzo 2011, 14:48 pm por F3B14N
 |  
						|   |   | [SNIPPET][VB6] WhereAmI, thisexe, GetMyPath... Programación Visual Basic
 | Karcrack | 4 | 3,312 |  11 Marzo 2014, 03:42 am por LeandroA
 |  
						|   |   | Ayuda .cpp Snippet Programación C/C++
 | alexlopezcifuentes | 1 | 1,945 |  6 Marzo 2015, 11:11 am por eferion
 |    |