Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function GetForegroundWindow() As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="GetWindowThreadProcessId")>
Private Shared Function GetWindowThreadProcessId(<InAttribute()> ByVal hWnd As IntPtr, ByRef lpdwProcessId As Integer) As Integer
End Function
<DllImport("user32.dll")>
Shared Function GetAsyncKeyState(ByVal vKey As System.Windows.Forms.Keys) As Short
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'' Code By LeftSpace
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If GetAsyncKeyState(Keys.LButton) <> 0 Then '' Sol Mouse tuşu basılıysa
Dim aPencere As IntPtr = GetForegroundWindow() '' aktif pencereyi alıyoruz
Dim aProcOut As Integer = Nothing '' processid değişkenimiz
GetWindowThreadProcessId(aPencere, aProcOut) '' burada pencere hwnd ile processid öğreniyoruz
If aProcOut Then '' processid var ise textbox texte aktarıyoruz
TextBox1.Text = aProcOut.ToString()
'' olası hata yakalama
Try
'' veya direk GetProcessById İlede olabilir
For Each tmp As Process In Process.GetProcesses()
If tmp.Id = aProcOut Then
TextBox2.Text = tmp.ProcessName
End If
Next
'' GetProcessByID örneği
TextBox2.Text = Process.GetProcessById(aProcOut).ProcessName
Catch ex As Exception
End Try
End If
End If
End Sub
End Class