diff --git a/OnTopReplica/MainForm.Designer.cs b/OnTopReplica/MainForm.Designer.cs index 522ee61..b552fde 100644 --- a/OnTopReplica/MainForm.Designer.cs +++ b/OnTopReplica/MainForm.Designer.cs @@ -69,16 +69,11 @@ this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.menuContextClose = new System.Windows.Forms.ToolStripMenuItem(); this.danskToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.menuIconContext = new System.Windows.Forms.ContextMenuStrip(this.components); - this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.resetWindowToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.menuContext.SuspendLayout(); this.menuWindows.SuspendLayout(); this.menuOpacity.SuspendLayout(); this.menuResize.SuspendLayout(); this.menuLanguages.SuspendLayout(); - this.menuIconContext.SuspendLayout(); this.SuspendLayout(); // // menuContext @@ -453,42 +448,6 @@ this.danskToolStripMenuItem.Text = "Dansk"; this.danskToolStripMenuItem.Click += new System.EventHandler(this.Menu_Language_click); // - // menuIconContext - // - this.menuIconContext.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.openToolStripMenuItem, - this.resetWindowToolStripMenuItem, - this.exitToolStripMenuItem}); - this.menuIconContext.Name = "menuIconContext"; - this.menuIconContext.Size = new System.Drawing.Size(148, 70); - // - // openToolStripMenuItem - // - this.openToolStripMenuItem.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.openToolStripMenuItem.Image = global::OnTopReplica.Properties.Resources.icon; - this.openToolStripMenuItem.Name = "openToolStripMenuItem"; - this.openToolStripMenuItem.Size = new System.Drawing.Size(147, 22); - this.openToolStripMenuItem.Text = global::OnTopReplica.Strings.MenuOpen; - this.openToolStripMenuItem.ToolTipText = global::OnTopReplica.Strings.MenuOpenTT; - this.openToolStripMenuItem.Click += new System.EventHandler(this.TaskIconOpen_click); - // - // resetWindowToolStripMenuItem - // - this.resetWindowToolStripMenuItem.Name = "resetWindowToolStripMenuItem"; - this.resetWindowToolStripMenuItem.Size = new System.Drawing.Size(147, 22); - this.resetWindowToolStripMenuItem.Text = global::OnTopReplica.Strings.MenuReset; - this.resetWindowToolStripMenuItem.ToolTipText = global::OnTopReplica.Strings.MenuResetTT; - this.resetWindowToolStripMenuItem.Click += new System.EventHandler(this.TaskIconReset_click); - // - // exitToolStripMenuItem - // - this.exitToolStripMenuItem.Image = global::OnTopReplica.Properties.Resources.close_new; - this.exitToolStripMenuItem.Name = "exitToolStripMenuItem"; - this.exitToolStripMenuItem.Size = new System.Drawing.Size(147, 22); - this.exitToolStripMenuItem.Text = global::OnTopReplica.Strings.MenuClose; - this.exitToolStripMenuItem.ToolTipText = global::OnTopReplica.Strings.MenuCloseTT; - this.exitToolStripMenuItem.Click += new System.EventHandler(this.TaskIconExit_click); - // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -507,7 +466,6 @@ this.menuOpacity.ResumeLayout(false); this.menuResize.ResumeLayout(false); this.menuLanguages.ResumeLayout(false); - this.menuIconContext.ResumeLayout(false); this.ResumeLayout(false); } @@ -530,12 +488,8 @@ private System.Windows.Forms.ToolStripMenuItem selectRegionToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem resizeToolStripMenuItem; private System.Windows.Forms.ToolStripSeparator toolStripSeparator2; - private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem5; - private System.Windows.Forms.ContextMenuStrip menuIconContext; - private System.Windows.Forms.ToolStripMenuItem openToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem noneToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem resetWindowToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem5; + private System.Windows.Forms.ToolStripMenuItem noneToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem switchToWindowToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem dockToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem topLeftToolStripMenuItem; diff --git a/OnTopReplica/MainForm.cs b/OnTopReplica/MainForm.cs index 9379e1e..1824c4e 100644 --- a/OnTopReplica/MainForm.cs +++ b/OnTopReplica/MainForm.cs @@ -19,9 +19,6 @@ namespace OnTopReplica { FullscreenForm _fullscreenForm; HotKeyManager _hotKeyManager; - //Icon - NotifyIcon taskIcon = null; - //Window manager WindowManager _windowManager; WindowHandle _lastWindowHandle = null; @@ -42,28 +39,30 @@ namespace OnTopReplica { public MainForm() { InitializeComponent(); - this.SetStyle(ControlStyles.EnableNotifyMessage, true); KeepAspectRatio = false; //Thumbnail panel - _thumbnailPanel = new ThumbnailPanel(Settings.Default.UseGlass); - _thumbnailPanel.RegionDrawn += new ThumbnailPanel.RegionDrawnHandler(Thumbnail_RegionDrawn); - _thumbnailPanel.CloneClick += new EventHandler(Thumbnail_CloneClick); - _thumbnailPanel.ClickThrough = !_clickForwarding; - _thumbnailPanel.Location = Point.Empty; - _thumbnailPanel.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - _thumbnailPanel.Size = ClientSize; + _thumbnailPanel = new ThumbnailPanel(Settings.Default.UseGlass){ + ClickThrough = !_clickForwarding, + Location = Point.Empty, + Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right, + Size = ClientSize + }; + _thumbnailPanel.RegionDrawn += new ThumbnailPanel.RegionDrawnHandler(Thumbnail_RegionDrawn); + _thumbnailPanel.CloneClick += new EventHandler(Thumbnail_CloneClick); Controls.Add(_thumbnailPanel); //Region box - _regionBox = new RegionBox(); - _regionBox.RequestClosing += new EventHandler(RegionBox_RequestClosing); - _regionBox.RequestRegionReset += new EventHandler(RegionBox_RequestRegionReset); - _regionBox.RegionSet += new RegionBox.RegionSetHandler(RegionBox_RegionChanged); - _regionBox.Location = new Point(ClientSize.Width, 0); - _regionBox.Size = new Size(_regionBox.Width, ClientSize.Height); - _regionBox.Anchor = AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom; - _regionBox.Enabled = _regionBox.Visible = false; + _regionBox = new RegionBox { + Location = new Point(ClientSize.Width, 0), + Anchor = AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom, + Enabled = false, + Visible = false + }; + _regionBox.Size = new Size(_regionBox.Width, ClientSize.Height); + _regionBox.RequestClosing += new EventHandler(RegionBox_RequestClosing); + _regionBox.RequestRegionReset += new EventHandler(RegionBox_RequestRegionReset); + _regionBox.RegionSet += new RegionBox.RegionSetHandler(RegionBox_RegionChanged); Controls.Add(_regionBox); //Full screen form @@ -71,9 +70,9 @@ namespace OnTopReplica { _fullscreenForm.CloseRequest += new EventHandler(FullscreenForm_CloseRequest); //Set native renderer on context menus - Asztal.Szótár.NativeToolStripRenderer.SetToolStripRenderer(new Control[] { - menuContext, menuWindows, menuOpacity, menuIconContext, menuResize, menuLanguages - }); + Asztal.Szótár.NativeToolStripRenderer.SetToolStripRenderer( + menuContext, menuWindows, menuOpacity, menuResize, menuLanguages + ); //Hook keyboard handler this.KeyUp += new KeyEventHandler(Common_Key); @@ -189,25 +188,33 @@ namespace OnTopReplica { #region Event override - protected override void OnNotifyMessage(Message m) { - if (_hotKeyManager != null) - _hotKeyManager.ProcessHotKeys(m); + protected override void OnShown(EventArgs e) { + base.OnShown(e); + + //Get a window manager + _windowManager = new WindowManager(); + + //Platform specific form initialization + Program.Platform.InitForm(this); + + //Reload position settings if needed + if (_startOverride) { + Location = _startLocation; + Size = _startSize; + } + else if (Settings.Default.WindowPositionStored) { + Location = Settings.Default.LastLocation; + ClientSize = Settings.Default.LastSize; + } + + //Glassify window + SetGlass(Settings.Default.UseGlass); } protected override void OnClosing(CancelEventArgs e) { - //Destroy NotifyIcon - if (taskIcon != null) { - taskIcon.Visible = false; - taskIcon.Dispose(); - taskIcon = null; - } - //Destroy hotkeys - { - var manager = _hotKeyManager; - _hotKeyManager = null; - manager.Dispose(); - } + _hotKeyManager.Dispose(); + _hotKeyManager = null; //Store position settings if (Settings.Default.StoreWindowPosition) { @@ -229,46 +236,20 @@ namespace OnTopReplica { new Margins(-1); } + protected override void OnActivated(EventArgs e) { + base.OnActivated(e); + } + protected override void OnMouseWheel(MouseEventArgs e) { base.OnMouseWheel(e); AdjustSize(e.Delta); } - protected override void OnShown(EventArgs e) { - base.OnShown(e); - - //Get a window manager - _windowManager = new WindowManager(); - - //Platform specific tray/taskbar - this.ShowInTaskbar = Program.Platform.ShowsInTaskBar; - if (Program.Platform.InstallTrayIcon) { - taskIcon = new NotifyIcon { - Text = Strings.ApplicationName, - Icon = Properties.Resources.main_icon, - Visible = true, - ContextMenuStrip = menuIconContext - }; - taskIcon.DoubleClick += new EventHandler(TaskIcon_doubleclick); - } - Program.Platform.InitForm(this); - - //Reload position settings if needed - if (_startOverride) { - Location = _startLocation; - Size = _startSize; - } - else if (Settings.Default.WindowPositionStored) { - Location = Settings.Default.LastLocation; - ClientSize = Settings.Default.LastSize; - } - - //Glassify window - SetGlass(Settings.Default.UseGlass); - } - protected override void WndProc(ref Message m) { + if (_hotKeyManager != null) + _hotKeyManager.ProcessHotKeys(m); + base.WndProc(ref m); //Open context menu if right button clicked on caption (i.e. all of the window area because of glass) @@ -281,50 +262,6 @@ namespace OnTopReplica { #endregion - #region Task Icon events - - void TaskIcon_doubleclick(object sender, EventArgs e) { - EnsureMainFormVisible(); - } - - private void TaskIconOpen_click(object sender, EventArgs e) { - EnsureMainFormVisible(); - } - - private void TaskIconReset_click(object sender, EventArgs e) { - var dlg = new TaskDialog(Strings.AskReset, Strings.AskResetTitle, Strings.AskResetContent); - dlg.UseCommandLinks = true; - dlg.CustomButtons = new CustomButton[] { - new CustomButton(Result.OK, Strings.AskResetButtonOk), - new CustomButton(Result.Cancel, Strings.ButtonCancel) - }; - dlg.CommonIcon = TaskDialogIcon.Information; - - if (dlg.Show().CommonButton == Result.OK) { - //Reset display status - TaskIcon_doubleclick(sender, e); - - //Reset form settings - ThumbnailUnset(); - RegionBoxShowing = false; - - Point nuLoc = Screen.PrimaryScreen.WorkingArea.Location; - nuLoc.Offset(40, 40); - - Location = nuLoc; - Size = MinimumSize; - - Show(); - _fullscreenForm.Hide(); - } - } - - private void TaskIconExit_click(object sender, EventArgs e) { - this.Close(); - } - - #endregion - #region Menu Event Handling private void Close_click(object sender, EventArgs e) { @@ -785,7 +722,7 @@ namespace OnTopReplica { /// /// Ensures that the main form is visible (either closing the fullscreen mode or reactivating from task icon). /// - private void EnsureMainFormVisible() { + public void EnsureMainFormVisible() { if (_isFullscreen) ToggleFullscreen(); @@ -795,6 +732,42 @@ namespace OnTopReplica { this.TopMost = true; } + /// + /// Opens a confirmation dialog to confirm whether to reset the main form or not. + /// + public void ResetMainFormWithConfirmation() { + var dlg = new TaskDialog(Strings.AskReset, Strings.AskResetTitle, Strings.AskResetContent); + dlg.UseCommandLinks = true; + dlg.CustomButtons = new CustomButton[] { + new CustomButton(Result.OK, Strings.AskResetButtonOk), + new CustomButton(Result.Cancel, Strings.ButtonCancel) + }; + dlg.CommonIcon = TaskDialogIcon.Information; + + if (dlg.Show(this).CommonButton == Result.OK) { + ResetMainForm(); + } + } + + /// + /// Resets the main form to its initial state. + /// + public void ResetMainForm() { + //Reset form settings + ThumbnailUnset(); + RegionBoxShowing = false; + + //Reset location and size (edge of the screen, min size) + Point nuLoc = Screen.PrimaryScreen.WorkingArea.Location; + nuLoc.Offset(40, 40); + Location = nuLoc; + Size = MinimumSize; + + _fullscreenForm.Hide(); + this.Show(); + this.Activate(); + } + #endregion } diff --git a/OnTopReplica/MainForm.resx b/OnTopReplica/MainForm.resx index 7cd3861..81c9540 100644 --- a/OnTopReplica/MainForm.resx +++ b/OnTopReplica/MainForm.resx @@ -132,9 +132,6 @@ 664, 17 - - 399, 17 - diff --git a/OnTopReplica/OnTopReplica.csproj b/OnTopReplica/OnTopReplica.csproj index 9f64d30..fcd15c7 100644 --- a/OnTopReplica/OnTopReplica.csproj +++ b/OnTopReplica/OnTopReplica.csproj @@ -112,6 +112,7 @@ + @@ -139,12 +140,12 @@ Component - + Form - - + + FullscreenForm.cs - + Form @@ -172,9 +173,9 @@ Strings.it.Designer.cs Designer - + FullscreenForm.cs - + MainForm.cs Designer diff --git a/OnTopReplica/PlatformSupport.cs b/OnTopReplica/PlatformSupport.cs index 8d81a57..9cafc93 100644 --- a/OnTopReplica/PlatformSupport.cs +++ b/OnTopReplica/PlatformSupport.cs @@ -5,7 +5,7 @@ using OnTopReplica.Platforms; using System.Windows.Forms; namespace OnTopReplica { - abstract class PlatformSupport { + abstract class PlatformSupport : IDisposable { public static PlatformSupport Create() { var os = Environment.OSVersion; @@ -32,29 +32,36 @@ namespace OnTopReplica { public abstract bool CheckCompatibility(); /// - /// Gets whether OnTopReplica should be displayed in the task bar. + /// Initialized the application. Called once in the app lifetime. /// - public virtual bool ShowsInTaskBar { - get { - return false; - } + public virtual void InitApp() { } - /// - /// Gets whether OnTopReplica should install a tray icon. - /// - public virtual bool InstallTrayIcon { - get { - return true; - } - } + protected MainForm Form { get; private set; } /// - /// Initialized a form. + /// Initializes a form. /// /// Form to initialize. - public virtual void InitForm(Form form) { + public virtual void InitForm(MainForm form) { + Form = form; } + public virtual void ShutdownApp() { + } + + #region IDisposable Members + + bool _isDisposed = false; + + public void Dispose() { + if (_isDisposed) + return; + + this.ShutdownApp(); + _isDisposed = true; + } + + #endregion } } diff --git a/OnTopReplica/Platforms/WindowsSeven.cs b/OnTopReplica/Platforms/WindowsSeven.cs index 6dd1400..77b9113 100644 --- a/OnTopReplica/Platforms/WindowsSeven.cs +++ b/OnTopReplica/Platforms/WindowsSeven.cs @@ -7,23 +7,20 @@ using System.Windows.Forms; namespace OnTopReplica.Platforms { class WindowsSeven : WindowsVista { - public override bool InstallTrayIcon { - get { - return false; - } - } - - public override bool ShowsInTaskBar { - get { - return true; - } - } - - public override void InitForm(Form form) { - base.InitForm(form); - + public override void InitForm(MainForm form) { DwmManager.SetExludeFromPeek(form, true); DwmManager.SetDisallowPeek(form, true); + + base.InitForm(form); + } + + public override void InitApp() { + //Set Application ID + WindowsSevenMethods.SetCurrentProcessExplicitAppUserModelID("OnTopReplica"); + } + + protected override void InitFormCore(MainForm form) { + //do nothing } } diff --git a/OnTopReplica/Platforms/WindowsVista.cs b/OnTopReplica/Platforms/WindowsVista.cs index 9bbd9bd..0ab143c 100644 --- a/OnTopReplica/Platforms/WindowsVista.cs +++ b/OnTopReplica/Platforms/WindowsVista.cs @@ -3,8 +3,11 @@ using System.Collections.Generic; using System.Text; using System.Windows.Forms; using VistaControls.Dwm; +using OnTopReplica.Properties; +using VistaControls.TaskDialog; namespace OnTopReplica.Platforms { + class WindowsVista : PlatformSupport { public override bool CheckCompatibility() { @@ -15,9 +18,74 @@ namespace OnTopReplica.Platforms { return true; } - public override void InitForm(Form form) { + public override void InitForm(MainForm form) { + base.InitForm(form); + DwmManager.SetWindowFlip3dPolicy(form, Flip3DPolicy.ExcludeAbove); + + this.InitFormCore(form); } + protected virtual void InitFormCore(MainForm form){ + //Do not show in task bar, but display icon + form.ShowInTaskbar = false; + + //Install tray icon + _contextMenu = new ContextMenuStrip(); + _contextMenu.Items.AddRange(new ToolStripItem[] { + new ToolStripMenuItem(Strings.MenuOpen, Resources.icon, TaskIconOpen_click) { + ToolTipText = Strings.MenuOpenTT, + }, + new ToolStripMenuItem(Strings.MenuReset, null, TaskIconReset_click){ + ToolTipText = Strings.MenuResetTT + }, + new ToolStripMenuItem(Strings.MenuClose, Resources.close_new, TaskIconExit_click){ + ToolTipText = Strings.MenuCloseTT + } + }); + Asztal.Szótár.NativeToolStripRenderer.SetToolStripRenderer(_contextMenu); + + _taskIcon = new NotifyIcon { + Text = Strings.ApplicationName, + Icon = Resources.main_icon, + Visible = true, + ContextMenuStrip = _contextMenu + }; + _taskIcon.DoubleClick += new EventHandler(TaskIcon_doubleclick); + } + + NotifyIcon _taskIcon; + ContextMenuStrip _contextMenu; + + public override void ShutdownApp() { + //Destroy NotifyIcon + if (_taskIcon != null) { + _taskIcon.Visible = false; + _taskIcon.Dispose(); + _taskIcon = null; + } + } + + #region Task Icon events + + void TaskIcon_doubleclick(object sender, EventArgs e) { + Form.EnsureMainFormVisible(); + } + + private void TaskIconOpen_click(object sender, EventArgs e) { + Form.EnsureMainFormVisible(); + } + + private void TaskIconReset_click(object sender, EventArgs e) { + Form.ResetMainFormWithConfirmation(); + } + + private void TaskIconExit_click(object sender, EventArgs e) { + Form.Close(); + } + + #endregion + } + } diff --git a/OnTopReplica/Program.cs b/OnTopReplica/Program.cs index 9c8ea4f..23133bc 100644 --- a/OnTopReplica/Program.cs +++ b/OnTopReplica/Program.cs @@ -14,6 +14,8 @@ namespace OnTopReplica { public static PlatformSupport Platform { get; private set; } + static CultureInfo _languageChangeCode = Settings.Default.Language; + /// /// The main entry point for the application. /// @@ -29,6 +31,7 @@ namespace OnTopReplica { Platform = PlatformSupport.Create(); if (!Platform.CheckCompatibility()) return; + Platform.InitApp(); //Update settings if needed if (Settings.Default.MustUpdate) { @@ -64,8 +67,6 @@ namespace OnTopReplica { Settings.Default.Save(); } - static CultureInfo _languageChangeCode = Settings.Default.Language; - /// /// Forces a global language change. As soon as the main form is closed, the change is performed /// and the form is reopened using the new language. @@ -102,6 +103,7 @@ namespace OnTopReplica { sw.WriteLine("OS: {0}", Environment.OSVersion.ToString()); sw.WriteLine(".NET: {0}", Environment.Version.ToString()); sw.WriteLine("Aero DWM: {0}", VistaControls.OsSupport.IsCompositionEnabled); + sw.WriteLine("Launch command: {0}", Environment.CommandLine); } } } diff --git a/OnTopReplica/WindowsSevenMethods.cs b/OnTopReplica/WindowsSevenMethods.cs new file mode 100644 index 0000000..c84bbb5 --- /dev/null +++ b/OnTopReplica/WindowsSevenMethods.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.InteropServices; + +namespace OnTopReplica { + + static class WindowsSevenMethods { + + [DllImport("shell32.dll")] + internal static extern void SetCurrentProcessExplicitAppUserModelID( + [MarshalAs(UnmanagedType.LPWStr)] string AppID); + + [DllImport("shell32.dll")] + internal static extern void GetCurrentProcessExplicitAppUserModelID( + [Out(), MarshalAs(UnmanagedType.LPWStr)] out string AppID); + + } + +}