PDA

Click to See Complete Forum and Search --> : Active Window


ChrisiMan
Jan 28th, 2001, 04:03 AM
HI!
How do i get the name and the coordinates of the current active window, not created by my program?

THX

Vlatko
Jan 28th, 2001, 06:55 AM
Private Declare Function GetActiveWindow Lib "user32" () As Long

Dim h as Long
h = GetActiveWindow


For the name

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 SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Private Sub Form_Activate()
Dim MyStr As String
'Create a buffer
MyStr = String(100, Chr$(0))
'Get the windowtext
GetWindowText Me.hwnd, MyStr, 100
'strip the rest of buffer
MyStr = Left$(MyStr, InStr(MyStr, Chr$(0)) - 1)
'Triple the window's text
MyStr = MyStr + MyStr + MyStr
'Set the new window text
SetWindowText Me.hwnd, MyStr
End Sub

ChrisiMan
Jan 28th, 2001, 08:08 AM
THX Vlatko
Could you tell me maybe how to get the coordinates of the Window too????

Vlatko
Jan 28th, 2001, 10:18 AM
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long

Dim r as Rect
GetWindowRect h,r
'r.left = x pos
'r.top = y pos
'etc.

Jan 28th, 2001, 11:42 AM
Some explaination to that:

Dim rc As RECT
GetWindowRect hWnd_of_Window, rc


To get the hWnd of a Window, use FindWindowEx.

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 Sub Command1_Click
'This will get the hWnd of calculator
hWnd = FindWindowEx(0, 0, "SciCalc", "Calculator")
End Sub

DarkJedi9
Jan 30th, 2001, 01:24 PM
SciCalc is the class name right? And don't you only need one of those arguments? I thought that if you had either one it would work.

Jan 30th, 2001, 02:26 PM
Well...yes...and no.

If we omit the class name as just use the WindowName, Windows will search for a window of any class with the name of Calculator. If we specify SciCalc, then it will only search within that class.

But on the other hand, if we omit the window name and just use the class name, we will still be able to find the window, because every SciCalc window has the name of Calculator (unless it's programatically changed).

So in a sense, either way is right, but I usually try to be as specific as possible.