using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace MyDb
{
    /// <summary>
    /// Win32 API
    /// </summary>
    public class RyWin32
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="bRevert"></param>
        /// <returns></returns>
        [DllImport("USER32.DLL")]
        public static extern int GetSystemMenu(IntPtr hwnd, int bRevert);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="hMenu"></param>
        /// <param name="nPosition"></param>
        /// <param name="wFlags"></param>
        /// <returns></returns>
        [DllImport("USER32.DLL")]
        public static extern int RemoveMenu(int hMenu, int nPosition, int wFlags);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        /// <summary>
        /// 获取当前前台窗口句柄
        /// </summary>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetForegroundWindow();
        /// <summary>
        /// 获得当前活动窗体
        /// </summary>
        /// <returns></returns>
        [DllImport("user32.dll")]
        public static extern IntPtr GetActiveWindow();//获得当前活动窗体
        /// <summary>
        /// 设置活动窗体
        /// </summary>
        /// <param name="hwnd"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        public static extern IntPtr SetActiveWindow(IntPtr hwnd);//设置活动窗体
        /// <summary>
        /// 获取类名
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="lpClassName"></param>
        /// <param name="nMaxCount"></param>
        /// <returns></returns>
        [DllImport("user32", EntryPoint = "GetClassName", SetLastError = false,
            CharSet = CharSet.Auto, ExactSpelling = false,
            CallingConvention = CallingConvention.StdCall)]
        public static extern int GetClassName(int hWnd, StringBuilder lpClassName, int nMaxCount);
        /// <summary>
        /// 获取当前线程对应的进程ID
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="ID"></param>
        /// <returns></returns>
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
        /// <summary>
        /// 判断指定句柄是否是一个窗口
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "IsWindow")]
        public static extern bool IsWindow(IntPtr hWnd);
        /// <summary>
        /// 获取窗口标题
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="lpString"></param>
        /// <param name="nMaxCount"></param>
        /// <returns></returns>
        [DllImport("user32", SetLastError = true)]
        public static extern int GetWindowText(
            int hWnd,//窗口句柄
            StringBuilder lpString,//标题
            int nMaxCount //最大值
            );
        /// <summary>
        /// 查找窗口
        /// </summary>
        /// <param name="lpClassName"></param>
        /// <param name="lpWindowName"></param>
        /// <returns></returns>
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        /// <summary>
        /// 设置父窗口
        /// </summary>
        /// <param name="hWndChild"></param>
        /// <param name="hWndParent"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        public static extern int SetParent(IntPtr hWndChild, IntPtr hWndParent);
        /// <summary>
        /// 移动窗口
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="nWidth"></param>
        /// <param name="nHeight"></param>
        /// <param name="BRePaint"></param>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool BRePaint);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="lpPrevWndFunc"></param>
        /// <param name="hwnd"></param>
        /// <param name="msg"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        [DllImport("User32.dll")]
        public extern static IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="nIndex"></param>
        /// <param name="dwNewLong"></param>
        /// <returns></returns>
        [DllImport("User32.dll")]
        public static extern IntPtr SetWindowLong(IntPtr hwnd, int nIndex, IntPtr dwNewLong);
        /// <summary>
        /// 
        /// </summary>
        public const int GWL_WNDPROC = (-4);
        /// <summary>
        /// 结束进程
        /// </summary>
        /// <param name="handle"></param>
        /// <param name="exitCode"></param>
        /// <returns></returns>
        [DllImport("kernel32")]
        public static extern long TerminateProcess(int handle, int exitCode);
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="msg"></param>
        /// <param name="wParam"></param>
        /// <param name="IParam"></param>
        /// <returns></returns>
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hwnd, int msg, int wParam, ref COPYDATASTRUCT IParam);
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="msg"></param>
        /// <param name="wParam"></param>
        /// <param name="IParam"></param>
        [DllImport("user32.dll")]
        public static extern void PostMessage(IntPtr hWnd, int msg, int wParam, ref COPYDATASTRUCT IParam);
        /// <summary>
        /// 获取窗口位置和大小
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="lpRect"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
        /// <summary>
        /// 矩形
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            /// <summary>
            /// 最左坐标
            /// </summary>
            public int Left;
            /// <summary>
            /// 最上坐标
            /// </summary>
            public int Top;
            /// <summary>
            /// 最右坐标
            /// </summary>
            public int Right; 
            /// <summary>
            /// 最下坐标
            /// </summary>
            public int Bottom;
        }
        /// <summary>
        /// WM_COPYDATA消息的主要目的是允许在进程间传递只读数据。 
        /// </summary>
        public const int WM_COPYDATA = 0x004A;
        /// <summary>
        /// 系统通道ID
        /// </summary>
        public const int Sys_chanel_id = 1000;
        /// <summary>
        /// 用户通道ID
        /// </summary>
        public const int User_chanel_id = 1001;
        /// <summary>
        /// 
        /// </summary>
        public struct COPYDATASTRUCT
        {
            /// <summary>
            /// 
            /// </summary>
            public IntPtr dwData;
            /// <summary>
            /// 
            /// </summary>
            public int cData;
            /// <summary>
            /// 
            /// </summary>
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpData;
        }
        /// <summary>
        /// 获取消息
        /// </summary>
        /// <param name="m"></param>
        /// <param name="handle"></param>
        /// <returns></returns>
        public static string GetMsg(Message m,out IntPtr handle)
        {
            COPYDATASTRUCT cdata = new COPYDATASTRUCT();
            Type mytype = cdata.GetType();
            cdata = (COPYDATASTRUCT)m.GetLParam(mytype);
            handle = cdata.dwData;
            return cdata.lpData;
        }
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="from_handle"></param>
        /// <param name="to_handle"></param>
        /// <param name="wParam"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int SendMsg(IntPtr from_handle, IntPtr to_handle, int wParam, string str)
        {
            byte[] arr = System.Text.Encoding.Default.GetBytes(str);
            int len = arr.Length;
            COPYDATASTRUCT cdata;
            cdata.dwData = from_handle;
            cdata.lpData = str;
            cdata.cData = len + 1;
            return SendMessage(to_handle, WM_COPYDATA, wParam, ref cdata);
        }
        /// <summary>
        /// 设置父窗口
        /// </summary>
        /// <param name="sub_form"></param>
        /// <param name="parent_form"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static bool SetParentWin(IntPtr sub_form, IntPtr parent_form,Size size)
        {
            SetParent(sub_form, parent_form);
            MoveWindow(sub_form, 0, 0, size.Width, size.Height, false);
            return true;
        }
    }
}
