-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloatingWindow.xaml.cs
168 lines (142 loc) · 5.02 KB
/
FloatingWindow.xaml.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
namespace WinMediaPie
{
/// <summary>
/// Logika interakcji dla klasy PieWindow.xaml
/// </summary>
public partial class FloatingWindow : Window
{
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern IntPtr SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr window, int index, int value);
[DllImport("user32.dll")]
public static extern int GetWindowLong(IntPtr window, int index);
private const int WM_SYSCOMMAND = 0x0112;
private const int SC_MINIMIZE = 0xf020;
private const int SC_MAXIMIZE = 0xf030;
private const int SC_MOVE = 0xF010;
private const int SC_SIZE = 0xF000;
const int GWL_EXSTYLE = -20;
const int WS_EX_TOOLWINDOW = 0x00000080;
const int WS_EX_APPWINDOW = 0x00040000;
private PieWindow pieWindow;
private bool IsBeingDisplayed = false;
private IntPtr PreviousForegroundWindow;
/// <summary>
/// Floating window that is an activator and wrapper for PieWindow
/// </summary>
public FloatingWindow()
{
PreviousForegroundWindow = GetForegroundWindow();
InitializeComponent();
this.ShowInTaskbar = false;
this.pieWindow = new PieWindow(this.ShowFloatingWindow);
this.ShowSelfOrPie();
}
internal void ShowSelfOrPie()
{
if (this.pieWindow.ShouldBeDisplayedInitially())
{
this.ShowPie();
}
else
{
this.ShowFloatingWindow();
}
}
/// <summary>
/// Hides all descendant windows
/// </summary>
public void HideAllWindows()
{
this.ShowFloatingWindow();
}
/// <summary>
/// Hides the PieWindow and shows this floating window
/// </summary>
private void ShowFloatingWindow()
{
SetForegroundWindow(PreviousForegroundWindow);
this.pieWindow.Hide();
this.Show();
System.Drawing.Rectangle workArea = Common.Helpers.WindowHelpers.CurrentScreen(this).Bounds;
this.Top = workArea.Height * 0.5 - this.Height / 2;
this.Left = workArea.Width - this.Width;
this.IsBeingDisplayed = true;
}
protected override void OnMouseEnter(System.Windows.Input.MouseEventArgs e)
{
base.OnMouseEnter(e);
this.ShowPie();
}
/// <summary>
/// Hides this floating window and shows the PieWindow
/// </summary>
void ShowPie()
{
PreviousForegroundWindow = GetForegroundWindow();
this.pieWindow.WindowState = WindowState.Minimized;
this.pieWindow.Show();
this.pieWindow.WindowState = WindowState.Normal;
this.pieWindow.BringIntoView();
this.pieWindow.Focus();
HideSelf();
}
void HideSelf()
{
this.Hide();
this.IsBeingDisplayed = false;
}
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
base.OnClosing(e);
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
source.AddHook(WndProc);
// Make it gone frmo ALT+TAB
int windowStyle = GetWindowLong(source.Handle, GWL_EXSTYLE);
SetWindowLong(source.Handle, GWL_EXSTYLE, windowStyle | WS_EX_TOOLWINDOW);
}
// Makes the window unmovable, neither maxibizable nor minimalizable & unresizable
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_SYSCOMMAND)
{
int wParamInt = wParam.ToInt32();
if (wParamInt == SC_MINIMIZE || wParamInt == SC_MAXIMIZE || wParamInt == SC_MOVE || wParamInt == SC_SIZE)
{
handled = true;
}
}
return IntPtr.Zero;
}
private void TogglePie(object sender, TouchEventArgs e)
{
this.TogglePie();
}
private void TogglePie(object sender, System.Windows.Input.MouseEventArgs e)
{
this.TogglePie();
}
/// <summary>
/// Toggles the visibility of PieWindow
/// </summary>
private void TogglePie()
{
if (this.IsBeingDisplayed) this.ShowPie(); else this.ShowFloatingWindow();
this.IsBeingDisplayed = !this.IsBeingDisplayed;
}
}
}