tx
2025-04-14 c33f54888d8fb4e1961bca69fe3d01e87fc54be6
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
#region Imports
 
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Security;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Native
{
    #region TaskBarNative
 
    #region General
 
    public enum TaskBarPosition
    {
        Unknown = -1,
        Left,
        Top,
        Right,
        Bottom,
    }
 
    #endregion
 
    internal class TaskBar
    {
        private const string ClassName = "Shell_TrayWnd";
 
        private Rectangle bounds = Rectangle.Empty;
        public Rectangle Bounds
        {
            get => bounds;
            private set => bounds = value;
        }
        public TaskBarPosition Position { get; private set; } = TaskBarPosition.Unknown;
 
        public Point Location => Bounds.Location;
 
        public Size Size => Bounds.Size;
 
        public bool AlwaysOnTop { get; private set; } = false;
        public bool AutoHide { get; private set; } = false;
 
        [SecuritySafeCritical]
        public TaskBar()
        {
            IntPtr taskbarHandle = WinApi.FindWindow(ClassName, null);
 
            WinApi.APPBARDATA data = new()
            {
                cbSize = (uint)Marshal.SizeOf(typeof(WinApi.APPBARDATA)),
                hWnd = taskbarHandle
            };
            IntPtr result = WinApi.SHAppBarMessage(WinApi.ABM.GetTaskbarPos, ref data);
            if (result == IntPtr.Zero)
            {
                throw new InvalidOperationException();
            }
 
            Position = (TaskBarPosition)data.uEdge;
            Bounds = Rectangle.FromLTRB(data.rc.Left, data.rc.Top, data.rc.Right, data.rc.Bottom);
 
            data.cbSize = (uint)Marshal.SizeOf(typeof(WinApi.APPBARDATA));
            result = WinApi.SHAppBarMessage(WinApi.ABM.GetState, ref data);
            int state = result.ToInt32();
            AlwaysOnTop = (state & WinApi.AlwaysOnTop) == WinApi.AlwaysOnTop;
            AutoHide = (state & WinApi.Autohide) == WinApi.Autohide;
        }
 
    }
 
    #endregion
}