Fenster Focus auslesen

  • VB.NET
  • .NET (FX) 1.0–2.0

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von Cheffboss.

    Fenster Focus auslesen

    Hallo!
    Wie kann ich ermittelt ob ein Fenster bzw. eine Anwendung gerade im Vordergrund steht, also den Focus hat. Ich arbeite an einem „GTA Sand Andreas Keybinder“, und möchte das, man nur Befehle senden kann, wenn dieses Spiel im Vordergrund ist und aktiv!
    Wäre cool wenn mir jemand helfen kann!
    Ich habe leider keinen Code gefunden!
    THX an euch!
    By

    VB.NET-Quellcode

    1. If WindowsFensterFoucusAktiv("gta.exe") = True Then
    2. 'Das Spiel ist gestartet und befindet sich im Vodergrund!
    3. End If
    Visual Basic.NET 8o
    MS-SQL
    8o
    @Cheffboss Da musst Du die API bemühen.
    1. VB.NET-Quellcode

      1. ''' <summary>The GetForegroundWindow function returns a handle to the foreground window.</summary>
      2. ''' <returns>The return value is a handle to the foreground window. The foreground window can be NULL in certain circumstances, such as when a window is losing activation. </returns>
      3. <DllImport("user32.dll", SetLastError:=True)> _
      4. Private Shared Function GetForegroundWindow() As IntPtr
      5. End Function

    2. VB.NET-Quellcode

      1. 'Imports System.Text
      2. <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
      3. Private Shared Function GetWindowText(ByVal hwnd As IntPtr, ByVal lpString As StringBuilder, ByVal cch As Integer) As Integer
      4. End Function

    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Vielen Dank, an euch beiden!
    Ich habe nun die Lösung gefunden!

    Hier für alle sichtbar:

    VB.NET-Quellcode

    1. Public Class Form1
    2. Private Declare Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As IntPtr
    3. Private Declare Auto Function GetWindowText Lib "user32" (ByVal hWnd As System.IntPtr, ByVal lpString As System.Text.StringBuilder, ByVal cch As Integer) As Integer
    4. Private makel As String
    5. Private Function GetCaption() As String
    6. Dim Caption As New System.Text.StringBuilder(256)
    7. Dim hWnd As IntPtr = GetForegroundWindow()
    8. GetWindowText(hWnd, Caption, Caption.Capacity)
    9. Return Caption.ToString()
    10. End Function
    11. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick, Timer1.Tick
    12. Dim CapTxt As String = GetCaption()
    13. If makel <> CapTxt Then
    14. makel = CapTxt
    15. Me.ListBox1.Items.Add(makel) ' Hinzufügen!...
    16. If makel = "GTA:SA:MP" Then
    17. MsgBox("Das Spiel läuft!")
    18. End
    19. End If
    20. End If
    21. End Sub
    22. End Class


    Nochmals Vielen Dank!
    Visual Basic.NET 8o
    MS-SQL
    8o