AntiGuide: WindowsListerFenetres



PagePrincipale :: DerniersChangements :: ParametresUtilisateur :: Vous êtes 216.73.216.176 :: Signaler un abus :: le: 20250719 08:48:17
pour PrtSc2008
selon https://social.msdn.microsoft.com/Forums/vstudio/en-US/dbf3d65a-e866-442b-9fa1-02672cee863b/how-to-get-foreground-window?forum=vbgeneral

feuille de route:
mémoriser la fenêtre active pour pouvoir la réactiver après capture de l'écran

mieuyx:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   
    End Sub
    Private Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr
  
    Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hWnd As IntPtr, <System.Runtime.InteropServices.Out()> ByRef processId As UInteger) As UInteger
  
    Private Function GetForegroundWindowProcess() As String

        Const nChars As Integer = 256
        Dim Buff As New System.Text.StringBuilder(nChars)
        Dim WindowHandle As IntPtr = 0
        Dim ProcessID As IntPtr = 0

        WindowHandle = GetForegroundWindow()
        GetWindowThreadProcessId(WindowHandle, ProcessID)

        Dim ActiveProcess = Process.GetProcessById(ProcessID)
        GetForegroundWindowProcess = "Process: " & ActiveProcess.MainWindowTitle ' ProcessName

    End Function


    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        TextBox1.Text = GetForegroundWindowProcess()
        System.Threading.Thread.Sleep(1000)

    End Sub
End Class

mais ne diagnostique pas bien la fenêtre active?

Option Strict On

Public Class Form1
    Dim ongo As Boolean = False
    Private Declare Function GetTopWindow Lib "user32.dll" (ByVal hwnd As IntPtr) As IntPtr


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        Me.Text = "Testing"
        Me.CenterToScreen()
    End Sub

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        While True

        If Not CheckBox1.Checked Then Exit Sub

            RichTextBox1.Clear()
            Dim ya As Boolean = False
            For Each p As Process In Process.GetProcesses
                'If the MainWindowTitle of the process is not empty   
                '  RichTextBox1.AppendText("To ")
                If p.MainWindowTitle = String.Empty = False Then
                    'Add the process name, the main window title, and the process ID (what windows uses to identify the process) to the RichTextBox)
                    Try
                        If GetTopWindow(p.Handle).ToString <> "0" Then
                            RichTextBox1.AppendText("Top window is name:" & p.ProcessName & ".....title: " & p.MainWindowTitle & ".....id: " & p.Id & vbCrLf)
                            ya = True
                        Else
                            RichTextBox1.AppendText("Bottom windows are name: " & p.ProcessName & ".....title: " & p.MainWindowTitle & ".....id: " & p.Id & vbCrLf)
                        End If
                    Catch
                        Err.Clear()
                        RichTextBox1.AppendText("???? " & p.ProcessName & "....." & p.MainWindowTitle & "....." & p.Id & vbCrLf)

                    End Try
                    '     If ya Then MsgBox("ya")
                    RichTextBox1.Refresh()
                End If
            Next
            System.Threading.Thread.Sleep(1000)
            Application.DoEvents()
        End While
    End Sub

End Class