Results 1 to 7 of 7

Thread: Active Window

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    5

    Active Window

    HI!
    How do i get the name and the coordinates of the current active window, not created by my program?

    THX
    Last edited by ChrisiMan; Jan 28th, 2001 at 07:04 AM.

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Code:
    Private Declare Function GetActiveWindow Lib "user32" () As Long
    
    Dim h as Long
    h = GetActiveWindow
    For the name
    Code:
    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

    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    5
    THX Vlatko
    Could you tell me maybe how to get the coordinates of the Window too????

  4. #4
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Code:
    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.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  5. #5
    Guest
    Some explaination to that:
    Code:
    Dim rc As RECT
    GetWindowRect hWnd_of_Window, rc
    To get the hWnd of a Window, use FindWindowEx.
    Code:
    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

  6. #6
    Lively Member
    Join Date
    Jun 2000
    Posts
    124

    Cool SciCalc

    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.
    On Error Resume Screaming...

  7. #7
    Guest
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width