示例#1
0
        /// <summary>
        /// OMG this method is awful!!! but yagni
        /// </summary>
        /// <param name="callbackParam"></param>
        private void WindowPositionChangedHandler(CallWindowProcedureParam callbackParam)
        {
            ApplicationDisplayMetrics appMetrics = null;

            if (monitorApplications == null ||
                !monitorApplications.ContainsKey(lastMetrics.Key))
            {
                Log.Error("No definitions found for this resolution: {0}", lastMetrics.Key);
                return;
            }

            appMetrics = monitorApplications[lastMetrics.Key]
                         .FirstOrDefault(row => row.Value.HWnd == callbackParam.hwnd)
                         .Value;

            if (appMetrics == null)
            {
                var newAppWindow = AppWindow.GetWindows()
                                   .FirstOrDefault(row => row.Parent.HWnd.ToInt64() == 0 &&
                                                   !string.IsNullOrEmpty(row.Title) &&
                                                   !row.Title.Equals("Program Manager") &&
                                                   row.Visible &&
                                                   row.HWnd == callbackParam.hwnd);

                if (newAppWindow == null)
                {
                    Log.Error("Can't find hwnd {0}", callbackParam.hwnd.ToInt64());
                    return;
                }
                ApplicationDisplayMetrics applicationDisplayMetric = null;
                AddOrUpdateWindow(lastMetrics.Key, newAppWindow, out applicationDisplayMetric);
                return;
            }

            WindowPlacement windowPlacement = appMetrics.WindowPlacement;
            WindowsPosition newPosition     = (WindowsPosition)Marshal.PtrToStructure(callbackParam.lparam, typeof(WindowsPosition));

            windowPlacement.NormalPosition.Left   = newPosition.Left;
            windowPlacement.NormalPosition.Top    = newPosition.Top;
            windowPlacement.NormalPosition.Right  = newPosition.Left + newPosition.Width;
            windowPlacement.NormalPosition.Bottom = newPosition.Top + newPosition.Height;

            var key = appMetrics.Key;

            if (monitorApplications[lastMetrics.Key].ContainsKey(key))
            {
                monitorApplications[lastMetrics.Key][appMetrics.Key].WindowPlacement = windowPlacement;
            }
            else
            {
                Log.Error("Hwnd {0} is not in list, we should capture", callbackParam.hwnd.ToInt64());
                return;
            }

            Log.Info("WPCH - Capturing {0} at [{1}x{2}] size [{3}x{4}]",
                     appMetrics,
                     appMetrics.WindowPlacement.NormalPosition.Left,
                     appMetrics.WindowPlacement.NormalPosition.Top,
                     appMetrics.WindowPlacement.NormalPosition.Width,
                     appMetrics.WindowPlacement.NormalPosition.Height
                     );
        }