Skip to content

Commit 54a0d2b

Browse files
committed
Display Meesage at the top of the external window
1 parent d2e1e36 commit 54a0d2b

File tree

4 files changed

+98
-28
lines changed

4 files changed

+98
-28
lines changed

src/WPFDevelopers.Shared/Controls/Message/Message.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,22 @@ static void CreateMessageAdorner(Window owner = null, string message = null, Mes
4040
throw;
4141
}
4242
}
43+
4344
public static void Push(this Window owner, string message, MessageBoxImage type = MessageBoxImage.Information, bool center = false)
4445
{
4546
CreateMessageAdorner(owner, message, type, center);
4647
}
48+
4749
public static void Push(string message, MessageBoxImage type = MessageBoxImage.Information, bool center = false)
4850
{
4951
CreateMessageAdorner(message: message, type: type, center: center);
5052
}
53+
public static void Push(IntPtr intPtr, string message, MessageBoxImage type = MessageBoxImage.Information, bool center = false)
54+
{
55+
PushDesktop(message, type, center, intPtr);
56+
}
5157

52-
public static void PushDesktop(string message, MessageBoxImage type = MessageBoxImage.Information, bool center = false)
58+
public static void PushDesktop(string message, MessageBoxImage type = MessageBoxImage.Information, bool center = false, IntPtr intPtr = default)
5359
{
5460
if (_messageExt == null)
5561
{
@@ -63,13 +69,15 @@ public static void PushDesktop(string message, MessageBoxImage type = MessageBox
6369
_messageExt.IsPosition = false;
6470
_messageExt.Position = _position;
6571
}
66-
_messageExt.Push(message, type, center);
72+
_messageExt.Push(message, type, center, intPtr);
6773
}
74+
6875
public static void SetPosition(Position position = Position.Top)
6976
{
7077
if (_position != position)
7178
_position = position;
7279
}
80+
7381
public static void Clear()
7482
{
7583
if(_messageAdorner != null)

src/WPFDevelopers.Shared/Controls/Message/MessageExt.cs

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Windows;
33
using System.Windows.Media;
4+
using WPFDevelopers.Helpers;
45

56
namespace WPFDevelopers.Controls
67
{
@@ -30,43 +31,76 @@ public MessageExt()
3031
}
3132

3233
}
33-
34-
internal void Push(string message, MessageBoxImage type = MessageBoxImage.Information, bool center = false)
34+
35+
internal void Push(string message, MessageBoxImage type = MessageBoxImage.Information,
36+
bool center = false, IntPtr intPtr = default)
3537
{
36-
var desktopWorkingArea = SystemParameters.WorkArea;
37-
var item = new MessageListBoxItem { Content = message, MessageType = type, IsCenter = center };
38-
_listBox.Items.Insert(0, item);
38+
Rect targetArea = GetTargetArea(intPtr);
39+
var messageItem = new MessageListBoxItem
40+
{
41+
Content = message,
42+
MessageType = type,
43+
IsCenter = center
44+
};
45+
_listBox.Items.Insert(0, messageItem);
3946
if (!IsPosition || Position == Position.Bottom)
4047
{
41-
double x = 0;
42-
double y = 0;
43-
switch (Position)
44-
{
45-
case Position.Top:
46-
x = (desktopWorkingArea.Right - item.Width) / 2;
47-
break;
48-
case Position.Right:
49-
x = desktopWorkingArea.Right - (item.Width + item.Margin.Right + item.Padding.Right);
50-
break;
51-
case Position.Bottom:
52-
x = (desktopWorkingArea.Right - item.Width) / 2;
53-
_listBox.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
54-
var controlHeight = _listBox.DesiredSize.Height;
55-
y = desktopWorkingArea.Height - controlHeight;
56-
break;
57-
}
58-
Height = desktopWorkingArea.Height;
59-
Left = x;
60-
Top = y;
48+
CalculateAndSetPosition(targetArea, messageItem);
6149
IsPosition = true;
6250
}
6351
}
52+
53+
private Rect GetTargetArea(IntPtr windowHandle)
54+
{
55+
if (windowHandle != IntPtr.Zero)
56+
{
57+
IsPosition = false;
58+
return WindowHelpers.GetWindowBounds(windowHandle);
59+
}
60+
return SystemParameters.WorkArea;
61+
}
62+
63+
private void CalculateAndSetPosition(Rect targetArea, MessageListBoxItem item)
64+
{
65+
double x = 0;
66+
double y = 0;
67+
68+
switch (Position)
69+
{
70+
case Position.Left:
71+
x = targetArea.Left + item.Margin.Left;
72+
y = targetArea.Top;
73+
break;
74+
75+
case Position.Top:
76+
x = targetArea.Left + (targetArea.Width - item.Width) / 2;
77+
y = targetArea.Top;
78+
break;
79+
80+
case Position.Right:
81+
x = targetArea.Right - (item.Width + item.Margin.Right + item.Padding.Right);
82+
y = targetArea.Top;
83+
break;
84+
85+
case Position.Bottom:
86+
x = targetArea.Left + (targetArea.Width - item.Width) / 2;
87+
_listBox.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
88+
y = targetArea.Bottom - _listBox.DesiredSize.Height;
89+
break;
90+
}
91+
92+
Height = targetArea.Height;
93+
Left = x;
94+
Top = y;
95+
}
96+
6497
internal void Clear()
6598
{
6699
if(_listBox!=null)
67100
_listBox.Items.Clear();
68101
Close();
69102
}
103+
70104
private void ListBox_SizeChanged(object sender, SizeChangedEventArgs e)
71105
{
72106
if(_listBox.ActualHeight <= 10)

src/WPFDevelopers.Shared/Core/Helpers/Win32.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public const string
1717
NTdll = "ntdll.dll",
1818
DwmApi = "dwmapi.dll",
1919
Winmm = "winmm.dll",
20-
Shcore = "Shcore.dll";
20+
Shcore = "shcore.dll";
2121
//查找窗口的委托 查找逻辑
2222
public delegate bool EnumWindowsProc(IntPtr hwnd, IntPtr lParam);
2323

@@ -192,6 +192,20 @@ public static bool EnableDarkModeForWindow(HwndSource source, bool enable)
192192
[DllImport(DwmApi)]
193193
public static extern int DwmInvalidateIconicBitmaps(IntPtr hwnd);
194194

195+
[DllImport(User32)]
196+
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
197+
198+
[DllImport(User32)]
199+
public static extern int GetDpiForWindow(IntPtr hwnd);
200+
201+
[StructLayout(LayoutKind.Sequential)]
202+
public struct RECT
203+
{
204+
public int Left;
205+
public int Top;
206+
public int Right;
207+
public int Bottom;
208+
}
195209
}
196210

197211
internal class WindowsMessageCodes

src/WPFDevelopers.Shared/Core/Helpers/WindowHelpers.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Windows;
66
using System.Windows.Interop;
77
using System.Windows.Media;
8+
using System.Windows.Threading;
89
using WPFDevelopers.Controls;
910
using WPFDevelopers.Utilities;
1011
using Size = System.Drawing.Size;
@@ -95,5 +96,18 @@ public static void SetIconicThumbnail(this Window window, string imagePath)
9596
Win32.DwmInvalidateIconicBitmaps(hwnd);
9697
window.ShowInTaskbar = true;
9798
}
99+
100+
public static Rect GetWindowBounds(IntPtr hWnd)
101+
{
102+
Win32.RECT rect;
103+
Win32.GetWindowRect(hWnd, out rect);
104+
int dpi = Win32.GetDpiForWindow(hWnd);
105+
double scale = dpi / 96.0;
106+
return new Rect(
107+
rect.Left / scale,
108+
rect.Top / scale,
109+
(rect.Right - rect.Left) / scale,
110+
(rect.Bottom - rect.Top) / scale);
111+
}
98112
}
99113
}

0 commit comments

Comments
 (0)