forked from dmnd/Caffeinated
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Taskbar.cs
130 lines (115 loc) · 3.04 KB
/
Taskbar.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
using System;
using System.Drawing;
using System.Runtime.InteropServices;
// taskbar api stuff from here:
// http://winsharp93.wordpress.com/2009/06/29/find-out-size-and-position-of-the-taskbar/
public enum TaskbarPosition {
Unknown = -1,
Left,
Top,
Right,
Bottom,
}
public sealed class Taskbar {
private const string ClassName = "Shell_TrayWnd";
public Rectangle Bounds {
get;
private set;
}
public TaskbarPosition Position {
get;
private set;
}
public Point Location {
get {
return this.Bounds.Location;
}
}
public Size Size {
get {
return this.Bounds.Size;
}
}
//Always returns false under Windows 7
public bool AlwaysOnTop {
get;
private set;
}
public bool AutoHide {
get;
private set;
}
public Taskbar() {
IntPtr taskbarHandle = User32.FindWindow(Taskbar.ClassName, null);
APPBARDATA data = new APPBARDATA();
data.cbSize = (uint)Marshal.SizeOf(typeof(APPBARDATA));
data.hWnd = taskbarHandle;
IntPtr result = Shell32.SHAppBarMessage(ABM.GetTaskbarPos, ref data);
if (result == IntPtr.Zero)
throw new InvalidOperationException();
this.Position = (TaskbarPosition)data.uEdge;
this.Bounds = Rectangle.FromLTRB(
data.rc.left,
data.rc.top,
data.rc.right,
data.rc.bottom
);
data.cbSize = (uint)Marshal.SizeOf(typeof(APPBARDATA));
result = Shell32.SHAppBarMessage(ABM.GetState, ref data);
int state = result.ToInt32();
this.AlwaysOnTop = (state & ABS.AlwaysOnTop) == ABS.AlwaysOnTop;
this.AutoHide = (state & ABS.Autohide) == ABS.Autohide;
}
}
public enum ABM : uint {
New = 0x00000000,
Remove = 0x00000001,
QueryPos = 0x00000002,
SetPos = 0x00000003,
GetState = 0x00000004,
GetTaskbarPos = 0x00000005,
Activate = 0x00000006,
GetAutoHideBar = 0x00000007,
SetAutoHideBar = 0x00000008,
WindowPosChanged = 0x00000009,
SetState = 0x0000000A,
}
public enum ABE : uint {
Left = 0,
Top = 1,
Right = 2,
Bottom = 3
}
public static class ABS {
public const int Autohide = 0x0000001;
public const int AlwaysOnTop = 0x0000002;
}
public static class Shell32 {
[DllImport("shell32.dll", SetLastError = true)]
public static extern IntPtr SHAppBarMessage(
ABM dwMessage, [In] ref APPBARDATA pData
);
}
public static class User32 {
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(
string lpClassName,
string lpWindowName
);
}
[StructLayout(LayoutKind.Sequential)]
public struct APPBARDATA {
public uint cbSize;
public IntPtr hWnd;
public uint uCallbackMessage;
public ABE uEdge;
public RECT rc;
public int lParam;
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT {
public int left;
public int top;
public int right;
public int bottom;
}