CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 1999
    Posts
    18

    Getting window that has the focus

    Is there an API function that will return the window handle of the window that currently has the focus on the desktop ?


  2. #2
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: Getting window that has the focus

    Sure, it's GetForeGroundWindow(), example:
    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 Sub Form_Load()
    Timer1.Interval = 100
    End Sub

    private Sub Timer1_Timer()
    static lHwnd as Long
    Dim lCurHwnd as Long
    Dim sText as string * 255

    lCurHwnd = GetForegroundWindow
    If lCurHwnd = lHwnd then Exit Sub
    lHwnd = lCurHwnd
    If lHwnd <> hwnd then
    Caption = "ActiveWidow: " & Left$(sText, GetWindowText(lHwnd, byval sText, 255))
    else
    Caption = "ActiveWindow: Form1"
    End If
    End Sub



    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

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

Featured