Example #1
0
 public void Save(XmlWriter writer)
 {
     writer.WriteAttributeString("selectedmainpanel", SelectedMainPanel);
     writer.WriteAttributeString("selectedsidepanel", SelectedSidePanel);
     writer.WriteAttributeBool("alwaysontop", AlwaysOnTop);
     _window.Save(writer);
 }
        public void SettingsAreCorrectlySaved()
        {
            // Arrange
            const string fileName = "WindowSettingsTests.SettingsAreCorrectlySaved";

            var window = new Mock <IWindowAdapter>();

            window.SetupGet(w => w.Height).Returns(1);
            window.SetupGet(w => w.Width).Returns(2);
            window.SetupGet(w => w.Top).Returns(3);
            window.SetupGet(w => w.Left).Returns(4);
            window.SetupGet(w => w.WindowState).Returns(WindowState.Maximized);

            var settings = new WindowSettings();

            // Act
            bool saved = settings.Save(fileName, window.Object);

            // Assert
            Assert.IsTrue(saved);

            var json   = File.ReadAllText(fileName);
            var loaded = JsonConvert.DeserializeObject <WindowSettings>(json);

            Assert.AreEqual(1.0, loaded.Height);
            Assert.AreEqual(2.0, loaded.Width);
            Assert.AreEqual(3.0, loaded.Top);
            Assert.AreEqual(4.0, loaded.Left);
            Assert.AreEqual(WindowState.Maximized, loaded.State);
        }
 public void Save(XmlWriter writer)
 {
     writer.WriteAttributeString("selectedmainpanel", SelectedMainPanel);
     writer.WriteAttributeString("selectedsidepanel", SelectedSidePanel);
     writer.WriteAttributeBool("alwaysontop", AlwaysOnTop);
     writer.WriteAttributeBool("isleftsidepanelvisible", IsLeftSidePanelVisible);
     writer.WriteAttributeEnum("previouswindowstate", _previousWindowState);
     _window.Save(writer);
 }
Example #4
0
        }         // Customers

        // ----------------------------------------------------------------------
        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);

            if (DialogResult == false)
            {
                return;                 // is canceling
            }

            if (!WindowSettings.Settings.HasChanges)
            {
                return;                 // nothing to do
            }

            StringBuilder sb = new StringBuilder();

            foreach (ISetting setting in WindowSettings.Settings)
            {
                if (!setting.HasChanged)
                {
                    continue;
                }

                sb.Append(" - ");
                sb.Append(setting.ToString());
                sb.Append("\n");
            }

            MessageBoxResult result = MessageBox.Show("Save changes?\n\n" + sb, Title, MessageBoxButton.YesNoCancel);

            switch (result)
            {
            case MessageBoxResult.Yes:
                WindowSettings.Save();
                break;

            case MessageBoxResult.No:
                break;

            case MessageBoxResult.Cancel:
                DialogResult = null;
                e.Cancel     = true;
                break;
            }
        }         // OnClosing
Example #5
0
 internal static void SaveWindowSettings(Window window)
 {
     WindowSettings.Save(new WindowWrapper(window));
 }
Example #6
0
        private void WindowGetMsg(object sender, WndProcEventArgs e)
        {
            var message = e.Message.ToInt64();

            if (message == WM_SYSCOMMAND)
            {
                //string dbgMessage = string.Format("WM_SYSCOMMAND, Form, Handle = {0}, WParam = {1}", e.Handle, e.WParam);
                //System.Diagnostics.Trace.WriteLine(dbgMessage);
                var window = _windows.FirstOrDefault(w => w.Handle == e.Handle);
                if (window != null)
                {
                    var lowOrder = e.WParam.ToInt64() & 0x0000FFFF;
                    switch (lowOrder)
                    {
                    case MenuItemId.SC_MAXIMIZE:
                    {
                        window.Menu.UncheckSizeMenu();
                    }
                    break;

                    case MenuItemId.SC_MINIMIZE_TO_SYSTEMTRAY:
                    {
                        window.MinimizeToSystemTray();
                    }
                    break;

                    case MenuItemId.SC_MINIMIZE_ALWAYS_TO_SYSTEMTRAY:
                    {
                        bool r = window.Menu.IsMenuItemChecked(MenuItemId.SC_MINIMIZE_ALWAYS_TO_SYSTEMTRAY);
                        window.Menu.CheckMenuItem(MenuItemId.SC_MINIMIZE_ALWAYS_TO_SYSTEMTRAY, !r);
                        window.SetStateMinimizeToTrayAlways(!r);
                    }
                    break;

                    case MenuItemId.SC_SUSPEND_TO_SYSTEMTRAY:
                    {
                        window.MinimizeToSystemTray();
                        Thread.Sleep(100);
                        window.Suspend();
                    }
                    break;

                    case MenuItemId.SC_INFORMATION:
                    {
                        var infoForm = new InfoForm(window.GetWindowInfo(), _settings.Language);
                        infoForm.Show(window.Win32Window);
                    }
                    break;

                    case MenuItemId.SC_SAVE_SCREEN_SHOT:
                    {
                        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);
                        }
                    }
                    break;

                    case MenuItemId.SC_COPY_WINDOW_TEXT:
                    {
                        var text = window.ExtractText();
                        if (!string.IsNullOrEmpty(text))
                        {
                            Clipboard.SetText(text);
                        }
                    }
                    break;

                    case MenuItemId.SC_COPY_WINDOW_TITLE:
                    {
                        var text = window.GetWindowText();
                        if (!string.IsNullOrEmpty(text))
                        {
                            Clipboard.SetText(text);
                        }
                    }
                    break;

                    case MenuItemId.SC_COPY_FULL_PROCESS_PATH:
                    {
                        var path = window.Process?.GetMainModuleFileName();
                        if (!string.IsNullOrEmpty(path))
                        {
                            Clipboard.SetText(path);
                        }
                    }
                    break;

                    case MenuItemId.SC_CLEAR_CLIPBOARD:
                    {
                        Clipboard.Clear();
                    }
                    break;

                    case MenuItemId.SC_DRAG_BY_MOUSE:
                    {
                        var isChecked = window.Menu.IsMenuItemChecked(MenuItemId.SC_DRAG_BY_MOUSE);
                        window.Menu.CheckMenuItem(MenuItemId.SC_DRAG_BY_MOUSE, !isChecked);
                    }
                    break;

                    case MenuItemId.SC_OPEN_FILE_IN_EXPLORER:
                    {
                        try
                        {
                            SystemUtils.RunAs("explorer.exe", "/select, " + window.Process.GetMainModuleFileName(), true, UserType.Normal);
                        }
                        catch
                        {
                        }
                    }
                    break;

                    case MenuItemId.SC_MINIMIZE_OTHER_WINDOWS:
                    case MenuItemId.SC_CLOSE_OTHER_WINDOWS:
                    {
                        EnumWindows((IntPtr handle, int lParam) =>
                            {
                                if (handle != IntPtr.Zero && handle != Handle && handle != window.Handle && WindowUtils.IsAltTabWindow(handle))
                                {
                                    if (lowOrder == MenuItemId.SC_CLOSE_OTHER_WINDOWS)
                                    {
                                        PostMessage(handle, WM_CLOSE, 0, 0);
                                    }
                                    else
                                    {
                                        PostMessage(handle, WM_SYSCOMMAND, MenuItemId.SC_MINIMIZE, 0);
                                    }
                                }
                                return(true);
                            }, 0);
                    }
                    break;

                    case MenuItemId.SC_TOPMOST:
                    {
                        var isChecked = window.Menu.IsMenuItemChecked(MenuItemId.SC_TOPMOST);
                        window.Menu.CheckMenuItem(MenuItemId.SC_TOPMOST, !isChecked);
                        window.MakeTopMost(!isChecked);
                    }
                    break;

                    case MenuItemId.SC_HIDE_FOR_ALT_TAB:
                    {
                        var isChecked = window.Menu.IsMenuItemChecked(MenuItemId.SC_HIDE_FOR_ALT_TAB);
                        window.Menu.CheckMenuItem(MenuItemId.SC_HIDE_FOR_ALT_TAB, !isChecked);
                        window.HideForAltTab(!isChecked);
                    }
                    break;

                    case MenuItemId.SC_SEND_TO_BOTTOM:
                    {
                        window.SendToBottom();
                    }
                    break;

                    case MenuItemId.SC_AERO_GLASS:
                    {
                        var isChecked = window.Menu.IsMenuItemChecked(MenuItemId.SC_AERO_GLASS);
                        window.AeroGlass(!isChecked);
                        window.Menu.CheckMenuItem(MenuItemId.SC_AERO_GLASS, !isChecked);
                    }
                    break;

                    case MenuItemId.SC_ROLLUP:
                    {
                        var isChecked = window.Menu.IsMenuItemChecked(MenuItemId.SC_ROLLUP);
                        window.Menu.CheckMenuItem(MenuItemId.SC_ROLLUP, !isChecked);
                        if (!isChecked)
                        {
                            window.RollUp();
                            window.Menu.UncheckSizeMenu();
                        }
                        else
                        {
                            window.UnRollUp();
                        }
                    }
                    break;


                    case MenuItemId.SC_SIZE_DEFAULT:
                    {
                        window.Menu.UncheckSizeMenu();
                        window.Menu.CheckMenuItem(MenuItemId.SC_SIZE_DEFAULT, true);
                        window.ShowNormal();
                        window.RestoreSize();
                        window.Menu.UncheckMenuItems(MenuItemId.SC_ROLLUP);
                    }
                    break;

                    case MenuItemId.SC_SIZE_CUSTOM:
                    {
                        var sizeForm = new SizeForm(window, _settings);
                        var result   = sizeForm.ShowDialog(window.Win32Window);
                        if (result == DialogResult.OK)
                        {
                            window.ShowNormal();

                            if (_settings.Sizer == WindowSizerType.WindowWithMargins)
                            {
                                window.SetSize(sizeForm.WindowWidth, sizeForm.WindowHeight, sizeForm.WindowLeft, sizeForm.WindowTop);
                            }
                            else if (_settings.Sizer == WindowSizerType.WindowWithoutMargins)
                            {
                                var margins = window.GetSystemMargins();
                                window.SetSize(sizeForm.WindowWidth + margins.Left + margins.Right, sizeForm.WindowHeight + margins.Top + margins.Bottom, sizeForm.WindowLeft, sizeForm.WindowTop);
                            }
                            else
                            {
                                window.SetSize(sizeForm.WindowWidth + (window.Size.Width - window.ClientSize.Width), sizeForm.WindowHeight + (window.Size.Height - window.ClientSize.Height), sizeForm.WindowLeft, sizeForm.WindowTop);
                            }

                            window.Menu.UncheckSizeMenu();
                            window.Menu.CheckMenuItem(MenuItemId.SC_SIZE_CUSTOM, true);
                            window.Menu.UncheckMenuItems(MenuItemId.SC_ROLLUP);
                        }
                    }
                    break;

                    case MenuItemId.SC_TRANS_DEFAULT:
                    {
                        window.Menu.UncheckTransparencyMenu();
                        window.Menu.CheckMenuItem(MenuItemId.SC_TRANS_DEFAULT, true);
                        window.RestoreTransparency();
                    }
                    break;

                    case MenuItemId.SC_TRANS_CUSTOM:
                    {
                        var opacityForm = new TransparencyForm(window, _settings);
                        var result      = opacityForm.ShowDialog(window.Win32Window);
                        if (result == DialogResult.OK)
                        {
                            window.SetTransparency(opacityForm.WindowTransparency);
                            window.Menu.UncheckTransparencyMenu();
                            window.Menu.CheckMenuItem(MenuItemId.SC_TRANS_CUSTOM, true);
                        }
                    }
                    break;

                    case MenuItemId.SC_ALIGN_DEFAULT:
                    {
                        window.Menu.UncheckAlignmentMenu();
                        window.Menu.CheckMenuItem(MenuItemId.SC_ALIGN_DEFAULT, true);
                        window.RestorePosition();
                    }
                    break;

                    case MenuItemId.SC_ALIGN_CUSTOM:
                    {
                        var positionForm = new PositionForm(window, _settings.Language);
                        var result       = positionForm.ShowDialog(window.Win32Window);

                        if (result == DialogResult.OK)
                        {
                            window.ShowNormal();
                            window.SetPosition(positionForm.WindowLeft, positionForm.WindowTop);
                            window.Menu.UncheckAlignmentMenu();
                            window.Menu.CheckMenuItem(MenuItemId.SC_ALIGN_CUSTOM, true);
                        }
                    }
                    break;

                    case MenuItemId.SC_TRANS_100:
                    case MenuItemId.SC_TRANS_90:
                    case MenuItemId.SC_TRANS_80:
                    case MenuItemId.SC_TRANS_70:
                    case MenuItemId.SC_TRANS_60:
                    case MenuItemId.SC_TRANS_50:
                    case MenuItemId.SC_TRANS_40:
                    case MenuItemId.SC_TRANS_30:
                    case MenuItemId.SC_TRANS_20:
                    case MenuItemId.SC_TRANS_10:
                    case MenuItemId.SC_TRANS_00:
                        SetTransparencyMenuItem(window, (int)lowOrder);
                        break;

                    case MenuItemId.SC_PRIORITY_REAL_TIME:
                    case MenuItemId.SC_PRIORITY_HIGH:
                    case MenuItemId.SC_PRIORITY_ABOVE_NORMAL:
                    case MenuItemId.SC_PRIORITY_NORMAL:
                    case MenuItemId.SC_PRIORITY_BELOW_NORMAL:
                    case MenuItemId.SC_PRIORITY_IDLE:
                        SetPriorityMenuItem(window, (int)lowOrder);
                        break;

                    case MenuItemId.SC_ALIGN_TOP_LEFT:
                    case MenuItemId.SC_ALIGN_TOP_CENTER:
                    case MenuItemId.SC_ALIGN_TOP_RIGHT:
                    case MenuItemId.SC_ALIGN_MIDDLE_LEFT:
                    case MenuItemId.SC_ALIGN_MIDDLE_CENTER:
                    case MenuItemId.SC_ALIGN_MIDDLE_RIGHT:
                    case MenuItemId.SC_ALIGN_BOTTOM_LEFT:
                    case MenuItemId.SC_ALIGN_BOTTOM_CENTER:
                    case MenuItemId.SC_ALIGN_BOTTOM_RIGHT:
                    case MenuItemId.SC_ALIGN_CENTER_HORIZONTALLY:
                    case MenuItemId.SC_ALIGN_CENTER_VERTICALLY:
                        SetAlignmentMenuItem(window, (int)lowOrder);
                        break;
                    }

                    var moveToSubMenuItem = (int)lowOrder - MenuItemId.SC_MOVE_TO;
                    if (window.Menu.MoveToMenuItems.ContainsKey(moveToSubMenuItem))
                    {
                        var monitorHandle = window.Menu.MoveToMenuItems[moveToSubMenuItem];
                        window.MoveToMonitor(monitorHandle);
                    }

                    var windowSizeItem = _settings.MenuItems.WindowSizeItems.FirstOrDefault(x => x.Id == lowOrder);
                    if (windowSizeItem != null)
                    {
                        SetSizeMenuItem(window, (int)lowOrder, windowSizeItem);
                    }

                    for (int i = 0; i < _settings.MenuItems.StartProgramItems.Count; i++)
                    {
                        if (lowOrder - MenuItemId.SC_START_PROGRAM == i)
                        {
                            try
                            {
                                var item                 = _settings.MenuItems.StartProgramItems[i];
                                var arguments            = item.Arguments;
                                var argumentParameters   = arguments.GetParams(item.BeginParameter, item.EndParameter);
                                var allParametersInputed = true;
                                var processPath          = window.Process?.GetMainModuleFileName() ?? string.Empty;
                                foreach (var parameter in argumentParameters)
                                {
                                    var parameterName = parameter.TrimStart(item.BeginParameter).TrimEnd(item.EndParameter);
                                    if (string.Compare(parameterName, StartProgramMenuItem.PARAMETER_PROCESS_ID, true) == 0)
                                    {
                                        arguments = arguments.Replace(parameter, window.Process?.Id.ToString() ?? string.Empty);
                                        continue;
                                    }

                                    if (string.Compare(parameterName, StartProgramMenuItem.PARAMETER_PROCESS_NAME, true) == 0)
                                    {
                                        arguments = arguments.Replace(parameter, Path.GetFileName(processPath));
                                        continue;
                                    }

                                    if (string.Compare(parameterName, StartProgramMenuItem.PARAMETER_WINDOW_TITLE, true) == 0)
                                    {
                                        arguments = arguments.Replace(parameter, window.GetWindowText());
                                        continue;
                                    }

                                    var parameterForm = new ParameterForm(parameterName, _settings.Language);
                                    var result        = parameterForm.ShowDialog(window.Win32Window);

                                    if (result == DialogResult.OK)
                                    {
                                        arguments = arguments.Replace(parameter, parameterForm.ParameterValue);
                                    }
                                    else
                                    {
                                        allParametersInputed = false;
                                        break;
                                    }
                                }

                                if (allParametersInputed)
                                {
                                    SystemUtils.RunAs(item.FileName, arguments, item.ShowWindow, item.RunAs, item.UseWindowWorkingDirectory ? Path.GetDirectoryName(processPath) : null);
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                            break;
                        }
                    }

                    var isSaveItemChecked = window.Menu.IsMenuItemChecked(MenuItemId.SC_SAVE_SELECTED_ITEMS);
                    if (lowOrder == MenuItemId.SC_SAVE_SELECTED_ITEMS)
                    {
                        isSaveItemChecked = !isSaveItemChecked;
                        window.Menu.CheckMenuItem(MenuItemId.SC_SAVE_SELECTED_ITEMS, isSaveItemChecked);
                        window.RefreshState();
                        var windowClassName = window.GetClassName();
                        var processPath     = window.Process?.GetMainModuleFileName() ?? string.Empty;
                        if (!string.IsNullOrEmpty(windowClassName) && !string.IsNullOrEmpty(processPath))
                        {
                            var states = _windowSettings.Find(windowClassName, processPath);
                            if (states.Any())
                            {
                                _windowSettings.Remove(windowClassName, processPath);
                            }

                            if (isSaveItemChecked)
                            {
                                _windowSettings.Items.Add((WindowState)window.State.Clone());
                            }

#if WIN32
                            var fileName = Path.Combine(AssemblyUtils.AssemblyDirectory, "Window.xml");
#else
                            var fileName = Path.Combine(AssemblyUtils.AssemblyDirectory, "Window64.xml");
#endif
                            WindowSettings.Save(fileName, _windowSettings);
                        }
                    }
                    else if (isSaveItemChecked &&
                             lowOrder != MenuItemId.SC_MOVE &&
                             lowOrder != MenuItemId.SC_MINIMIZE &&
                             lowOrder != MenuItemId.SC_MAXIMIZE &&
                             lowOrder != MenuItemId.SC_RESTORE &&
                             lowOrder != MenuItemId.SC_RESIZE &&
                             lowOrder != MenuItemId.SC_CLOSE)
                    {
                        window.RefreshState();
                        var windowClassName = window.GetClassName();
                        var processPath     = window.Process?.GetMainModuleFileName() ?? string.Empty;
                        if (!string.IsNullOrEmpty(windowClassName) && !string.IsNullOrEmpty(processPath))
                        {
                            var states = _windowSettings.Find(windowClassName, processPath);
                            if (states.Any())
                            {
                                _windowSettings.Remove(windowClassName, processPath);
                            }

                            _windowSettings.Items.Add((WindowState)window.State.Clone());
#if WIN32
                            var fileName = Path.Combine(AssemblyUtils.AssemblyDirectory, "Window.xml");
#else
                            var fileName = Path.Combine(AssemblyUtils.AssemblyDirectory, "Window64.xml");
#endif
                            WindowSettings.Save(fileName, _windowSettings);
                        }
                    }
                }
            }
        }