Created
April 14, 2023 02:25
-
-
Save emoacht/bb6c0305e24a915e4e233005aad605d5 to your computer and use it in GitHub Desktop.
Switch taskbar aute hide state.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Runtime.InteropServices; | |
internal static class TaskbarHelper | |
{ | |
[DllImport("user32.dll", CharSet = CharSet.Unicode)] | |
extern static IntPtr FindWindow( | |
[MarshalAs(UnmanagedType.LPWStr), In] string lpClassName, | |
[MarshalAs(UnmanagedType.LPWStr), In] string? lpWindowName); | |
[DllImport("shell32.dll", CharSet = CharSet.Unicode)] | |
extern static UIntPtr SHAppBarMessage( | |
uint dwMessage, | |
ref APPBARDATA pData); | |
[StructLayout(LayoutKind.Sequential)] | |
struct APPBARDATA | |
{ | |
public uint cbSize; | |
public IntPtr hWnd; | |
public int uCallbackMessage; | |
public int uEdge; | |
public RECT rc; | |
public IntPtr lParam; | |
} | |
[StructLayout(LayoutKind.Sequential)] | |
struct RECT | |
{ | |
public int Left; | |
public int Top; | |
public int Right; | |
public int Bottom; | |
} | |
const uint ABM_GETSTATE = 0x00000004; | |
const uint ABM_SETSTATE = 0x0000000A; | |
const uint ABS_AUTOHIDE = 0x01; | |
const uint ABS_ALWAYSONTOP = 0x02; | |
public static void SwitchState() | |
{ | |
var taskbarHandle = FindWindow("System_TrayWnd", null); | |
var status = GetStatus(taskbarHandle); | |
switch (status) | |
{ | |
case ABS_AUTOHIDE: | |
Trace.WriteLine("To always on top"); | |
SetStatus(taskbarHandle, ABS_ALWAYSONTOP); | |
break; | |
case ABS_ALWAYSONTOP: | |
default: | |
Trace.WriteLine("To auto hide"); | |
SetStatus(taskbarHandle, ABS_AUTOHIDE); | |
break; | |
} | |
} | |
private static uint GetStatus(IntPtr handle) | |
{ | |
var data = new APPBARDATA { hWnd = handle }; | |
data.cbSize = (uint)Marshal.SizeOf(data); | |
return (uint)SHAppBarMessage(ABM_GETSTATE, ref data); | |
} | |
private static void SetStatus(IntPtr handle, uint option) | |
{ | |
var data = new APPBARDATA { hWnd = handle, lParam = (int)option }; | |
data.cbSize = (uint)Marshal.SizeOf(data); | |
SHAppBarMessage(ABM_SETSTATE, ref data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment