Skip to content

Commit

Permalink
Fix tray on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Redth committed Jan 14, 2022
1 parent c5c0716 commit e2fb960
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 34 deletions.
12 changes: 8 additions & 4 deletions src/WeatherTwentyOne/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ public static MauiApp CreateMauiApp()
});
builder.ConfigureLifecycleEvents(lifecycle => {
#if WINDOWS
lifecycle
.AddWindows(windows => windows.OnLaunched((app, args) => {
var winuiApp = (Microsoft.Maui.Controls.Window)MauiWinUIApplication.Current.Application.Windows[0];
winuiApp.SetIcon("Platforms/Windows/trayicon.ico");
lifecycle
.AddWindows(windows =>
windows.OnNativeMessage((app, args) => {
if (WindowExtensions.Hwnd == IntPtr.Zero)
{
WindowExtensions.Hwnd = args.Hwnd;
WindowExtensions.SetIcon("Platforms/Windows/trayicon.ico");
}
}));
#endif
});
Expand Down
39 changes: 12 additions & 27 deletions src/WeatherTwentyOne/Platforms/Windows/MauiWinUIWindowExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,30 @@ namespace WeatherTwentyOne;

public static class WindowExtensions
{
public static IntPtr GetNativeWindowHandle(this Microsoft.Maui.Controls.Window window)
{
var nativeWindow = window.As<IWindowNative>();
return nativeWindow.WindowHandle;
}
public static IntPtr Hwnd { get; set; }

public static void SetIcon(this Microsoft.Maui.Controls.Window window, string iconFilename)
public static void SetIcon(string iconFilename)
{
var hwnd = window.GetNativeWindowHandle();
if (Hwnd == IntPtr.Zero)
return;

var hIcon = PInvoke.User32.LoadImage(IntPtr.Zero, iconFilename,
PInvoke.User32.ImageType.IMAGE_ICON, 16, 16, PInvoke.User32.LoadImageFlags.LR_LOADFROMFILE);

PInvoke.User32.SendMessage(hwnd, PInvoke.User32.WindowMessage.WM_SETICON, (IntPtr)0, hIcon);
PInvoke.User32.SendMessage(Hwnd, PInvoke.User32.WindowMessage.WM_SETICON, (IntPtr)0, hIcon);
}

public static void BringToFront(this Microsoft.Maui.Controls.Window window)
public static void BringToFront()
{
var hwnd = window.GetNativeWindowHandle();
PInvoke.User32.ShowWindow(Hwnd, PInvoke.User32.WindowShowStyle.SW_SHOW);
PInvoke.User32.ShowWindow(Hwnd, PInvoke.User32.WindowShowStyle.SW_RESTORE);

PInvoke.User32.ShowWindow(hwnd, PInvoke.User32.WindowShowStyle.SW_SHOW);
PInvoke.User32.ShowWindow(hwnd, PInvoke.User32.WindowShowStyle.SW_RESTORE);

_ = PInvoke.User32.SetForegroundWindow(hwnd);
_ = PInvoke.User32.SetForegroundWindow(Hwnd);
}

public static void MinimizeToTray(this Microsoft.Maui.Controls.Window window)
public static void MinimizeToTray()
{
var hwnd = window.GetNativeWindowHandle();

PInvoke.User32.ShowWindow(hwnd, PInvoke.User32.WindowShowStyle.SW_MINIMIZE);
PInvoke.User32.ShowWindow(hwnd, PInvoke.User32.WindowShowStyle.SW_HIDE);
PInvoke.User32.ShowWindow(Hwnd, PInvoke.User32.WindowShowStyle.SW_MINIMIZE);
PInvoke.User32.ShowWindow(Hwnd, PInvoke.User32.WindowShowStyle.SW_HIDE);
}
}

[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("EECDBF0E-BAE9-4CB6-A68E-9598E1CB57BB")]
internal interface IWindowNative
{
IntPtr WindowHandle { get; }
}
4 changes: 1 addition & 3 deletions src/WeatherTwentyOne/Platforms/Windows/TrayService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ public void Initialize()
{
tray = new WindowsTrayIcon("Platforms/Windows/trayicon.ico");
tray.LeftClick = () => {
var winuiApp = (Microsoft.Maui.Controls.Window)MauiWinUIApplication.Current.Application.Windows[0].Handler!.NativeView!;
winuiApp.BringToFront();
//Microsoft.Maui.MauiWinUIApplication.Current.Application.Windows[0].BringToFront();
WindowExtensions.BringToFront();
ClickHandler?.Invoke();
};
}
Expand Down

0 comments on commit e2fb960

Please sign in to comment.