-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgram.cs
71 lines (66 loc) · 2.46 KB
/
Program.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
using System;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
class Program
{
static void Main(string[] args)
{
bool vanish = args.Contains("-x"), show = args.Contains("-v"), hide = args.Contains("-h");
bool toggle = !(show ^ hide);
var trayWindow = FindWindow("Shell_TrayWnd", null);
var trayVisible = IsWindowVisible(trayWindow);
var trayData = new APPBARDATA { cbSize = Marshal.SizeOf(typeof(APPBARDATA)) };
var trayState = SHAppBarMessage(ABM_GETSTATE, ref trayData);
if ((toggle | show) && ((trayState & ABS_AUTOHIDE) > 0 || (vanish && !trayVisible)))
{
ShowWindow(trayWindow, SW_SHOW);
trayData.lParam = trayState & ~ABS_AUTOHIDE;
SHAppBarMessage(ABM_SETSTATE, ref trayData);
}
else if ((toggle | hide) && ((trayState & ABS_AUTOHIDE) == 0 || (vanish && trayVisible)))
{
trayData.lParam = trayState | ABS_AUTOHIDE;
SHAppBarMessage(ABM_SETSTATE, ref trayData);
if (vanish)
ShowWindow(trayWindow, SW_HIDE);
else
{
GetCursorPos(out var cursor);
SetForegroundWindow(WindowFromPoint(cursor)); // deactivate taskbar to trigger autohide
}
}
}
#region winuser.h, shellapi.h
[StructLayout(LayoutKind.Sequential)]
struct APPBARDATA
{
public int cbSize;
public IntPtr hWnd;
public uint uCallbackMessage;
public uint uEdge;
public Rectangle rc;
public int lParam;
}
const int ABS_AUTOHIDE = 1;
const int ABS_ALWAYSONTOP = 2;
const int ABM_GETSTATE = 4;
const int ABM_SETSTATE = 10;
const int SW_HIDE = 0;
const int SW_SHOW = 5;
[DllImport("shell32")]
static extern int SHAppBarMessage(int msg, ref APPBARDATA data);
[DllImport("user32")]
static extern IntPtr SetForegroundWindow(IntPtr hWnd);
[DllImport("user32")]
static extern bool GetCursorPos(out Point lpPoint);
[DllImport("user32")]
static extern IntPtr WindowFromPoint(Point lpPoint);
[DllImport("user32")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32")]
static extern bool IsWindowVisible(IntPtr hWnd);
#endregion
}