using System.Runtime.InteropServices;

namespace AudioWallpaper {
    using System;
    using System.Runtime.InteropServices;

    public static class Win32 {
        /// <summary>
        /// 查找指定类名和窗口名的窗口句柄。
        /// </summary>
        /// <param name="className">窗口类名</param>
        /// <param name="winName">窗口名称</param>
        /// <returns>窗口句柄</returns>
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string className, string winName);

        /// <summary>
        /// 向指定窗口发送消息。
        /// </summary>
        /// <param name="hwnd">窗口句柄</param>
        /// <param name="Msg">消息类型</param>
        /// <param name="wParam">消息的附加信息</param>
        /// <param name="lParam">消息的附加信息</param>
        /// <returns>消息处理结果</returns>
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessage(IntPtr hwnd, uint Msg, IntPtr wParam, IntPtr lParam);

        /// <summary>
        /// 向指定窗口发送消息，并设置超时。
        /// </summary>
        /// <param name="hwnd">窗口句柄</param>
        /// <param name="msg">消息类型</param>
        /// <param name="wParam">消息的附加信息</param>
        /// <param name="lParam">消息的附加信息</param>
        /// <param name="fuFlage">标志</param>
        /// <param name="timeout">超时（毫秒）</param>
        /// <param name="result">结果指针</param>
        /// <returns>消息处理结果</returns>
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessageTimeout(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam, uint fuFlage, uint timeout, IntPtr result);

        /// <summary>
        /// 枚举所有顶级窗口。
        /// </summary>
        /// <param name="proc">回调函数，用于处理每个窗口</param>
        /// <param name="lParam">用户自定义参数</param>
        /// <returns>是否成功</returns>
        [DllImport("user32.dll")]
        public static extern bool EnumWindows(EnumWindowsProc proc, IntPtr lParam);

        /// <summary>
        /// 窗口枚举回调委托。
        /// </summary>
        /// <param name="hwnd">窗口句柄</param>
        /// <param name="lParam">用户自定义参数</param>
        /// <returns>是否继续枚举</returns>
        public delegate bool EnumWindowsProc(IntPtr hwnd, IntPtr lParam);

        /// <summary>
        /// 查找指定父窗口下的子窗口。
        /// </summary>
        /// <param name="hwndParent">父窗口句柄</param>
        /// <param name="hwndChildAfter">在此窗口之后查找</param>
        /// <param name="className">窗口类名</param>
        /// <param name="winName">窗口名称</param>
        /// <returns>子窗口句柄</returns>
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string className, string winName);

        /// <summary>
        /// 显示或隐藏指定窗口。
        /// </summary>
        /// <param name="hwnd">窗口句柄</param>
        /// <param name="nCmdShow">显示状态</param>
        /// <returns>是否成功</returns>
        [DllImport("user32.dll")]
        public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);

        /// <summary>
        /// 设置窗口的位置和大小。
        /// </summary>
        /// <param name="hwnd">窗口句柄</param>
        /// <param name="hhWndInserAfter">窗口在 Z 顺序中的位置</param>
        /// <param name="x">新的 X 坐标</param>
        /// <param name="y">新的 Y 坐标</param>
        /// <param name="cx">新的宽度</param>
        /// <param name="cy">新的高度</param>
        /// <param name="uFlags">标志</param>
        /// <returns>是否成功</returns>
        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hwnd, IntPtr hhWndInserAfter, int x, int y, int cx, int cy, uint uFlags);

        /// <summary>
        /// 设置指定窗口的父窗口。
        /// </summary>
        /// <param name="hwnd">窗口句柄</param>
        /// <param name="parentHwnd">新父窗口句柄</param>
        /// <returns>新的窗口句柄</returns>
        [DllImport("user32.dll")]
        public static extern IntPtr SetParent(IntPtr hwnd, IntPtr parentHwnd);

        /// <summary>
        /// 发送 APPBAR 消息。
        /// </summary>
        /// <param name="dwMessage">消息类型</param>
        /// <param name="pData">APPBARDATA 结构的引用</param>
        /// <returns>结果</returns>
        [DllImport("shell32.dll")]
        public static extern uint SHAppBarMessage(uint dwMessage, ref APPBARDATA pData);

        /// <summary>
        /// 注册窗口消息。
        /// </summary>
        /// <param name="lpString">消息字符串</param>
        /// <returns>消息标识</returns>
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        public static extern uint RegisterWindowMessage(string lpString);

        /// <summary>
        /// APPBARDATA 结构用于 APPBAR 功能。
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct APPBARDATA {
            public int cbSize;              // 结构大小
            public IntPtr hWnd;            // APPBAR 窗口句柄
            public uint uCallbackMessage;   // 回调消息
            public uint uEdge;             // 边缘位置
            public RECT rc;                // 矩形区域
            public int lParam;             // 用户自定义参数
        }

        /// <summary>
        /// 矩形结构。
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT {
            public int left;   // 左边界
            public int top;    // 上边界
            public int right;  // 右边界
            public int bottom; // 下边界
        }
    }

}
