如何获得最小化的窗口句柄?

时间:2014-08-26 12:24:48

标签: c# .net winforms

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace MinimizeCapture
{

    class WatchForWindow
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        private const int SW_SHOWNORMAL = 1;
        private const int SW_SHOWMINIMIZED = 2;
        private const int SW_SHOWMAXIMIZED = 3;

        private static ManagementEventWatcher watcher = null;

        public static void StartWatching()
        {
            WqlEventQuery query = new WqlEventQuery("Select * From __InstanceCreationEvent Within 2 Where TargetInstance Isa 'Win32_Process'");
            watcher = new ManagementEventWatcher(query);
            watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
            watcher.Start();
        }

        public static void StopWatching()
        {
            if (watcher != null)
            {
                watcher.Stop();
            }
        }

        private static void watcher_EventArrived(object sender, EventArrivedEventArgs e)
        {
            ManagementBaseObject obj = (ManagementBaseObject)e.NewEvent["TargetInstance"];
            string t = obj["Name"].ToString();
            GetHWND(t);
        }

        private static void GetHWND(string wName)
        {
            IntPtr hWnd = FindWindow(wName, "Untitled - Notepad");

        }
    }
}

例如,我打开了一个新的镀铬窗口而不是标签而是窗口。 然后我在string t = obj["Name"].ToString();使用断点,我看到t包含chrome.exe 但最后我看到hWnd是0.我希望最小化窗口句柄(hWnd)不是前面的窗口,而是后面的窗口(最小化)。

编辑:

0xA3也许还有其他方法可以检测每个镀铬打开的窗口而不是通过句柄,并在最小化时截取此窗口的屏幕截图?即使chrome打开了2个窗口,我也可以将当前打开的屏幕截图最小化。问题不是我想要检测自动窗口并在最小化时获取它的屏幕截图。 Chrome我运行的任何其他最小化的流程/应用程序。

这是我今天使用的类,用于获取最小化快照的窗口。 WindowSnap.cs类名是WindowSnap.cs,这是第二个类WindowSnapCollection.cs:WindowSnapCollection.cs这就是我使用第一个类将窗口添加到form1中的listBox然后当我在列表框中的项目我看到每个窗口在pictureBox中拍摄快照:

this.listBoxSnap.Items.AddRange(WindowSnap.GetAllWindows(true, true).ToArray());   

这是WindowSnap.cs类的GetallWindows方法:

public static WindowSnapCollection GetAllWindows(bool minimized, bool specialCapturring)
        {
            windowSnaps = new WindowSnapCollection();
            countMinimizedWindows = minimized;//set minimized flag capture
            useSpecialCapturing = specialCapturring;//set specialcapturing flag
            EnumWindowsCallbackHandler callback = new EnumWindowsCallbackHandler(EnumWindowsCallback);
            EnumWindows(callback, IntPtr.Zero);
            return new WindowSnapCollection(windowSnaps.ToArray(), true);
        }

0 个答案:

没有答案