/// <summary> /// Tries to get the adjacent location to NotifyIcon using specified window width and height. /// </summary> /// <param name="windowWidth">Window width</param> /// <param name="windowHeight">Window height</param> /// <param name="location">Location of window</param> /// <returns>True if succeeded</returns> protected bool TryGetAdjacentLocationToTaskbar(double windowWidth, double windowHeight, out Point location) { if (!WindowHelper.TryGetTaskbar(out Rect taskbarRect, out TaskbarAlignment taskbarAlignment)) { location = default(Point); return(false); } if (!NotifyIconHelper.TryGetNotifyIconRect(_notifyIcon, out Rect iconRect)) { iconRect = taskbarRect; // Fallback } if (!WindowHelper.TryGetDwmWindowMargin(_window, out Thickness windowMargin)) { windowMargin = new Thickness(0); // Fallback } double x = 0; double y = 0; switch (taskbarAlignment) { case TaskbarAlignment.Top: case TaskbarAlignment.Bottom: x = iconRect.Right - windowWidth + windowMargin.Right; switch (taskbarAlignment) { case TaskbarAlignment.Top: y = taskbarRect.Bottom - windowMargin.Top; PivotAlignment = PivotAlignment.TopRight; break; case TaskbarAlignment.Bottom: y = taskbarRect.Top - windowHeight + windowMargin.Bottom; PivotAlignment = PivotAlignment.BottomRight; break; } break; case TaskbarAlignment.Left: case TaskbarAlignment.Right: switch (taskbarAlignment) { case TaskbarAlignment.Left: x = taskbarRect.Right - windowMargin.Left; PivotAlignment = PivotAlignment.BottomLeft; break; case TaskbarAlignment.Right: x = taskbarRect.Left - windowWidth + windowMargin.Right; PivotAlignment = PivotAlignment.BottomRight; break; } y = iconRect.Bottom - windowHeight + windowMargin.Bottom; break; } location = new Point(x, y); return(true); }
/// <summary> /// Tries to get the adjacent location to NotifyIcon using specified window width and height. /// </summary> /// <param name="windowWidth">Window width</param> /// <param name="windowHeight">Window height</param> /// <param name="location">Location of window</param> /// <returns>True if succeeded</returns> protected bool TryGetAdjacentLocationToTaskbar(double windowWidth, double windowHeight, out Point location) { if (!WindowHelper.TryGetTaskbar(out Rect taskbarRect, out TaskbarAlignment taskbarAlignment)) { location = default(Point); return(false); } if (!NotifyIconHelper.TryGetNotifyIconRect(_notifyIcon, out Rect iconRect)) { iconRect = taskbarRect; // Fallback } var margin = WindowHelper.GetDwmWindowMargin(_window); if (margin == Padding.Empty) { margin = new Padding(0); // Fallback } double x = 0; double y = 0; switch (taskbarAlignment) { case TaskbarAlignment.Top: case TaskbarAlignment.Bottom: x = iconRect.Right - windowWidth + margin.Right; switch (taskbarAlignment) { case TaskbarAlignment.Top: y = taskbarRect.Bottom - margin.Top; break; case TaskbarAlignment.Bottom: y = taskbarRect.Top - windowHeight + margin.Bottom; break; } break; case TaskbarAlignment.Left: case TaskbarAlignment.Right: switch (taskbarAlignment) { case TaskbarAlignment.Left: x = taskbarRect.Right - margin.Left; break; case TaskbarAlignment.Right: x = taskbarRect.Left - windowWidth + margin.Right; break; } y = iconRect.Bottom - windowHeight + margin.Bottom; break; } location = new Point(x, y); return(true); }
/// <summary> /// Attempts to get the adjacent location to NotifyIcon using specified window width and height. /// </summary> /// <param name="windowWidth">Window width</param> /// <param name="windowHeight">Window height</param> /// <param name="location">Location of window</param> /// <returns>True if successfully gets</returns> protected bool TryGetAdjacentLocationToTaskbar(double windowWidth, double windowHeight, out Rect location) { if (!WindowHelper.TryGetTaskbar(out Rect taskbarRect, out TaskbarAlignment taskbarAlignment)) { location = default; return(false); } var iconPlacement = IconPlacement.Unknown; var overflowAreaRect = default(Rect); if (NotifyIconHelper.TryGetNotifyIconRect(_notifyIcon, out Rect iconRect)) { if (taskbarRect.Contains(iconRect)) { iconPlacement = IconPlacement.InTaskbar; } else if (WindowHelper.TryGetOverflowAreaRect(out overflowAreaRect)) { iconPlacement = IconPlacement.InOverflowArea; } } if (!WindowHelper.TryGetDwmWindowMargin(_window, out Thickness windowMargin)) { windowMargin = new Thickness(0); // Fallback } double x = 0, y = 0; switch (taskbarAlignment) { case TaskbarAlignment.Top: case TaskbarAlignment.Bottom: x = iconPlacement switch { IconPlacement.InTaskbar => iconRect.Right, IconPlacement.InOverflowArea => overflowAreaRect.Left, _ => taskbarRect.Right, // Fallback }; x -= (windowWidth - windowMargin.Right); switch (taskbarAlignment) { case TaskbarAlignment.Top: y = taskbarRect.Bottom - windowMargin.Top; PivotAlignment = PivotAlignment.TopRight; break; case TaskbarAlignment.Bottom: y = taskbarRect.Top - (windowHeight - windowMargin.Bottom); PivotAlignment = PivotAlignment.BottomRight; break; } break; case TaskbarAlignment.Left: case TaskbarAlignment.Right: switch (taskbarAlignment) { case TaskbarAlignment.Left: x = taskbarRect.Right - windowMargin.Left; PivotAlignment = PivotAlignment.BottomLeft; break; case TaskbarAlignment.Right: x = taskbarRect.Left - (windowWidth - windowMargin.Right); PivotAlignment = PivotAlignment.BottomRight; break; } y = iconPlacement switch { IconPlacement.InTaskbar => iconRect.Bottom, IconPlacement.InOverflowArea => overflowAreaRect.Top, _ => taskbarRect.Bottom, // Fallback }; y -= (windowHeight - windowMargin.Bottom); break; } location = new Rect(x, y, windowWidth, windowHeight); return(true); }
/// <summary> /// Attempts to get the adjacent location to NotifyIcon using specified window width and height. /// </summary> /// <param name="windowWidth">Window width</param> /// <param name="windowHeight">Window height</param> /// <param name="location">Location of window</param> /// <returns>True if successfully gets</returns> protected bool TryGetAdjacentLocationToTaskbar(double windowWidth, double windowHeight, out Rect location) { if (!WindowHelper.TryGetTaskbar(out Rect taskbarRect, out TaskbarAlignment taskbarAlignment, out bool isShown)) { location = default; return(false); } var iconPlacement = IconPlacement.Unknown; var overflowAreaRect = default(Rect); if (NotifyIconHelper.TryGetNotifyIconRect(_notifyIcon, out Rect iconRect)) { if (taskbarRect.Contains( iconRect.X + iconRect.Width / 2D, iconRect.Y + iconRect.Height / 2D)) { iconPlacement = IconPlacement.InTaskbar; } else if (WindowHelper.TryGetOverflowAreaRect(out overflowAreaRect) && overflowAreaRect.Contains(iconRect)) { iconPlacement = IconPlacement.InOverflowArea; } } if (!WindowHelper.TryGetDwmWindowMargin(_window, out Thickness windowMargin)) { windowMargin = new Thickness(0); // Fallback } var isLeftToRight = !CultureInfoAddition.UserDefaultUICulture.TextInfo.IsRightToLeft; var distance = KeepsDistance ? new Vector(Distance, Distance) * VisualTreeHelperAddition.GetDpi(_window).ToMatrix() : new Vector(0, 0); double x = 0, y = 0; // To avoid a gap between window and taskbar when taskbar alignment is right or bottom // and monitor DPI is 125%, 150%, 175%, the window width and height (in DIP) must be // a multiple of 4. Otherwise, the window width and height multiplied with those DPI // will have a fraction and it will cause a blurry edge looking as if there is a gap. switch (taskbarAlignment) { case TaskbarAlignment.Top: case TaskbarAlignment.Bottom: x = iconPlacement switch { IconPlacement.InTaskbar => isLeftToRight ? iconRect.Right : iconRect.Left, IconPlacement.InOverflowArea => isLeftToRight ? (overflowAreaRect.Left - distance.X) : (overflowAreaRect.Right + distance.X), _ => isLeftToRight ? (taskbarRect.Right - distance.X) : (taskbarRect.Left + distance.X), // Fallback }; x -= isLeftToRight ? (windowWidth - windowMargin.Right) : windowMargin.Left; switch (taskbarAlignment) { case TaskbarAlignment.Top: y = (isShown ? taskbarRect.Bottom : taskbarRect.Top) - windowMargin.Top + distance.Y; PivotAlignment = isLeftToRight ? PivotAlignment.TopRight : PivotAlignment.TopLeft; break; case TaskbarAlignment.Bottom: y = (isShown ? taskbarRect.Top : taskbarRect.Bottom) - (windowHeight - windowMargin.Bottom) - distance.Y; PivotAlignment = isLeftToRight ? PivotAlignment.BottomRight : PivotAlignment.BottomLeft; break; } break; case TaskbarAlignment.Left: case TaskbarAlignment.Right: switch (taskbarAlignment) { case TaskbarAlignment.Left: x = (isShown ? taskbarRect.Right : taskbarRect.Left) - windowMargin.Left + distance.X; PivotAlignment = PivotAlignment.BottomLeft; break; case TaskbarAlignment.Right: x = (isShown ? taskbarRect.Left : taskbarRect.Right) - (windowWidth - windowMargin.Right) - distance.X; PivotAlignment = PivotAlignment.BottomRight; break; } y = iconPlacement switch { IconPlacement.InTaskbar => iconRect.Bottom, IconPlacement.InOverflowArea => overflowAreaRect.Top - distance.Y, _ => taskbarRect.Bottom - distance.Y, // Fallback }; y -= (windowHeight - windowMargin.Bottom); break; } location = new Rect(x, y, windowWidth, windowHeight); return(true); }