Skip to content
This repository was archived by the owner on Dec 18, 2023. It is now read-only.

Commit 777e96b

Browse files
committed
add trayicon baseon notifyicon
1 parent 1de0cb0 commit 777e96b

File tree

18 files changed

+6312
-114
lines changed

18 files changed

+6312
-114
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,12 @@ dotnet_diagnostic.CS8632.severity = none
4141

4242
# CS8604: 可能的 null 引用参数。
4343
dotnet_diagnostic.CS8604.severity = none
44+
45+
# CS3003: 类型不符合 CLS
46+
dotnet_diagnostic.CS3003.severity = none
47+
48+
# CS3009: 基类型不符合 CLS
49+
dotnet_diagnostic.CS3009.severity = none
50+
51+
# CS3001: 参数类型不符合 CLS
52+
dotnet_diagnostic.CS3001.severity = none

CookPopularCSharpToolkit/Communal/Extensions/ImageBitmapExtension.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Windows.Interop;
99
using System.Windows.Media;
1010
using System.Windows.Media.Imaging;
11+
using System.Windows.Resources;
1112

1213

1314

@@ -74,6 +75,23 @@ public static Icon ToIcon(this Bitmap bitmap, System.Drawing.Size size)
7475
}
7576
}
7677

78+
public static Icon ToIcon(this ImageSource imageSource)
79+
{
80+
if (imageSource == null) return null;
81+
82+
Uri uri = new Uri(imageSource.ToString());
83+
StreamResourceInfo streamInfo = Application.GetResourceStream(uri);
84+
85+
if (streamInfo == null)
86+
{
87+
string msg = "The supplied image source '{0}' could not be resolved.";
88+
msg = string.Format(msg, imageSource);
89+
throw new ArgumentException(msg);
90+
}
91+
92+
return new Icon(streamInfo.Stream);
93+
}
94+
7795
/// <summary>
7896
/// 转换Image为Icon
7997
/// </summary>

CookPopularCSharpToolkit/Windows/Extensions/WindowExtension.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ internal struct FLASHWINFO
134134
[DllImport("user32.dll", SetLastError = true)]
135135
internal static extern int GetWindowLong(IntPtr hWnd, int nIndex);
136136

137-
[DllImport("user32.dll")]
138-
internal static extern bool SetForegroundWindow(IntPtr hWnd);
137+
//[DllImport("user32.dll")]
138+
//internal static extern bool SetForegroundWindow(IntPtr hWnd);
139139

140140
[DllImport("user32.dll", SetLastError = true)]
141141
internal static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
@@ -237,7 +237,7 @@ public static bool SafeActivate(this Window window)
237237
{
238238
try
239239
{
240-
return NativeMethods.SetForegroundWindow(window.EnsureHandle());
240+
return InteropMethods.SetForegroundWindow(window.EnsureHandle());
241241
}
242242
catch (Exception)
243243
{
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Windows;
3+
4+
namespace CookPopularCSharpToolkit.Windows
5+
{
6+
/// <summary>
7+
/// Helper class used by routed events of the
8+
/// <see cref="TaskbarIcon"/> class.
9+
/// </summary>
10+
public static class RoutedEventHelper
11+
{
12+
/// <summary>
13+
/// A static helper method to raise a routed event on a target UIElement or ContentElement.
14+
/// </summary>
15+
/// <param name="target">UIElement or ContentElement on which to raise the event</param>
16+
/// <param name="args">RoutedEventArgs to use when raising the event</param>
17+
internal static void RaiseEvent(DependencyObject target, RoutedEventArgs args)
18+
{
19+
if (target is UIElement uiElement)
20+
{
21+
uiElement.RaiseEvent(args);
22+
}
23+
else if (target is ContentElement contentElement)
24+
{
25+
contentElement.RaiseEvent(args);
26+
}
27+
}
28+
29+
/// <summary>
30+
/// A static helper method that adds a handler for a routed event
31+
/// to a target UIElement or ContentElement.
32+
/// </summary>
33+
/// <param name="element">UIElement or ContentElement that listens to the event</param>
34+
/// <param name="routedEvent">Event that will be handled</param>
35+
/// <param name="handler">Event handler to be added</param>
36+
internal static void AddHandler(DependencyObject element, RoutedEvent routedEvent, Delegate handler)
37+
{
38+
if (element is UIElement uie)
39+
{
40+
uie.AddHandler(routedEvent, handler);
41+
}
42+
else if (element is ContentElement ce)
43+
{
44+
ce.AddHandler(routedEvent, handler);
45+
}
46+
}
47+
48+
/// <summary>
49+
/// A static helper method that removes a handler for a routed event
50+
/// from a target UIElement or ContentElement.
51+
/// </summary>
52+
/// <param name="element">UIElement or ContentElement that listens to the event</param>
53+
/// <param name="routedEvent">Event that will no longer be handled</param>
54+
/// <param name="handler">Event handler to be removed</param>
55+
internal static void RemoveHandler(DependencyObject element, RoutedEvent routedEvent, Delegate handler)
56+
{
57+
if (element is UIElement uie)
58+
{
59+
uie.RemoveHandler(routedEvent, handler);
60+
}
61+
else if (element is ContentElement ce)
62+
{
63+
ce.RemoveHandler(routedEvent, handler);
64+
}
65+
}
66+
}
67+
}

CookPopularCSharpToolkit/Windows/Interop/InteropMethods.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ internal class InteropMethods
2121

2222
internal static readonly IntPtr HRGN_NONE = new IntPtr(-1);
2323

24-
[DllImport(InteropValues.ExternDll.User32, CharSet = CharSet.Auto)]
24+
[DllImport(InteropValues.ExternDll.User32, EntryPoint = "RegisterWindowMessageW", CharSet = CharSet.Auto)]
2525
[ResourceExposure(ResourceScope.None)]
26-
internal static extern int RegisterWindowMessage(string msg);
26+
internal static extern uint RegisterWindowMessage([MarshalAs(UnmanagedType.LPWStr)] string msg);
2727

2828
[DllImport(InteropValues.ExternDll.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
2929
internal static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, out InteropValues.TBBUTTON lpBuffer,
@@ -170,6 +170,9 @@ internal static BitmapHandle CreateDIBSection(HandleRef hdc, ref InteropValues.B
170170
[DllImport(InteropValues.ExternDll.Kernel32, EntryPoint = "CloseHandle", CharSet = CharSet.Auto, SetLastError = true)]
171171
internal static extern bool IntCloseHandle(HandleRef handle);
172172

173+
[DllImport(InteropValues.ExternDll.User32, CharSet = CharSet.Auto, ExactSpelling = true)]
174+
public static extern int GetDoubleClickTime();
175+
173176
[SecurityCritical]
174177
[SuppressUnmanagedCodeSecurity]
175178
[DllImport(InteropValues.ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto,
@@ -234,11 +237,13 @@ internal static string GetModuleFileName(HandleRef hModule)
234237
[SecurityCritical]
235238
[SuppressUnmanagedCodeSecurity]
236239
[DllImport(InteropValues.ExternDll.Shell32, CharSet = CharSet.Auto, BestFitMapping = false, ThrowOnUnmappableChar = true)]
237-
internal static extern int ExtractIconEx(string szExeFileName, int nIconIndex, out IconHandle phiconLarge,
238-
out IconHandle phiconSmall, int nIcons);
240+
internal static extern int ExtractIconEx(string szExeFileName, int nIconIndex, out IconHandle phiconLarge, out IconHandle phiconSmall, int nIcons);
239241

240242
[DllImport(InteropValues.ExternDll.Shell32, CharSet = CharSet.Auto)]
241-
internal static extern int Shell_NotifyIcon(int message, InteropValues.NOTIFYICONDATA pnid);
243+
internal static extern bool Shell_NotifyIcon(int message, [In] ref InteropValues.NOTIFYICONDATA pnid);
244+
245+
[DllImport(InteropValues.ExternDll.User32, SetLastError = true)]
246+
internal static extern bool GetPhysicalCursorPos(ref InteropValues.POINT lpPoint);
242247

243248
[SecurityCritical]
244249
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
@@ -263,7 +268,7 @@ internal static extern IntPtr CreateWindowEx(
263268
internal static extern short RegisterClass(InteropValues.WNDCLASS4ICON wc);
264269

265270
[DllImport(InteropValues.ExternDll.User32, CharSet = CharSet.Auto)]
266-
internal static extern IntPtr DefWindowProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
271+
internal static extern IntPtr DefWindowProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
267272

268273
[DllImport(InteropValues.ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)]
269274
internal static extern bool SetForegroundWindow(IntPtr hWnd);

CookPopularCSharpToolkit/Windows/Interop/NativeMethods.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ internal struct RECT
5656

5757
internal const int WS_EX_NOACTIVATE = 0x08000000;
5858

59-
[DllImport("user32.dll", SetLastError = true)]
60-
internal static extern bool DestroyWindow(IntPtr hWnd);
59+
//[DllImport("user32.dll", SetLastError = true)]
60+
//internal static extern bool DestroyWindow(IntPtr hWnd);
6161

6262
[DllImport("user32.dll", SetLastError = true)]
6363
internal static extern uint GetWindowLong(IntPtr hWnd, int nIndex);
@@ -84,8 +84,8 @@ internal struct RECT
8484
[DllImport("user32.dll", SetLastError = true)]
8585
internal static extern IntPtr GetForegroundWindow();
8686

87-
[DllImport("user32.dll", SetLastError = true)]
88-
internal static extern bool SetForegroundWindow(IntPtr hWnd);
87+
//[DllImport("user32.dll", SetLastError = true)]
88+
//internal static extern bool SetForegroundWindow(IntPtr hWnd);
8989

9090
[DllImport("User32")]
9191
internal static extern IntPtr GetTopWindow(IntPtr hWnd);

0 commit comments

Comments
 (0)