-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathStatusBarInjector.cs
105 lines (82 loc) · 1.98 KB
/
StatusBarInjector.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace Lkytal.StatusInfo
{
internal class StatusBarInjector
{
private readonly Window mainWindow;
private FrameworkElement statusBar;
private Panel panel;
public StatusBarInjector(Window pMainWindow)
{
mainWindow = pMainWindow;
mainWindow.Initialized += MainWindowInitialized;
FindStatusBar();
}
private static DependencyObject FindChild(DependencyObject parent, string childName)
{
if (parent == null)
{
return null;
}
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
if (child is FrameworkElement frameworkElement && frameworkElement.Name == childName)
{
return frameworkElement;
}
child = FindChild(child, childName);
if (child != null)
{
return child;
}
}
return null;
}
private void FindStatusBar()
{
statusBar = FindChild(mainWindow, "StatusBarContainer") as FrameworkElement;
var frameworkElement = statusBar;
if (frameworkElement != null)
{
panel = frameworkElement.Parent as DockPanel;
}
}
private void RefindStatusBar()
{
if (panel == null)
{
FindStatusBar();
}
}
public void InjectControl(FrameworkElement pControl)
{
RefindStatusBar();
panel?.Dispatcher.Invoke(() => {
pControl.SetValue(DockPanel.DockProperty, Dock.Right);
panel.Children.Insert(1, pControl);
});
}
public bool IsInjected(FrameworkElement pControl)
{
RefindStatusBar();
var flag = false;
panel?.Dispatcher.Invoke(() => {
flag = panel.Children.Contains(pControl);
});
return flag;
}
public void UninjectControl(FrameworkElement pControl)
{
RefindStatusBar();
panel?.Dispatcher.Invoke(() => panel.Children.Remove(pControl));
}
private void MainWindowInitialized(object sender, EventArgs e)
{
}
}
}