このコードをチェックして、実装を手伝ってくれるかどうかを確認したいと思います
もしそうなら、「a」で始まり、「b」で始まるプロセスのみを取得して、より速くクエリを実行する方法はありますか?
(filter
パラメータを使用することによるものですか)
によって必要とされるパラメータを使用して他の方法で実行できますか
EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero)
ネイティブメソッドを使用するだけでなく、別のメソッドを呼び出すEnumDesktopWindows()
にはどうすればよいですか?前述のようにフィルタリング...一種の「ヘルパー」メソッド
private const string USER32 = "user32.dll";
internal delegate bool EnumDelegate(IntPtr hWnd, int lParam);
[DllImport(WindowsFunctions.USER32, EntryPoint = "GetWindowText", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
internal static extern int GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);
[DllImport(WindowsFunctions.USER32)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport(WindowsFunctions.USER32)]
internal static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);
[DllImport(WindowsFunctions.USER32)]
internal static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpfn, IntPtr lParam);
public class WindowData
{
public string Title { get; set; }
public long ProcessId { get; set; }
public long ThreadId { get; set; }
public IntPtr HWindow { get; set; }
}
var collection = new List<WindowData>();
EnumDelegate filter = delegate(IntPtr hWnd, int lParam)
{
StringBuilder strbTitle = new StringBuilder(255);
int nLength = WindowsFunctions.GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1);
string strTitle = strbTitle.ToString();
if (IsWindowVisible(hWnd) && !string.IsNullOrEmpty(strTitle))
{
int pid;
int threadId = WindowsFunctions.GetWindowThreadProcessId(hWnd, out pid);
collection.Add(new WindowData()
{
Title = strbTitle.ToString(),
ProcessId = pid,
ThreadId = threadId,
HWindow = hWnd
});
}
return true;
};
if (EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero))
{
return collection.ToArray();
}
return null;
}