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

时间:2012-09-01 19:06:00

标签: c# winapi enumeration window-handles

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

如果是的话, 有没有办法快速查询获取 以“a”开头的流程,流程以“b”开头

(是否使用filter参数)

是否可以通过

所需参数的其他方法完成
EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero)

我如何用另一个调用EnumDesktopWindows()或许不仅仅使用本机方法 - 如上所述过滤... kind'a“helper”方法

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

1 个答案:

答案 0 :(得分:0)

这就是我提出的临时解决方案

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

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