जैसा कि अन्य ने कहा है कि आपको Win32 EnumWindows फ़ंक्शन का उपयोग करके विंडो के माध्यम से गणना करने की आवश्यकता है, और इस तरह अपनी गिनती प्राप्त करें।
आप Process.GetProcesses();
का उपयोग कर प्रक्रियाओं के माध्यम से भी गणना कर सकते हैं हालांकि एक्सप्लोरर विंडो जैसी खिड़कियां जो एक अलग प्रक्रिया नहीं हैं, उस सूची में दिखाई नहीं देगी।
int appCount = 0;
public bool EnumerateWindows(IntPtr hwnd, IntPtr lParam)
{
if (IsWindowVisible(hwnd))
{
StringBuilder sb = new StringBuilder();
string text = "";
GetWindowText(hwnd, sb, 1024);
text = sb.ToString();
if (text != string.Empty && text != "Program Manager")
{
appCount++;
}
}
return true;
}
private int GetAppCount()
{
appCount = 0;
EnumWindows(EnumerateWindows, new IntPtr(0));
return appCount;
}
internal delegate bool EnumThreadWindowsCallback(IntPtr hwnd, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
internal static extern bool IsWindowVisible(IntPtr hwnd);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpString, int nMaxCount);