Refactored platform specific stuff (tray icon and other stuff).

Added Window 7 Application ID.
This commit is contained in:
Lorenz Cuno Klopfenstein 2010-06-29 20:36:08 +02:00
parent ca8570bd76
commit 64fad6e529
9 changed files with 225 additions and 206 deletions

View file

@ -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;

View file

@ -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<CloneClickEventArgs>(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<CloneClickEventArgs>(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<CloseRequestEventArgs>(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 {
/// <summary>
/// Ensures that the main form is visible (either closing the fullscreen mode or reactivating from task icon).
/// </summary>
private void EnsureMainFormVisible() {
public void EnsureMainFormVisible() {
if (_isFullscreen)
ToggleFullscreen();
@ -795,6 +732,42 @@ namespace OnTopReplica {
this.TopMost = true;
}
/// <summary>
/// Opens a confirmation dialog to confirm whether to reset the main form or not.
/// </summary>
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();
}
}
/// <summary>
/// Resets the main form to its initial state.
/// </summary>
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
}

View file

@ -132,9 +132,6 @@
<metadata name="menuLanguages.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>664, 17</value>
</metadata>
<metadata name="menuIconContext.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>399, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>

View file

@ -112,6 +112,7 @@
</Compile>
<Compile Include="CloneClickEventArgs.cs" />
<Compile Include="CloseRequestEventArgs.cs" />
<Compile Include="WindowsSevenMethods.cs" />
<None Include="CommonControls.cs" />
<Compile Include="EnumerationExtensions.cs" />
<Compile Include="FullscreenMode.cs" />
@ -139,12 +140,12 @@
<Compile Include="FocusedTextBox.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="FullscreenForm.cs">
<None Include="FullscreenForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="FullscreenForm.Designer.cs">
</None>
<None Include="FullscreenForm.Designer.cs">
<DependentUpon>FullscreenForm.cs</DependentUpon>
</Compile>
</None>
<Compile Include="MainForm.cs">
<SubType>Form</SubType>
</Compile>
@ -172,9 +173,9 @@
<LastGenOutput>Strings.it.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="FullscreenForm.resx">
<None Include="FullscreenForm.resx">
<DependentUpon>FullscreenForm.cs</DependentUpon>
</EmbeddedResource>
</None>
<EmbeddedResource Include="MainForm.resx">
<DependentUpon>MainForm.cs</DependentUpon>
<SubType>Designer</SubType>

View file

@ -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();
/// <summary>
/// Gets whether OnTopReplica should be displayed in the task bar.
/// Initialized the application. Called once in the app lifetime.
/// </summary>
public virtual bool ShowsInTaskBar {
get {
return false;
}
public virtual void InitApp() {
}
/// <summary>
/// Gets whether OnTopReplica should install a tray icon.
/// </summary>
public virtual bool InstallTrayIcon {
get {
return true;
}
}
protected MainForm Form { get; private set; }
/// <summary>
/// Initialized a form.
/// Initializes a form.
/// </summary>
/// <param name="form">Form to initialize.</param>
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
}
}

View file

@ -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
}
}

View file

@ -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
}
}

View file

@ -14,6 +14,8 @@ namespace OnTopReplica {
public static PlatformSupport Platform { get; private set; }
static CultureInfo _languageChangeCode = Settings.Default.Language;
/// <summary>
/// The main entry point for the application.
/// </summary>
@ -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;
/// <summary>
/// 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);
}
}
}

View file

@ -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);
}
}