Code modernising (#8)

* Applied some code conventions, used more current language features which should improve readability and making it easier to re-factor / modify. Also fixed some typos in documentation.
* Changes based on PR conversation for the SystemInfo
* Some modifications due to conversations on the PR, especially I removed the FlagsAttribute on the BalloonFlags.
* Removed Silverlight targeting in code.
This commit is contained in:
Robin Krom
2019-07-16 14:10:00 +02:00
committed by Philipp Sumi
parent 90051586d4
commit 23186feefe
36 changed files with 611 additions and 754 deletions

View File

@@ -0,0 +1,109 @@
// Some interop code taken from Mike Marshall's AnyForm
using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
public class AppBarInfo
{
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("shell32.dll")]
private static extern uint SHAppBarMessage(uint dwMessage, ref APPBARDATA data);
[DllImport("user32.dll")]
private static extern int SystemParametersInfo(uint uiAction, uint uiParam,
IntPtr pvParam, uint fWinIni);
private const int ABE_BOTTOM = 3;
private const int ABE_LEFT = 0;
private const int ABE_RIGHT = 2;
private const int ABE_TOP = 1;
private const int ABM_GETTASKBARPOS = 0x00000005;
// SystemParametersInfo constants
private const uint SPI_GETWORKAREA = 0x0030;
private APPBARDATA m_data;
public ScreenEdge Edge
{
get { return (ScreenEdge) m_data.uEdge; }
}
public Rectangle WorkArea
{
get { return GetRectangle(m_data.rc); }
}
private Rectangle GetRectangle(RECT rc)
{
return new Rectangle(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
}
public void GetPosition(string strClassName, string strWindowName)
{
m_data = new APPBARDATA();
m_data.cbSize = (uint) Marshal.SizeOf(m_data.GetType());
IntPtr hWnd = FindWindow(strClassName, strWindowName);
if (hWnd != IntPtr.Zero)
{
uint uResult = SHAppBarMessage(ABM_GETTASKBARPOS, ref m_data);
if (uResult != 1)
{
throw new Exception("Failed to communicate with the given AppBar");
}
}
else
{
throw new Exception("Failed to find an AppBar that matched the given criteria");
}
}
public void GetSystemTaskBarPosition()
{
GetPosition("Shell_TrayWnd", null);
}
public enum ScreenEdge
{
Undefined = -1,
Left = ABE_LEFT,
Top = ABE_TOP,
Right = ABE_RIGHT,
Bottom = ABE_BOTTOM
}
[StructLayout(LayoutKind.Sequential)]
private struct APPBARDATA
{
public uint cbSize;
public IntPtr hWnd;
public uint uCallbackMessage;
public uint uEdge;
public RECT rc;
public int lParam;
}
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
}
}

View File

@@ -1,3 +1,5 @@
using System;
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
/// <summary>

View File

@@ -56,7 +56,8 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// the terminating NULL. For Version 5.0 and later, szTip can have a maximum of
/// 128 characters, including the terminating NULL.
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string ToolTipText;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string ToolTipText;
/// <summary>
@@ -66,7 +67,7 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// <summary>
/// A value that specifies which bits of the state member are retrieved or modified.
/// For example, setting this member to <see cref="Interop.IconState.Hidden"/>
/// For example, setting this member to <see cref="TaskbarNotification.Interop.IconState.Hidden"/>
/// causes only the item's hidden
/// state to be retrieved.
/// </summary>
@@ -76,12 +77,13 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// String with the text for a balloon ToolTip. It can have a maximum of 255 characters.
/// To remove the ToolTip, set the NIF_INFO flag in uFlags and set szInfo to an empty string.
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] public string BalloonText;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string BalloonText;
/// <summary>
/// Mainly used to set the version when <see cref="WinApi.Shell_NotifyIcon"/> is invoked
/// with <see cref="NotifyCommand.SetVersion"/>. However, for legacy operations,
/// the same member is also used to set timouts for balloon ToolTips.
/// the same member is also used to set timeouts for balloon ToolTips.
/// </summary>
public uint VersionOrTimeout;
@@ -89,7 +91,8 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// String containing a title for a balloon ToolTip. This title appears in boldface
/// above the text. It can have a maximum of 63 characters.
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)] public string BalloonTitle;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string BalloonTitle;
/// <summary>
/// Adds an icon to a balloon ToolTip, which is placed to the left of the title. If the
@@ -108,7 +111,7 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// <summary>
/// Windows Vista (Shell32.dll version 6.0.6) and later. The handle of a customized
/// balloon icon provided by the application that should be used independently
/// of the tray icon. If this member is non-NULL and the <see cref="Interop.BalloonFlags.User"/>
/// of the tray icon. If this member is non-NULL and the <see cref="TaskbarNotification.Interop.BalloonFlags.User"/>
/// flag is set, this icon is used as the balloon icon.<br/>
/// If this member is NULL, the legacy behavior is carried out.
/// </summary>
@@ -120,7 +123,7 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// a hidden taskbar icon without the icon being set.
/// </summary>
/// <param name="handle"></param>
/// <returns></returns>
/// <returns>NotifyIconData</returns>
public static NotifyIconData CreateDefault(IntPtr handle)
{
var data = new NotifyIconData();
@@ -157,7 +160,7 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
| IconDataMembers.Tip;
//reset strings
data.ToolTipText = data.BalloonText = data.BalloonTitle = String.Empty;
data.ToolTipText = data.BalloonText = data.BalloonTitle = string.Empty;
return data;
}

View File

@@ -2,37 +2,34 @@ using System.Windows.Interop;
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
public static class SystemInfo
{
private static System.Windows.Point? dpiFactors;
private static System.Windows.Point? DpiFactors
/// <summary>
/// This class is a helper for system information, currently to get the DPI factors
/// </summary>
public static class SystemInfo
{
get
{
if (dpiFactors == null)
using (var source = new HwndSource(new HwndSourceParameters()))
dpiFactors = new System.Windows.Point(source.CompositionTarget.TransformToDevice.M11, source.CompositionTarget.TransformToDevice.M22);
return dpiFactors;
}
private static readonly System.Windows.Point DpiFactors;
static SystemInfo()
{
using (var source = new HwndSource(new HwndSourceParameters()))
{
if (source.CompositionTarget?.TransformToDevice != null)
{
DpiFactors = new System.Windows.Point(source.CompositionTarget.TransformToDevice.M11, source.CompositionTarget.TransformToDevice.M22);
return;
}
DpiFactors = new System.Windows.Point(1, 1);
}
}
/// <summary>
/// Returns the DPI X Factor
/// </summary>
public static double DpiFactorX => DpiFactors.X;
/// <summary>
/// Returns the DPI Y Factor
/// </summary>
public static double DpiFactorY => DpiFactors.Y;
}
public static double DpiXFactor
{
get
{
var factors = DpiFactors;
return factors.HasValue ? factors.Value.X : 1;
}
}
public static double DpiYFactor
{
get
{
var factors = DpiFactors;
return factors.HasValue ? factors.Value.Y : 1;
}
}
}
}
}

View File

@@ -1,10 +1,6 @@
// Some interop code taken from Mike Marshall's AnyForm
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
@@ -26,25 +22,24 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
Rectangle rcWorkArea = info.WorkArea;
int x = 0, y = 0;
if (info.Edge == AppBarInfo.ScreenEdge.Left)
switch (info.Edge)
{
x = rcWorkArea.Right + space;
y = rcWorkArea.Bottom;
}
else if (info.Edge == AppBarInfo.ScreenEdge.Bottom)
{
x = rcWorkArea.Right;
y = rcWorkArea.Bottom - rcWorkArea.Height - space;
}
else if (info.Edge == AppBarInfo.ScreenEdge.Top)
{
x = rcWorkArea.Right;
y = rcWorkArea.Top + rcWorkArea.Height + space;
}
else if (info.Edge == AppBarInfo.ScreenEdge.Right)
{
x = rcWorkArea.Right - rcWorkArea.Width - space;
y = rcWorkArea.Bottom;
case AppBarInfo.ScreenEdge.Left:
x = rcWorkArea.Right + space;
y = rcWorkArea.Bottom;
break;
case AppBarInfo.ScreenEdge.Bottom:
x = rcWorkArea.Right;
y = rcWorkArea.Bottom - rcWorkArea.Height - space;
break;
case AppBarInfo.ScreenEdge.Top:
x = rcWorkArea.Right;
y = rcWorkArea.Top + rcWorkArea.Height + space;
break;
case AppBarInfo.ScreenEdge.Right:
x = rcWorkArea.Right - rcWorkArea.Width - space;
y = rcWorkArea.Bottom;
break;
}
return GetDeviceCoordinates(new Point {X = x, Y = y});
@@ -54,112 +49,15 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// Recalculates OS coordinates in order to support WPFs coordinate
/// system if OS scaling (DPIs) is not 100%.
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
/// <param name="point">Point</param>
/// <returns>Point</returns>
public static Point GetDeviceCoordinates(Point point)
{
return new Point() { X = (int)(point.X / SystemInfo.DpiXFactor), Y = (int)(point.Y / SystemInfo.DpiYFactor) };
}
}
public class AppBarInfo
{
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
[DllImport("shell32.dll")]
private static extern UInt32 SHAppBarMessage(UInt32 dwMessage, ref APPBARDATA data);
[DllImport("user32.dll")]
private static extern Int32 SystemParametersInfo(UInt32 uiAction, UInt32 uiParam,
IntPtr pvParam, UInt32 fWinIni);
private const int ABE_BOTTOM = 3;
private const int ABE_LEFT = 0;
private const int ABE_RIGHT = 2;
private const int ABE_TOP = 1;
private const int ABM_GETTASKBARPOS = 0x00000005;
// SystemParametersInfo constants
private const UInt32 SPI_GETWORKAREA = 0x0030;
private APPBARDATA m_data;
public ScreenEdge Edge
{
get { return (ScreenEdge) m_data.uEdge; }
}
public Rectangle WorkArea
{
get { return GetRectangle(m_data.rc); }
}
private Rectangle GetRectangle(RECT rc)
{
return new Rectangle(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
}
public void GetPosition(string strClassName, string strWindowName)
{
m_data = new APPBARDATA();
m_data.cbSize = (UInt32) Marshal.SizeOf(m_data.GetType());
IntPtr hWnd = FindWindow(strClassName, strWindowName);
if (hWnd != IntPtr.Zero)
{
UInt32 uResult = SHAppBarMessage(ABM_GETTASKBARPOS, ref m_data);
if (uResult != 1)
{
throw new Exception("Failed to communicate with the given AppBar");
}
}
else
{
throw new Exception("Failed to find an AppBar that matched the given criteria");
}
}
public void GetSystemTaskBarPosition()
{
GetPosition("Shell_TrayWnd", null);
}
public enum ScreenEdge
{
Undefined = -1,
Left = ABE_LEFT,
Top = ABE_TOP,
Right = ABE_RIGHT,
Bottom = ABE_BOTTOM
}
[StructLayout(LayoutKind.Sequential)]
private struct APPBARDATA
{
public UInt32 cbSize;
public IntPtr hWnd;
public UInt32 uCallbackMessage;
public UInt32 uEdge;
public RECT rc;
public Int32 lParam;
}
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public Int32 left;
public Int32 top;
public Int32 right;
public Int32 bottom;
return new Point
{
X = (int)(point.X / SystemInfo.DpiFactorX),
Y = (int)(point.Y / SystemInfo.DpiFactorY)
};
}
}
}

View File

@@ -8,6 +8,8 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// </summary>
internal static class WinApi
{
private const string User32 = "user32.dll";
/// <summary>
/// Creates, updates or deletes the taskbar icon.
/// </summary>
@@ -18,7 +20,7 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// <summary>
/// Creates the helper window that receives messages from the taskar icon.
/// </summary>
[DllImport("USER32.DLL", EntryPoint = "CreateWindowExW", SetLastError = true)]
[DllImport(User32, EntryPoint = "CreateWindowExW", SetLastError = true)]
public static extern IntPtr CreateWindowEx(int dwExStyle, [MarshalAs(UnmanagedType.LPWStr)] string lpClassName,
[MarshalAs(UnmanagedType.LPWStr)] string lpWindowName, int dwStyle, int x, int y,
int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance,
@@ -28,21 +30,21 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// <summary>
/// Processes a default windows procedure.
/// </summary>
[DllImport("USER32.DLL")]
[DllImport(User32)]
public static extern IntPtr DefWindowProc(IntPtr hWnd, uint msg, IntPtr wparam, IntPtr lparam);
/// <summary>
/// Registers the helper window class.
/// </summary>
[DllImport("USER32.DLL", EntryPoint = "RegisterClassW", SetLastError = true)]
[DllImport(User32, EntryPoint = "RegisterClassW", SetLastError = true)]
public static extern short RegisterClass(ref WindowClass lpWndClass);
/// <summary>
/// Registers a listener for a window message.
/// </summary>
/// <param name="lpString"></param>
/// <returns></returns>
[DllImport("User32.Dll", EntryPoint = "RegisterWindowMessageW")]
/// <returns>uint</returns>
[DllImport(User32, EntryPoint = "RegisterWindowMessageW")]
public static extern uint RegisterWindowMessage([MarshalAs(UnmanagedType.LPWStr)] string lpString);
/// <summary>
@@ -50,8 +52,8 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// taskbar icon.
/// </summary>
/// <param name="hWnd"></param>
/// <returns></returns>
[DllImport("USER32.DLL", SetLastError = true)]
/// <returns>bool</returns>
[DllImport(User32, SetLastError = true)]
public static extern bool DestroyWindow(IntPtr hWnd);
@@ -59,8 +61,8 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// Gives focus to a given window.
/// </summary>
/// <param name="hWnd"></param>
/// <returns></returns>
[DllImport("USER32.DLL")]
/// <returns>bool</returns>
[DllImport(User32)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
@@ -72,18 +74,18 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// <returns>The maximum amount of time, in milliseconds, that can
/// elapse between a first click and a second click for the OS to
/// consider the mouse action a double-click.</returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
[DllImport(User32, CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int GetDoubleClickTime();
/// <summary>
/// Gets the screen coordinates of the current mouse position.
/// </summary>
[DllImport("USER32.DLL", SetLastError = true)]
[DllImport(User32, SetLastError = true)]
public static extern bool GetPhysicalCursorPos(ref Point lpPoint);
[DllImport("USER32.DLL", SetLastError = true)]
[DllImport(User32, SetLastError = true)]
public static extern bool GetCursorPos(ref Point lpPoint);
}
}

View File

@@ -7,7 +7,7 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// Callback delegate which is used by the Windows API to
/// submit window messages.
/// </summary>
public delegate IntPtr WindowProcedureHandler(IntPtr hwnd, uint uMsg, IntPtr wparam, IntPtr lparam);
public delegate IntPtr WindowProcedureHandler(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);
/// <summary>

View File

@@ -131,7 +131,7 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// pointer rather than a real window handler.<br/>
/// Used at design time.
/// </summary>
/// <returns></returns>
/// <returns>WindowMessageSink</returns>
internal static WindowMessageSink CreateEmpty()
{
return new WindowMessageSink
@@ -169,7 +169,7 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
wc.hIcon = IntPtr.Zero;
wc.hCursor = IntPtr.Zero;
wc.hbrBackground = IntPtr.Zero;
wc.lpszMenuName = "";
wc.lpszMenuName = string.Empty;
wc.lpszClassName = WindowId;
// Register the window class
@@ -185,11 +185,7 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
if (MessageWindowHandle == IntPtr.Zero)
{
#if SILVERLIGHT
throw new Exception("Message window handle was not a valid pointer.");
#else
throw new Win32Exception("Message window handle was not a valid pointer");
#endif
}
}
@@ -200,20 +196,20 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// <summary>
/// Callback method that receives messages from the taskbar area.
/// </summary>
private IntPtr OnWindowMessageReceived(IntPtr hwnd, uint messageId, IntPtr wparam, IntPtr lparam)
private IntPtr OnWindowMessageReceived(IntPtr hWnd, uint messageId, IntPtr wParam, IntPtr lParam)
{
if (messageId == taskbarRestartMessageId)
{
//recreate the icon if the taskbar was restarted (e.g. due to Win Explorer shutdown)
var listener = TaskbarCreated;
if(listener != null) listener();
listener?.Invoke();
}
//forward message
ProcessWindowMessage(messageId, wparam, lparam);
ProcessWindowMessage(messageId, wParam, lParam);
// Pass the message to the default window procedure
return WinApi.DefWindowProc(hwnd, messageId, wparam, lparam);
return WinApi.DefWindowProc(hWnd, messageId, wParam, lParam);
}
@@ -278,13 +274,13 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
case 0x402:
var listener = BalloonToolTipChanged;
if (listener != null) listener(true);
listener?.Invoke(true);
break;
case 0x403:
case 0x404:
listener = BalloonToolTipChanged;
if (listener != null) listener(false);
listener?.Invoke(false);
break;
case 0x405:
@@ -293,12 +289,12 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
case 0x406:
listener = ChangeToolTipStateRequest;
if (listener != null) listener(true);
listener?.Invoke(true);
break;
case 0x407:
listener = ChangeToolTipStateRequest;
if (listener != null) listener(false);
listener?.Invoke(false);
break;
default:
@@ -328,7 +324,7 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// Therefore, you should call GC.SuppressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
@@ -340,7 +336,7 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// method does not get called. This gives this base class the
/// opportunity to finalize.
/// <para>
/// Important: Do not provide destructors in types derived from
/// Important: Do not provide destructor in types derived from
/// this class.
/// </para>
/// </summary>