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

namespace MyDb
{
    /// <summary>
    /// Win32 API
    /// </summary>
    public class RyWin32
    {
        /// <summary>
        /// 
        /// </summary>
        public const int GWL_WNDPROC = (-4);

        /// <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>
        /// <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 User32.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)
        {
            User32.SetParent(sub_form, parent_form);
            User32.MoveWindow(sub_form, 0, 0, size.Width, size.Height, false);
            return true;
        }

    }
}
