コード例 #1
0
ファイル: WindowSaver.cs プロジェクト: menees/Libraries
        /// <summary>
        /// Loads the window settings manually, which is useful if <see cref="AutoLoad"/> is false.
        /// </summary>
        /// <returns>True if the previous settings were re-loaded; false if no previous settings existed.</returns>
        public bool Load()
        {
            bool result = false;

            if (!WindowsUtility.IsInDesignMode(this.Window))
            {
                using (ISettingsStore store = ApplicationInfo.CreateUserSettingsStore())
                {
                    ISettingsNode?settingsNode = this.GetSettingsNode(store, false);
                    if (settingsNode != null)
                    {
                        NativeMethods.LoadWindowPlacement(this.Window, settingsNode, this.LoadStateOverride);
                        result = true;
                    }
                    else if (this.LoadStateOverride != null)
                    {
                        this.Window.WindowState = this.LoadStateOverride.Value;
                    }

                    this.LoadSettings?.Invoke(this, new SettingsEventArgs(store.RootNode));
                }
            }

            return(result);
        }
コード例 #2
0
ファイル: FormSaver.cs プロジェクト: menees/Libraries
        /// <summary>
        /// Used to explicitly save the form's settings if <see cref="AutoSave"/> is false.
        /// </summary>
        public void Save()
        {
            Conditions.RequireState(this.form != null, "Save requires a non-null Form.");

            if (!this.DesignMode)
            {
                using (ISettingsStore store = ApplicationInfo.CreateUserSettingsStore())
                {
                    ISettingsNode?formLayoutNode = this.GetFormLayoutNode(store, true);
                    if (formLayoutNode != null)
                    {
                        // If the form is currently minimized or maximized, then pulling the form's current Bounds
                        // will return values we don't want to store (e.g., -32000 or full screen).  We always want
                        // the Normal window position, so we have to make a Windows API call to get that reliably.
                        Rectangle bounds = NativeMethods.GetNormalWindowBounds(this.form);
                        formLayoutNode.SetValue("Left", bounds.Left);
                        formLayoutNode.SetValue("Top", bounds.Top);
                        formLayoutNode.SetValue("Width", bounds.Width);
                        formLayoutNode.SetValue("Height", bounds.Height);
                        formLayoutNode.SetValue("WindowState", this.form.WindowState);
                    }

                    // Fire the public event first.
                    var eventHandler = this.SaveSettings;
                    eventHandler?.Invoke(this, new SettingsEventArgs(store.RootNode));

                    eventHandler = this.InternalSaveSettings;
                    eventHandler?.Invoke(this, new SettingsEventArgs(store.RootNode));

                    // Make sure the settings get saved out.
                    store.Save();
                }
            }
        }
コード例 #3
0
 internal void SaveNonWindowSettings()
 {
     using (ISettingsStore store = ApplicationInfo.CreateUserSettingsStore())
     {
         ISettingsNode settings = store.RootNode;
         this.profile?.Save(settings.GetSubNode(nameof(Profile), true));
         this.appOptions?.Save(settings.GetSubNode(nameof(AppOptions), true));
         store.Save();
     }
 }
コード例 #4
0
        internal AppOptions LoadNonWindowSettings()
        {
            using (ISettingsStore store = ApplicationInfo.CreateUserSettingsStore())
            {
                ISettingsNode settings = store.RootNode;
                this.appOptions   = new AppOptions(settings.GetSubNode(nameof(AppOptions), false));
                this.profile      = new Profile(settings.GetSubNode(nameof(Profile), false));
                this.stateManager = new StateManager(this.profile);

                string logFileName = this.GenerateLogFileName();
                this.OpenLogger(logFileName);
                this.appOptions.Apply(this);

                this.backgroundTimer = new Timer(this.BackgroundTimerCallback, null, TimeSpan.Zero, TimeSpan.FromSeconds(1));
                return(this.appOptions);
            }
        }
コード例 #5
0
ファイル: WindowSaver.cs プロジェクト: menees/Libraries
        /// <summary>
        /// Saves the window settings manually, which is useful if <see cref="AutoSave"/> is false.
        /// </summary>
        public void Save()
        {
            if (!WindowsUtility.IsInDesignMode(this.Window))
            {
                using (ISettingsStore store = ApplicationInfo.CreateUserSettingsStore())
                {
                    ISettingsNode?settingsNode = this.GetSettingsNode(store, true);
                    if (settingsNode != null)
                    {
                        NativeMethods.SaveWindowPlacement(this.Window, settingsNode);
                    }

                    this.SaveSettings?.Invoke(this, new SettingsEventArgs(store.RootNode));

                    // Make sure the settings get saved out.
                    store.Save();
                }
            }
        }
コード例 #6
0
        private Program()
        {
            // Load options first.
            using (ISettingsStore store = ApplicationInfo.CreateUserSettingsStore())
            {
                Options.Load(store.RootNode);
            }

            // Create a tray icon with no parent form.
            // http://bluehouse.wordpress.com/2006/01/24/how-to-create-a-notify-icon-in-c-without-a-form/
            this.trayIcon                  = new NotifyIcon();
            this.trayIcon.Visible          = true;
            this.trayIcon.Icon             = new Icon(Properties.Resources.Icon16_Stopwatch_00, SystemInformation.SmallIconSize);
            this.trayIcon.ContextMenuStrip = new ContextMenuStrip();
            this.trayIcon.DoubleClick     += this.ShowStatus_Click;

            // Windows uses the tooltip text to identify an icon for its tray notification settings.
            // So the tooltip needs to be predictable and consistent for Windows to match it up
            // correctly.  If this includes variable data (e.g., working time), then Windows will
            // treat each instance as a unique instance, which sucks.
            this.trayIcon.Text = ApplicationInfo.ApplicationName;

            // Set up the context menu.
            ToolStripItemCollection menuItems = this.trayIcon.ContextMenuStrip.Items;

            menuItems.Add("&Show Status", null, this.ShowStatus_Click);
            menuItems.Add("&Reset Timer", null, this.ResetTimer_Click);
            menuItems.Add("-");
            menuItems.Add("Options...", null, this.Options_Click);
            menuItems.Add("About...", null, this.About_Click);
            menuItems.Add("-");
            menuItems.Add("E&xit", null, this.Exit_Click);

            // Create the status window, but don't show it yet.
            this.statusWindow = new StatusWindow();
            this.statusWindow.VisibleChanged += this.StatusWindow_VisibleChanged;

            // Set up the timer.
            this.timer          = new System.Windows.Forms.Timer();
            this.timer.Interval = (int)TimeSpan.FromSeconds(1).TotalMilliseconds;
            this.timer.Tick    += this.Timer_Tick;
            this.timer.Start();
        }
コード例 #7
0
ファイル: FormSaver.cs プロジェクト: menees/Libraries
        /// <summary>
        /// Used to explicitly load the form's settings if <see cref="AutoLoad"/> is false.
        /// </summary>
        /// <returns>True if the previous settings were re-loaded;
        /// False if no previous settings existed.</returns>
        public bool Load()
        {
            bool result = false;

            Conditions.RequireState(this.form != null, "Load requires a non-null Form.");

            if (!this.DesignMode)
            {
                using (ISettingsStore store = ApplicationInfo.CreateUserSettingsStore())
                {
                    ISettingsNode?formLayoutNode = this.GetFormLayoutNode(store, false);
                    if (formLayoutNode != null)
                    {
                        result = true;

                        int             left   = formLayoutNode.GetValue("Left", this.form.Left);
                        int             top    = formLayoutNode.GetValue("Top", this.form.Top);
                        int             width  = formLayoutNode.GetValue("Width", this.form.Width);
                        int             height = formLayoutNode.GetValue("Height", this.form.Height);
                        FormWindowState state  = formLayoutNode.GetValue("WindowState", this.form.WindowState);

                        this.form.SuspendLayout();
                        try
                        {
                            this.form.Location = new Point(left, top);
                            if (this.form.FormBorderStyle == FormBorderStyle.Sizable || this.form.FormBorderStyle == FormBorderStyle.SizableToolWindow)
                            {
                                this.form.Size = new Size(width, height);
                            }

                            // If the form's current state isn't Normal, then it was launched from a
                            // shortcut or command line START command to be Minimized or Maximized.
                            // In those cases, we don't want to override the state.
                            if (this.form.WindowState == FormWindowState.Normal &&
                                (this.AllowLoadMinimized || state != FormWindowState.Minimized))
                            {
                                this.form.WindowState = state;
                            }
                        }
                        finally
                        {
                            this.form.ResumeLayout();
                        }

                        // Make sure the window is somewhere on one of the screens.
                        if (!SystemInformation.VirtualScreen.Contains(left, top))
                        {
                            Point     point         = SystemInformation.VirtualScreen.Location;
                            const int DefaultOffset = 20;
                            point.Offset(DefaultOffset, DefaultOffset);
                            this.form.DesktopLocation = point;
                        }
                    }

                    // Fire the internal event first
                    var eventHandler = this.InternalLoadSettings;
                    eventHandler?.Invoke(this, new SettingsEventArgs(store.RootNode));

                    eventHandler = this.LoadSettings;
                    eventHandler?.Invoke(this, new SettingsEventArgs(store.RootNode));
                }
            }

            return(result);
        }