Beispiel #1
0
        public WindowInfo GetWindowInfo()
        {
            var process = Process;
            var info    = new WindowInfo();

            info.GetWindowText      = GetWindowText();
            info.WM_GETTEXT         = GetWmGettext();
            info.GetClassName       = GetClassName();
            info.RealGetWindowClass = RealGetWindowClass();
            info.Handle             = Handle;
            info.ParentHandle       = NativeMethods.GetParent(Handle);
            info.Size         = Size;
            info.ProcessId    = ProcessId;
            info.ThreadId     = WindowUtils.GetThreadId(Handle);
            info.GWL_STYLE    = NativeMethods.GetWindowLong(Handle, NativeConstants.GWL_STYLE);
            info.GWL_EXSTYLE  = NativeMethods.GetWindowLong(Handle, NativeConstants.GWL_EXSTYLE);
            info.GWL_ID       = NativeMethods.GetWindowLong(Handle, NativeConstants.GWL_ID);
            info.GWL_USERDATA = NativeMethods.GetWindowLong(Handle, NativeConstants.GWL_USERDATA);
            info.GCL_STYLE    = NativeMethods.GetClassLong(Handle, NativeConstants.GCL_STYLE);
            info.GCL_WNDPROC  = NativeMethods.GetClassLong(Handle, NativeConstants.GCL_WNDPROC);
            info.DWL_DLGPROC  = NativeMethods.GetClassLong(Handle, NativeConstants.DWL_DLGPROC);
            info.DWL_USER     = NativeMethods.GetClassLong(Handle, NativeConstants.DWL_USER);
            info.FullPath     = process == null ? "" : process.GetMainModuleFileName();
            info.FullPath     = info.FullPath == null ? "" : info.FullPath;
            info.Priority     = ProcessPriority;
            info.StartTime    = process == null ? (DateTime?)null : process.StartTime;

            try
            {
                var processInfo = SystemUtils.GetWmiProcessInfo(process.Id);
                info.Owner          = processInfo.Owner;
                info.CommandLine    = processInfo.CommandLine;
                info.ThreadCount    = processInfo.ThreadCount;
                info.HandleCount    = processInfo.HandleCount;
                info.VirtualSize    = processInfo.VirtualSize;
                info.WorkingSetSize = processInfo.WorkingSetSize;
            }
            catch
            {
            }

            try
            {
                info.FontName = GetFontName();
            }
            catch
            {
            }

            try
            {
                var windowInfo = new WINDOW_INFO();
                windowInfo.cbSize = Marshal.SizeOf(windowInfo);
                if (NativeMethods.GetWindowInfo(Handle, ref windowInfo))
                {
                    info.WindowInfoExStyle = windowInfo.dwExStyle;
                }
            }
            catch
            {
            }

            try
            {
                uint key;
                Byte alpha;
                uint flags;
                var  result        = NativeMethods.GetLayeredWindowAttributes(Handle, out key, out alpha, out flags);
                var  layeredWindow = (LayeredWindow)flags;
                info.LWA_ALPHA    = layeredWindow.HasFlag(LayeredWindow.LWA_ALPHA);
                info.LWA_COLORKEY = layeredWindow.HasFlag(LayeredWindow.LWA_COLORKEY);
            }
            catch
            {
            }

            try
            {
                info.Instance = process == null ? IntPtr.Zero : process.Modules[0].BaseAddress;
            }
            catch
            {
            }

            try
            {
                info.Parent = Path.GetFileName(process.GetParentProcess().MainModule.FileName);
            }
            catch
            {
            }

            try
            {
                var fileVersionInfo = process.MainModule.FileVersionInfo;
                info.ProductName    = fileVersionInfo.ProductName;
                info.ProductVersion = fileVersionInfo.ProductVersion;
                info.FileVersion    = fileVersionInfo.FileVersion;
                info.Copyright      = fileVersionInfo.LegalCopyright;
            }
            catch
            {
            }

            /*try
             * {
             *  var control = Control.FromHandle(Handle);
             *  var accessibilityObject = control.AccessibilityObject;
             *  info.AccessibleName = accessibilityObject == null ? "" : accessibilityObject.Name;
             *  info.AccessibleValue = accessibilityObject == null ? "" : accessibilityObject.Value;
             *  info.AccessibleRole = accessibilityObject == null ? "" : accessibilityObject.Role.ToString();
             *  info.AccessibleDescription = accessibilityObject == null ? "" : accessibilityObject.Description;
             * }
             * catch
             * {
             * }*/

            return(info);
        }
Beispiel #2
0
        static void ProcessCommandLine(ToggleParser toggleParser, SmartSystemMenuSettings settings)
        {
            // Delay
            if (toggleParser.HasToggle("d") || toggleParser.HasToggle("delay"))
            {
                var delayString = toggleParser.GetToggleValueOrDefault("d", null) ?? toggleParser.GetToggleValueOrDefault("delay", null);
                if (int.TryParse(delayString, out var delay))
                {
                    Thread.Sleep(delay);
                }
            }

            // Clear Clipboard
            if (toggleParser.HasToggle("clearclipboard"))
            {
                Clipboard.Clear();
            }

            var windowHandles = new List <IntPtr>();
            var processId     = (int?)null;

            if (toggleParser.HasToggle("processId"))
            {
                var processIdString = toggleParser.GetToggleValueOrDefault("processId", null);
                processId = !string.IsNullOrWhiteSpace(processIdString) && int.TryParse(processIdString, out var pid) ? pid : (int?)null;
            }

            if (toggleParser.HasToggle("handle"))
            {
                var windowHandleString = toggleParser.GetToggleValueOrDefault("handle", null);
                if (!string.IsNullOrWhiteSpace(windowHandleString))
                {
                    var windowHandle = windowHandleString.StartsWith("0x") ? int.TryParse(windowHandleString.Substring(2), System.Globalization.NumberStyles.AllowHexSpecifier, null, out var number) ? new IntPtr(number) :
                                       IntPtr.Zero : int.TryParse(windowHandleString, out var number2) ? new IntPtr(number2) : IntPtr.Zero;
                    windowHandles.Add(windowHandle);
                }
            }

            if (toggleParser.HasToggle("title"))
            {
                var windowTitle = toggleParser.GetToggleValueOrDefault("title", null);
                var handles     = WindowUtils.FindWindowByTitle(windowTitle, processId, (value, title) => string.Compare(value, title, true) == 0);
                windowHandles.AddRange(handles);
            }

            if (toggleParser.HasToggle("titleBegins"))
            {
                var windowTitle = toggleParser.GetToggleValueOrDefault("titleBegins", null);
                var handles     = WindowUtils.FindWindowByTitle(windowTitle, processId, (value, title) => title.StartsWith(value, StringComparison.OrdinalIgnoreCase));
                windowHandles.AddRange(handles);
            }

            if (toggleParser.HasToggle("titleEnds"))
            {
                var windowTitle = toggleParser.GetToggleValueOrDefault("titleEnds", null);
                var handles     = WindowUtils.FindWindowByTitle(windowTitle, processId, (value, title) => title.EndsWith(value, StringComparison.OrdinalIgnoreCase));
                windowHandles.AddRange(handles);
            }

            if (toggleParser.HasToggle("titleContains"))
            {
                var windowTitle = toggleParser.GetToggleValueOrDefault("titleContains", null);
                var handles     = WindowUtils.FindWindowByTitle(windowTitle, processId, (value, title) => title.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0);
                windowHandles.AddRange(handles);
            }


            foreach (var windowHandle in windowHandles.Where(x => x != IntPtr.Zero && User32.GetParent(x) == IntPtr.Zero))
            {
                var window = new Window(windowHandle);

                // Set a Window monitor
                if (toggleParser.HasToggle("m") || toggleParser.HasToggle("monitor"))
                {
                    var monitorString = toggleParser.GetToggleValueOrDefault("m", null) ?? toggleParser.GetToggleValueOrDefault("monitor", null);
                    if (int.TryParse(monitorString, out var monitor))
                    {
                        var monitorItem = SystemUtils.GetMonitors().Select((x, i) => new { Index = i, MonitorHandle = x }).FirstOrDefault(x => x.Index == monitor);
                        if (monitorItem != null)
                        {
                            window.MoveToMonitor(monitorItem.MonitorHandle);
                        }
                    }
                }

                // Set a Window width
                if (toggleParser.HasToggle("w") || toggleParser.HasToggle("width"))
                {
                    var widthString = toggleParser.GetToggleValueOrDefault("w", null) ?? toggleParser.GetToggleValueOrDefault("width", null);
                    if (int.TryParse(widthString, out var width))
                    {
                        window.SetWidth(width);
                    }
                }

                // Set a Window height
                if (toggleParser.HasToggle("h") || toggleParser.HasToggle("height"))
                {
                    var heightString = toggleParser.GetToggleValueOrDefault("h", null) ?? toggleParser.GetToggleValueOrDefault("height", null);
                    if (int.TryParse(heightString, out var height))
                    {
                        window.SetHeight(height);
                    }
                }

                // Set a Window left position
                if (toggleParser.HasToggle("l") || toggleParser.HasToggle("left"))
                {
                    var leftString = toggleParser.GetToggleValueOrDefault("l", null) ?? toggleParser.GetToggleValueOrDefault("left", null);
                    if (int.TryParse(leftString, out var left))
                    {
                        window.SetLeft(left);
                    }
                }

                // Set a Window top position
                if (toggleParser.HasToggle("t") || toggleParser.HasToggle("top"))
                {
                    var topString = toggleParser.GetToggleValueOrDefault("t", null) ?? toggleParser.GetToggleValueOrDefault("top", null);
                    if (int.TryParse(topString, out var top))
                    {
                        window.SetTop(top);
                    }
                }

                // Set a Window position
                if (toggleParser.HasToggle("a") || toggleParser.HasToggle("alignment"))
                {
                    var windowAlignmentString = toggleParser.GetToggleValueOrDefault("a", null) ?? toggleParser.GetToggleValueOrDefault("alignment", null);
                    var windowAlignment       = Enum.TryParse <WindowAlignment>(windowAlignmentString, true, out var alignment) ? alignment : 0;
                    window.SetAlignment(windowAlignment);
                }

                // Set a Window transparency
                if (toggleParser.HasToggle("transparency"))
                {
                    if (byte.TryParse(toggleParser.GetToggleValueOrDefault("transparency", null), out var transparency))
                    {
                        transparency = transparency > 100 ? (byte)100 : transparency;
                        window.SetTransparency(transparency);
                    }
                }

                // Set a Process priority
                if (toggleParser.HasToggle("p") || toggleParser.HasToggle("priority"))
                {
                    var processPriorityString = toggleParser.GetToggleValueOrDefault("p", null) ?? toggleParser.GetToggleValueOrDefault("priority", null);
                    var processPriority       = Enum.TryParse <Priority>(processPriorityString, true, out var priority) ? priority : 0;
                    window.SetPriority(processPriority);
                }

                // Set a Window AlwaysOnTop
                if (toggleParser.HasToggle("alwaysontop"))
                {
                    var alwaysontopString = toggleParser.GetToggleValueOrDefault("alwaysontop", string.Empty).ToLower();

                    if (alwaysontopString == "on")
                    {
                        window.MakeTopMost(true);
                    }

                    if (alwaysontopString == "off")
                    {
                        window.MakeTopMost(false);
                    }
                }

                // Set a Window Aero Glass
                if (toggleParser.HasToggle("g") || toggleParser.HasToggle("aeroglass"))
                {
                    var aeroglassString = (toggleParser.GetToggleValueOrDefault("g", null) ?? toggleParser.GetToggleValueOrDefault("aeroglass", string.Empty)).ToLower();
                    var enabled         = aeroglassString == "on" ? true : aeroglassString == "off" ? false : (bool?)null;

                    if (enabled.HasValue)
                    {
                        var version = Environment.OSVersion.Version;
                        if (version.Major == 6 && (version.Minor == 0 || version.Minor == 1))
                        {
                            WindowUtils.AeroGlassForVistaAndSeven(window.Handle, enabled.Value);
                        }
                        else if (version.Major >= 6 || (version.Major == 6 && version.Minor > 1))
                        {
                            WindowUtils.AeroGlassForEightAndHigher(window.Handle, enabled.Value);
                        }
                    }
                }

                // Send To Bottom Window
                if (toggleParser.HasToggle("sendtobottom"))
                {
                    window.SendToBottom();
                }

                // Open File In Explorer
                if (toggleParser.HasToggle("o") || toggleParser.HasToggle("openinexplorer"))
                {
                    try
                    {
                        SystemUtils.RunAs("explorer.exe", "/select, " + window.Process.GetMainModuleFileName(), true, UserType.Normal);
                    }
                    catch
                    {
                    }
                }

                // Copy to clipboard
                if (toggleParser.HasToggle("c") || toggleParser.HasToggle("copytoclipboard"))
                {
                    var text = window.ExtractText();
                    if (text != null)
                    {
                        Clipboard.SetText(text);
                    }
                }

                //Information dialog
                if (toggleParser.HasToggle("i") || toggleParser.HasToggle("information"))
                {
                    var dialog = new InfoForm(window.GetWindowInfo(), settings.Language);
                    dialog.ShowDialog();
                }

                //Save Screenshot
                if (toggleParser.HasToggle("s") || toggleParser.HasToggle("savescreenshot"))
                {
                    var bitmap = WindowUtils.PrintWindow(window.Handle);
                    var dialog = new SaveFileDialog
                    {
                        OverwritePrompt  = true,
                        ValidateNames    = true,
                        Title            = settings.Language.GetValue("save_screenshot_title"),
                        FileName         = settings.Language.GetValue("save_screenshot_filename"),
                        DefaultExt       = settings.Language.GetValue("save_screenshot_default_ext"),
                        RestoreDirectory = false,
                        Filter           = settings.Language.GetValue("save_screenshot_filter")
                    };
                    if (dialog.ShowDialog(window.Win32Window) == DialogResult.OK)
                    {
                        var fileExtension = Path.GetExtension(dialog.FileName).ToLower();
                        var imageFormat   = fileExtension == ".bmp" ? ImageFormat.Bmp :
                                            fileExtension == ".gif" ? ImageFormat.Gif :
                                            fileExtension == ".jpeg" ? ImageFormat.Jpeg :
                                            fileExtension == ".png" ? ImageFormat.Png :
                                            fileExtension == ".tiff" ? ImageFormat.Tiff : ImageFormat.Wmf;
                        bitmap.Save(dialog.FileName, imageFormat);
                    }
                }
            }
        }