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;

namespace ryCommon
{ 
    /// <summary>
    /// 窗体操作类
    /// </summary>
    public class RyForm
    {
        /// <summary>
        /// 设置Comobox的行间距
        /// </summary>
        /// <param name="list"></param>
        /// <param name="itemHeight"></param>
        public static void CmbBind(ComboBox list, int itemHeight)
        {
            list.DropDownStyle = ComboBoxStyle.DropDownList;
            list.ItemHeight = itemHeight;
            list.DrawMode = DrawMode.OwnerDrawFixed;
            list.DrawItem += new DrawItemEventHandler(delegate (object sender, DrawItemEventArgs e)
            {
                if (e.Index < 0)
                {
                    return;
                }
                e.DrawBackground();
                e.DrawFocusRectangle();
                e.Graphics.DrawString(list.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y + 3);
            });
        }
        /// <summary>
        /// 移除关闭按钮。返回值，非零表示成功，零表示失败。
        /// </summary>
        /// <param name="iHWND">窗口的句柄</param>
        /// <returns>是否成功</returns>
        public static int RemoveXButton(IntPtr iHWND)
        {
            int iSysMenu;
            const int MF_BYCOMMAND = 0x400; //0x400-关闭
            iSysMenu = User32.GetSystemMenu(iHWND, 0);
            return User32.RemoveMenu(iSysMenu, 6, MF_BYCOMMAND);
        }
        /// <summary>
        /// 把窗体放到最前
        /// </summary>
        /// <param name="iHWND"></param>
        public static void BringToTop(IntPtr iHWND)
        {
            User32.SetForegroundWindow(iHWND);
        }
        /// <summary>
        /// 显示窗体
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="nCmdShow"></param>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]      
        public static extern bool ShowWindow(HandleRef hWnd, int nCmdShow);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);
        private const int HWND_TOPMOST = -1;
        private const int HWND_NOTOPMOST = -2;
        private const int SWP_NOACTIVATE = 0x10;
        private const int SWP_NOMOVE = 0x2;
        private const int SWP_NOSIZE = 0x1;
        /// <summary>
        /// 设置窗体是否置顶
        /// </summary>
        /// <param name="iHWND"></param>
        /// <param name="TopMost"></param>
        public static void SetTopMost(IntPtr iHWND,bool TopMost)
        {
            SetWindowPos(iHWND, (TopMost?HWND_TOPMOST: HWND_NOTOPMOST), 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
        }
        /// <summary>
        /// 显示没有焦点的窗口
        /// </summary>
        /// <param name="frm"></param>
        public static void ShowNoActiveWindow(Form frm)
        {
            ShowWindow(new HandleRef(frm, frm.Handle), 4);
        }
        /// <summary>
        /// 根据句柄获取窗口文本
        /// </summary>
        /// <param name="Handle"></param>
        /// <returns></returns>
        public static string GetWinText(IntPtr Handle)
        {
            StringBuilder WinTitle = new StringBuilder(200);
            int len = User32.GetWindowText(Handle.ToInt32(), WinTitle, 200);
            return WinTitle.ToString();
        }

        /// <summary>
        /// 根据句柄获取窗口类
        /// </summary>
        /// <param name="Handle"></param>
        /// <returns></returns>
        public static string GetWinClass(IntPtr Handle)
        {
            StringBuilder WinClass = new StringBuilder(200);
            int len = User32.GetClassName(Handle.ToInt32(), WinClass, 200);
            return WinClass.ToString();
        }
        /// <summary>
        /// 获取当前活动的窗口句柄
        /// </summary>
        /// <returns></returns>
        public static IntPtr GetActiveWindow()
        {
            return User32.GetActiveWindow();
        }
        /// <summary>
        /// 设置为当前活动窗口
        /// </summary>
        /// <param name="hwnd"></param>
        /// <returns></returns>
        public static IntPtr SetActiveWindow(IntPtr hwnd)
        {
            return User32.SetActiveWindow(hwnd);
        }
        /// <summary>
        /// 根据窗口句柄来获得进程id
        /// </summary>
        /// <param name="hwnd"></param>
        /// <returns></returns>
        public static int GetProcessId(IntPtr hwnd)
        {
            User32.GetWindowThreadProcessId(hwnd, out int ID);
            return ID;
        }
        /// <summary>
        /// 显示窗体
        /// </summary>
        /// <param name="frm"></param>
        public static void Show(Form frm)
        {
            bool topmost = frm.TopMost;
            frm.BringToFront();
            frm.TopMost = true;
            frm.TopMost = topmost;
            frm.Show();
        }
        /// <summary>
        /// 设置窗体父窗体为桌面,不会随着显示桌面而最小化,但无法设置窗体透明度
        /// </summary>
        /// <param name="handle"></param>
        public static void SetDesktopForm(IntPtr handle)
        {
            //在构造函数中或者Load中   
           IntPtr hDeskTop = WinAPI.User32.FindWindow("Progman", null);
           WinAPI.User32.SetParent(handle, hDeskTop);
        }
        /// <summary>
        /// 结束进程
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public static long KillPorc(int pid)
        {
           return WinAPI.Kernel32.TerminateProcess(pid,1);
        }
        /// <summary>
        /// 打开窗体,只打开一个实例(非模态)
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static Form ShowOne(Form type)
        {
            try
            {
                foreach (Form frm in Application.OpenForms)
                {
                    if (frm.GetType() == type.GetType())
                    {
                        frm.WindowState = FormWindowState.Normal;
                        frm.BringToFront();
                        return frm;
                    }
                }
                type.StartPosition = FormStartPosition.CenterScreen;
                type.Show();
                return type;
            }
            catch { }
            return null;
        }
        /// <summary>
        /// 将窗体植入到容器控件中
        /// </summary>
        /// <param name="frm"></param>
        /// <param name="panrent_ctl">作为窗体容器的控件</param>
        /// <returns></returns>
        public static bool SetParentWin(Form frm, Control panrent_ctl)
        {
            if (frm == null) { return false; }
            if (panrent_ctl == null) { return false; }
            frm.FormBorderStyle = FormBorderStyle.None;
            _ = WinAPI.User32.SetParent(frm.Handle, panrent_ctl.Handle);
            _ = WinAPI.User32.MoveWindow(frm.Handle, 0, 0, panrent_ctl.Width, panrent_ctl.Height, false);
            return true;
        }
        /// <summary>
        /// 重新调整窗体大小以适配容器大小
        /// </summary>
        /// <param name="frm"></param>
        /// <param name="panrent_ctl">作为窗体容器的控件</param>
        /// <returns></returns>
        public static bool ResizeWin(Form frm, Control panrent_ctl)
        {
            if (frm == null) { return false; }
            if (panrent_ctl == null) { return false; }
            _ = WinAPI.User32.MoveWindow(frm.Handle, 0, 0, panrent_ctl.Width, panrent_ctl.Height, true);
            return true;
        }
    }
}
