28 Ekim 2014 Salı

C# ekrandaki aktif programları alma

c# ile ekrandaki aktif programları almak
var openWindowProcesses = System.Diagnostics.Process.GetProcesses().Where(p => p.MainWindowHandle != IntPtr.Zero);
Use that with a timer right before you start the process to wait for the 3rd party app window to show up.
UPDATE
foreach (var item in openWindowProcesses)
{
    Console.WriteLine(GetWindowTitle(item.MainWindowHandle));
}

[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
public static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam, uint fuFlags, uint uTimeout, out IntPtr lpdwResult);

private static string GetWindowTitle(IntPtr windowHandle)
{
    uint SMTO_ABORTIFHUNG = 0x0002;
    uint WM_GETTEXT = 0xD;
    int MAX_STRING_SIZE = 32768;
    IntPtr result;
    string title = string.Empty;
    IntPtr memoryHandle = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(MAX_STRING_SIZE);
    System.Runtime.InteropServices.Marshal.Copy(title.ToCharArray(), 0, memoryHandle, title.Length);
    SendMessageTimeout(windowHandle, WM_GETTEXT, (IntPtr)MAX_STRING_SIZE, memoryHandle, SMTO_ABORTIFHUNG, (uint)1000, out result);
    title = System.Runtime.InteropServices.Marshal.PtrToStringAuto(memoryHandle);
    System.Runtime.InteropServices.Marshal.FreeCoTaskMem(memoryHandle);
    return title;
}

Hiç yorum yok:

Yorum Gönder