C# 通过其句柄操纵窗口/进程(即使没有标题或窗口)

C# 通过其句柄操纵窗口/进程(即使没有标题或窗口),c#,winapi,enumeration,window-handles,C#,Winapi,Enumeration,Window Handles,我想检查一下这段代码,看看你是否能帮我实现它 如果是,, 有没有一种方法可以查询更快的“仅获取”功能 从a开始的流程和从b开始的流程 它是通过使用filter参数实现的吗 是否可以通过其他方法完成,并提供所需的参数 EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero) 我如何用另一个调用EnumDesktopWindows,可能不仅仅是使用本机方法-如前所述的筛选。。。a类辅助方法 private const string USER32 =

我想检查一下这段代码,看看你是否能帮我实现它

如果是,, 有没有一种方法可以查询更快的“仅获取”功能 从a开始的流程和从b开始的流程

它是通过使用filter参数实现的吗

是否可以通过其他方法完成,并提供所需的参数

EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero)
我如何用另一个调用EnumDesktopWindows,可能不仅仅是使用本机方法-如前所述的筛选。。。a类辅助方法

 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;
    }

这是我临时想出的解决办法

public static class MyProc
    {
        public static IntPtr WoWhWnd;
        public static string WoWProcName;
        public static string WowMainWinTitle;
        public static int WowID;
        public static IntPtr MyApphWnd;
        public static string MyAppProcName;
        public static string MyAppMainWinTitle;
        public static int MyAppID;
    }


    public void bring(string handletoFront)
    {
        switch (handletoFront)
        {
            default:

                SetForegroundWindow(MyProc.WoWhWnd);

                break;
            case "MyApp":

                SetForegroundWindow(MyProc.MyApphWnd);
                                    break;
        }


    }


           #region <<=========== Enumerating Processes   ============>>

        void  ShowP1(){

            IEnumerable<Process> processes = from p in Process.GetProcesses() where p.ProcessName.StartsWith(TBXSearchWindowTerm.Text) || p.ProcessName.StartsWith("WindowsF") orderby p.ProcessName select p;
            MyProc.MyApphWnd = processes.ElementAt(1).MainWindowHandle;
            MyProc.MyAppMainWinTitle = processes.ElementAt(1).MainWindowTitle;
            MyProc.MyAppID = processes.ElementAt(1).Id;
            MyProc.WoWhWnd = processes.ElementAt(0).MainWindowHandle;
            MyProc.WowMainWinTitle = processes.ElementAt(0).MainWindowTitle;
            MyProc.WowID = processes.ElementAt(0).Id;
            pause(350);
            SetForegroundWindow(MyProc.WoWhWnd);
            pause(850);
            SetForegroundWindow(MyProc.MyApphWnd);
        }

在其中一个查询中,我使用了一个文本框值,因此它可以是一个常量搜索和一个动态可选搜索,如果您看到我做错了什么,或者我可以做得更好,请发表评论。谢谢。

请提出一个简短而直接的问题。这里真的没有问题。你的问题是我该如何执行最后一段代码?如果是这样的话,就把问题的其余部分都去掉,只问这个问题。其实,我是在问一种优雅的方式来查询丢失的硬件,如何用最巧妙的方法获取它。根据你的经验,你会使用不安全的代码来提高任务的性能吗?你会选择什么方法,关于最后一个我发布的想法,利用pinvoke可以完成我提到的工作,优雅而性能优化如果这个codelast可以做到,我想看看怎么做,但主要是你会选择怎么做?我仍然无法理解这一点,也看不到真正的问题。对不起。@DavidHeffernan将它重新压缩为一个尽可能短的问题