Ejemplo n.º 1
0
        /// <summary>
        /// Enumerate windows by calling the specified callback proc for each top-level
        /// window in the system. If a custom object is specified this object will
        /// also be passed to the callback proc (allows the callback proc to modify
        /// state, build a list, etc.)
        /// </summary>
        /// <param name="callbackProc">callback proc</param>
        /// <param name="customParam">custom data</param>
        public static void EnumerateChildWindows(IntPtr hWndParent, EnumerateChildWindowsDelegate callbackProc, bool visibleOnly, object customParam)
        {
            // create delegate that will be used for call to native EnumWindows function
            // (also create a GC handle to prevent the delegate from being garbage collected
            // during the call to EnumWindows!)
            EnumWindowsDelegate enumWindowsProc  = new EnumWindowsDelegate(EnumWindowsProc);
            GCHandle            hEnumWindowsProc = GCHandle.Alloc(enumWindowsProc);

            // Create the custom parameter that will be passed to EnumWindows and allocate
            // a GC handle for it so we can pass it as the LPARAM to the native EnumWindows
            EnumWindowsParam param  = new EnumWindowsParam(callbackProc, visibleOnly, customParam);
            GCHandle         hParam = GCHandle.Alloc(param);

            try
            {
                // enumerate windows
                User32.EnumChildWindows(hWndParent, enumWindowsProc, hParam);
            }
            finally
            {
                // free GC handles used for enumerating windows
                hEnumWindowsProc.Free();
                hParam.Free();
            }
        }
Ejemplo n.º 2
0
        public static bool TryAttach(WindowControl window, out string title)
        {
            title = null;
            if (window.AppVar != null)
            {
                return(false);
            }

            var staticCount = window.GetFromWindowClass("Static").Length;

            var buttons = window.GetFromWindowClass("Button");

            if (buttons.Length == 0 || 3 < buttons.Length)
            {
                return(false);
            }

            //The message box must consist of buttons and static.
            int childCount = 0;
            EnumWindowsDelegate emumFunc = (hWnd, param) => { childCount++; return(true); };

            EnumChildWindows(window.Handle, emumFunc, IntPtr.Zero);
            GC.KeepAlive(emumFunc);
            if (buttons.Length + staticCount != childCount)
            {
                return(false);
            }

            title = window.GetWindowText();
            return(true);
        }
Ejemplo n.º 3
0
        internal static List <IntPtr> GetWindows(int processId)
        {
            var handles = new List <IntPtr>();

            EnumWindowsDelegate enumWindows = (IntPtr hwnd, IntPtr lparam) =>
            {
                if (!IsWindow(hwnd) || !IsWindowVisible(hwnd) || !IsWindowEnabled(hwnd))
                {
                    return(true);
                }

                GetWindowThreadProcessId(hwnd, out var windowProcessId);
                if (processId != windowProcessId)
                {
                    return(true);
                }
                handles.Add(hwnd);
                return(true);
            };

            EnumWindows(enumWindows, IntPtr.Zero);
            GC.KeepAlive(enumWindows);

            return(handles);
        }
Ejemplo n.º 4
0
        public static ProcessListModel GetProccesses()
        {
            ProcessListModel    processes    = new ProcessListModel();
            EnumWindowsDelegate enumCallback =
                new EnumWindowsDelegate(EnumWindowsCallBack);

            EnumWindows(enumCallback, processes);
            return(processes);
        }
Ejemplo n.º 5
0
 public CaptureWindow()
 {
     InitializeComponent();
     _windowList          = new Dictionary <string, IntPtr>();
     _enumWindowsDelegate = new EnumWindowsDelegate(EnumWindowCallBack);
     _fpsBaseTime         = DateTime.Now;
     _fpsDrawCount        = 0;
     _soundStream         = Properties.Resources.Camera_Phone03_1;
     _soundPlayer         = new SoundPlayer(_soundStream);
 }
Ejemplo n.º 6
0
        /// <summary>
        /// This function fires every 500ms.  It will scan all windows looking for the Outlook Reminder window
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void monitorWindowsTimer_Tick(object sender, EventArgs e)
        {
            EnumWindowsDelegate filter = delegate(IntPtr hWnd, int lParam)
            {
                try
                {
                    // get the process id of the window we are looking at
                    uint pid = 0;
                    GetWindowThreadProcessId(hWnd, out pid);
                    StringBuilder fileName = new StringBuilder(255);
                    IntPtr        hProcess = OpenProcess(ProcessAccessFlags.QueryInformation | ProcessAccessFlags.VirtualMemoryRead, false, pid);
                    uint          ret      = GetModuleFileNameEx(hProcess, IntPtr.Zero, fileName, fileName.Capacity);
                    if (ret == 0)
                    {
                        int            lastError = Marshal.GetLastWin32Error();
                        Win32Exception ex        = new Win32Exception(lastError);
                        Trace.WriteLine(ex.Message);
                        CloseHandle(hProcess);
                        return(true);
                    }
                    CloseHandle(hProcess);

                    // we only care if this window belongs to Outlook
                    if (fileName.ToString().ToLower().Contains("outlook.exe"))
                    {
                        // retrieve the text of the Window
                        StringBuilder strbTitle = new StringBuilder(255);
                        int           nLength   = GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1);
                        string        strTitle  = strbTitle.ToString();

                        // ignore the "0 Reminder" window
                        if (strTitle.Contains("0 Reminder"))
                        {
                            return(true);
                        }
                        if (strTitle.Contains("Reminder"))
                        {
                            // we have a winner - process it
                            MoveWindowOnTop(hWnd);
                        }
                    }
                }
                catch (Exception ex)
                {
                    // for any exception, display it and kill the timer
                    monitorWindowsTimer.Enabled = false;
                    MessageBox.Show("Fatal error encountered!\n\n" + ex.Message + "\n\n" + ex.StackTrace, "Exception in Outlook Reminders On Top", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                return(true);
            };

            EnumWindows(filter, IntPtr.Zero);
        }
    IntPtr GetThreadWindow()
    {
        // return window handle with correct class associated with current thread
        uint currentThreadId             = GetCurrentThreadId();
        EnumWindowsDelegate enumDelegate = new EnumWindowsDelegate(GetWindowHandle);
        IntPtr enumDelegatePtr           = Marshal.GetFunctionPointerForDelegate(enumDelegate);

        EnumThreadWindows(currentThreadId, enumDelegatePtr, IntPtr.Zero);

        return(bestHandle);
    }
Ejemplo n.º 8
0
        public static IntPtr FindWindow(
            string processName)
        {
#if false
            var handle = IntPtr.Zero;

            var callback = new EnumWindowsDelegate((hWnd, _) =>
            {
                const bool NEXT = true;

                int processId;
                GetWindowThreadProcessId(hWnd, out processId);
                if (processId == 0)
                {
                    Thread.Yield();
                    return(NEXT);
                }

                var p = Process.GetProcessById(processId);
                if (p == null)
                {
                    Thread.Yield();
                    return(NEXT);
                }

                if (p.ProcessName.IndexOf(processName, StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    handle = hWnd;
                    return(false);
                }

                Thread.Yield();
                return(NEXT);
            });

            EnumWindows(callback, IntPtr.Zero);

            return(handle);
#else
            var p = Process.GetProcessesByName(processName);
            if (p == null ||
                p.Length <= 0)
            {
                return(IntPtr.Zero);
            }

            return(p.First().MainWindowHandle);
#endif
        }
Ejemplo n.º 9
0
        public Alert(WindowsAppFriend driverApp)
        {
            int processId       = driverApp.ProcessId;
            int currentThreadId = driverApp.Type(typeof(Codeer.Friendly.Windows.Grasp.Inside.NativeMethods)).GetCurrentThreadId();

            var handles = new List <IntPtr>();
            EnumWindowsDelegate enumWindows = (IntPtr hwnd, IntPtr lparam) =>
            {
                if (!IsWindow(hwnd) || !IsWindowVisible(hwnd) || !IsWindowEnabled(hwnd))
                {
                    return(true);
                }

                GetWindowThreadProcessId(hwnd, out var windowProcessId);
                if (processId != windowProcessId)
                {
                    return(true);
                }

                var sb = new StringBuilder(1024);
                GetWindowText(hwnd, sb, 1024);
                if (sb.ToString().IndexOf("JavaScript") == 0)
                {
                    handles.Add(hwnd);
                }
                return(true);
            };

            EnumWindows(enumWindows, IntPtr.Zero);
            GC.KeepAlive(enumWindows);

            if (0 < handles.Count)
            {
                _app = new WindowsAppFriend(handles[0]);
                var win = new WindowControl(_app, handles[0]);
                _editor  = win.GetFromWindowClass("Edit").FirstOrDefault();
                _ok      = win.GetFromWindowClass("Button").Where(e => e.GetWindowText() == "OK").FirstOrDefault();
                _cancel  = win.GetFromWindowClass("Button").Where(e => e.GetWindowText() == "Cancel").FirstOrDefault();
                _message = win.GetFromWindowClass("Static").FirstOrDefault();
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Enumerate windows by calling the specified callback proc for each top-level
        /// window in the system. If a custom object is specified this object will
        /// also be passed to the callback proc (allows the callback proc to modify
        /// state, build a list, etc.)
        /// </summary>
        /// <param name="callbackProc">callback proc</param>
        /// <param name="customParam">custom data</param>
        public static void EnumerateChildWindows(IntPtr hWndParent, EnumerateChildWindowsDelegate callbackProc, bool visibleOnly, object customParam)
        {
            // create delegate that will be used for call to native EnumWindows function
            // (also create a GC handle to prevent the delegate from being garbage collected
            // during the call to EnumWindows!)
            EnumWindowsDelegate enumWindowsProc = new EnumWindowsDelegate(EnumWindowsProc);
            GCHandle hEnumWindowsProc = GCHandle.Alloc(enumWindowsProc);

            // Create the custom parameter that will be passed to EnumWindows and allocate
            // a GC handle for it so we can pass it as the LPARAM to the native EnumWindows
            EnumWindowsParam param = new EnumWindowsParam(callbackProc, visibleOnly, customParam);
            GCHandle hParam = GCHandle.Alloc(param);
            try
            {
                // enumerate windows
                User32.EnumChildWindows(hWndParent, enumWindowsProc, hParam);
            }
            finally
            {
                // free GC handles used for enumerating windows
                hEnumWindowsProc.Free();
                hParam.Free();
            }
        }
 public static extern bool EnumWindows(EnumWindowsDelegate enumProc, IntPtr lParam);
Ejemplo n.º 12
0
        //
        // 「配信開始」ボタン押下
        //
        private static void PushStart()
        {
            // NicoLiveEncoder が立ち上がっていなければ、何もしない
            if (!IsAlive) return;

            // 配信開始ボタンが有効かチェック。無効であれば、探しに行く
            if (NicoLiveEncoderStartButtonWnd == IntPtr.Zero ||
                GetWindowText(NicoLiveEncoderStartButtonWnd).Contains(NicoStartBtnCaption))
            {
                // ニコライブのメインウインドウを探す
                NicoLiveEncoderStartButtonWnd = IntPtr.Zero;
                hNicoLiveEncoderWnd = FindWindow(null, NicoLiveEncoderTitle);

                // 子ウインドから、スタートボタンを探す
                EnumWindowsDelegate EnumDelegate = new EnumWindowsDelegate(FindStartButton);
                EnumChildWindows(hNicoLiveEncoderWnd, EnumDelegate, IntPtr.Zero);
            }

            // 「配信開始」ボタンが有効であれば、コマンド発行
            if (NicoLiveEncoderStartButtonWnd != IntPtr.Zero)
            {
                PostMessage(NicoDlgWnd, WM_COMMAND, DialogStart_ID, (int)NicoLiveEncoderStartButtonWnd);
            }
        }
Ejemplo n.º 13
0
 static extern bool EnumChildWindows(uint window, EnumWindowsDelegate callback, IntPtr lParam);
 internal static extern int EnumWindows(EnumWindowsDelegate lpEnumFunc, int lParam);
Ejemplo n.º 15
0
 public extern static bool EnumChildWindows(IntPtr hWndParent, EnumWindowsDelegate lpEnumFunc, IntPtr lparam);
Ejemplo n.º 16
0
 public static extern int EnumWindows(EnumWindowsDelegate lpEnumFunc, long lParam);
Ejemplo n.º 17
0
 public static extern bool EnumWindows(EnumWindowsDelegate lpEnumFunc, GCHandle lParam);
Ejemplo n.º 18
0
 private static extern int EnumWindows(EnumWindowsDelegate lpEnumFunc, int lParam);
Ejemplo n.º 19
0
 public static extern IntPtr EnumWindows(EnumWindowsDelegate lpEnumFunc, IntPtr lParam);
Ejemplo n.º 20
0
 private static extern int EnumWindows(EnumWindowsDelegate cb, int lparam);
Ejemplo n.º 21
0
 private static extern int EnumWindows(EnumWindowsDelegate cb, int lparam);
Ejemplo n.º 22
0
 private static extern bool EnumWindows(EnumWindowsDelegate lpfn, int lParam);
Ejemplo n.º 23
0
 public static extern int EnumWindows(EnumWindowsDelegate CallBack,
                                      ProcessListModel Processes);
Ejemplo n.º 24
0
 public static extern int EnumWindows(EnumWindowsDelegate enumWindowsProc, IntPtr lParam);
Ejemplo n.º 25
0
 public static extern bool EnumChildWindows(IntPtr hWndParent, EnumWindowsDelegate lpEnumFunc, GCHandle lParam);
Ejemplo n.º 26
0
 public static extern int EnumChildWindows(IntPtr Parent, EnumWindowsDelegate EnumFunction, IntPtr Parameters);
Ejemplo n.º 27
0
 private static extern int EnumChildWindows(IntPtr hWndParent, EnumWindowsDelegate lpEnumFunc, int lParam);
Ejemplo n.º 28
0
 internal static extern bool EnumChildWindows(
     IntPtr handle,
     [MarshalAs(UnmanagedType.FunctionPtr)] EnumWindowsDelegate lpEnumFunc,
     IntPtr lParam);
 public static extern bool EnumWindows(EnumWindowsDelegate lpEnumFunc, object lParam);
Ejemplo n.º 30
0
 private static extern int EnumWindows(EnumWindowsDelegate cb, IntPtr lParam);
Ejemplo n.º 31
0
 private static extern int EnumWindows(EnumWindowsDelegate cb, IntPtr lParam);
Ejemplo n.º 32
0
 static extern bool EnumThreadWindows(int dwThreadId, EnumWindowsDelegate lpfn, IntPtr lParam);
Ejemplo n.º 33
0
 private static extern bool EnumWindows(EnumWindowsDelegate lpEnumFunc, IntPtr lParam);
 public static extern int EnumWindows(EnumWindowsDelegate CallBack, 
   ProcessWatcher processWatcher);
Ejemplo n.º 35
0
 public extern static bool EnumWindows(EnumWindowsDelegate callback, IntPtr lParam);
 internal static extern int EnumChildWindows(IntPtr hWndParent, EnumWindowsDelegate lpEnumFunc, IntPtr lParam);
Ejemplo n.º 37
0
 public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsDelegate lpEnumFunc, IntPtr lParam);
Ejemplo n.º 38
0
 private static extern int EnumChildWindows(IntPtr Parent, EnumWindowsDelegate EnumFunction, IntPtr Parameters);
Ejemplo n.º 39
0
 public static extern int EnumWindows(EnumWindowsDelegate lpEnumFunc, IntPtr lParam);
Ejemplo n.º 40
0
 internal static extern int EnumWindows(EnumWindowsDelegate lpEnumFunc, IntPtr lparam);
Ejemplo n.º 41
0
        public static void EnumWindowsIncludingModernUI(EnumWindowsDelegate lpEnumFunc, IntPtr lParam)
        {
            int i = 0;
            IntPtr hWnd = IntPtr.Zero;

            while (i < 1000 && (hWnd = FindWindowEx(IntPtr.Zero, hWnd, null, null)) != IntPtr.Zero)
            {
                if (!lpEnumFunc(hWnd, lParam))
                    return;

                //prevent infinite loops
                i++;
            }
        }
Ejemplo n.º 42
0
 public extern static Boolean EnumWindows(EnumWindowsDelegate pEnumFunc, IntPtr lParam);
Ejemplo n.º 43
0
		public extern static bool EnumWindows( EnumWindowsDelegate lpEnumFunc, IntPtr lparam );
Ejemplo n.º 44
0
 private extern static bool EnumChildWindows(IntPtr hWnd, EnumWindowsDelegate lpEnumFunc, IntPtr lparam);
 private static extern bool EnumWindows(EnumWindowsDelegate lpEnumFunc, IntPtr lparam);
Ejemplo n.º 46
0
 public extern static bool EnumWindows(EnumWindowsDelegate lpEnumFunc, IntPtr lparam);
Ejemplo n.º 47
0
 internal static extern bool EnumWindows(EnumWindowsDelegate enumProc, IntPtr lParam);