多媒体软件通用操作(一、对软件窗口的一些操作以及使用系统键盘)

首先我们先实现软件的最大化和最小化,需要使用到系统的user32.dll,这里也将系统的弹窗用到了,防止后面用到(例如,具体哪里用看自己了,我是在加载一些重要文件,文件缺失时弹出提醒)。

隐藏显示窗口,实现窗口最大化,最小化以及窗口化下无边框显示,使用系统弹框,关机,移动窗口,调用系统键盘osk并屏幕底部居中显示(适用于触摸屏输入),移动窗口

直接上代码吧,很简单的,一看就能懂。

/// <summary>
    /// 窗口类工具
    /// </summary>
    public class WindowsUtil
    {
        [DllImport("User32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
        public static extern int MessageBox(IntPtr handle, String message, String title, int type);//系统弹框
        [DllImport("User32.dll")]
        public static extern bool ShowWindow(System.IntPtr hwnd, int nCmdShow);//设置当前窗口的显示状态
        [DllImport("User32.dll", EntryPoint = "GetForegroundWindow")]
        public static extern IntPtr GetForegroundWindow();//获取当前激活窗口
        [DllImport("User32.dll", EntryPoint = "SetForegroundWindow")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("User32.dll", EntryPoint = "MoveWindow")]//移动窗口
        public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
        [DllImport("User32.dll")]
        private static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong); //设置窗口边框
        [DllImport("User32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);//寻找窗口
        [DllImport("User32.dll")]
        static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);//设置窗口位置
        //边框参数
        const uint SWP_SHOWWINDOW = 0x0040;
        const int GWL_STYLE = -16;
        const int WS_BORDER = 1;
        const int SW_HIDE = 0;//(隐藏窗口)
        const int SW_SHOWNORMAL = 1;
        const int WS_POPUP = 0x800000;
        const int SW_SHOWMINIMIZED = 2;//(最小化窗口)
        const int SW_SHOWMAXIMIZED = 3;//(最大化窗口)
        static Process tempProcess = null;
        /// <summary>
        /// 显示系统的弹窗
        /// </summary>
        /// <param name="message">显示内容</param>
        /// <param name="title">弹框标题</param>
        /// <param name="type">弹框类型</param>
        public static void ShowBox(string message, string title, int type = 1)
        {
            MessageBox(IntPtr.Zero, message, title, type);
        }
        /// <summary>
        /// 设置窗口最小化
        /// </summary>
        public static void SetWindowsMin()
        {
            ShowWindow(GetForegroundWindow(), SW_SHOWMINIMIZED);
        }
        /// <summary>
        /// 未全屏的状态下设置窗口最大化
        /// </summary>
        public static void SetWindowsMax()
        {
            ShowWindow(GetForegroundWindow(), SW_SHOWMAXIMIZED);
        }
        //隐藏窗口
        public static void HideWindows(IntPtr currentWindow)
        {
            if (currentWindow == IntPtr.Zero) return;
            ShowWindow(currentWindow, SW_HIDE);
        }
        //显示窗口
        public static void ShowWindows(IntPtr currentWindow)
        {
            if (currentWindow == IntPtr.Zero) return;
            SetForegroundWindow(currentWindow);
            ShowWindow(currentWindow, SW_SHOWNORMAL);
        }
        /// <summary>
        /// 设置屏幕激活
        /// </summary>
        public static void SetDisplayActivate()
        {
            for (int i = 0; i < Display.displays.Length; i++)
            {
                Display.displays[i].Activate(Display.displays[i].renderingWidth, Display.displays[i].renderingHeight, 60);
            }
        }
        /// <summary>
        /// 设置屏幕分辨率
        /// </summary>
        /// <param name="width">宽</param>
        /// <param name="height">高</param>
        /// <param name="fullscreen">是否窗口化 默认有边框</param>
        public static IEnumerator SetWindowsResolution(int width, int height, bool fullscreen = false)
        {
            Screen.SetResolution(width, height, fullscreen);
#if !UNITY_EDITOR
            if (!fullscreen)
            {
                string name = Application.productName;
                IntPtr handler = FindWindow(null, name);
                if (handler == IntPtr.Zero)
                {
                    handler = GetForegroundWindow();
                }
                yield return new WaitForSeconds(.1f);
                SetWindowLong(handler, GWL_STYLE, WS_BORDER);
                int posx = (int)((Screen.currentResolution.width - width) / 2);
                int posy = (int)((Screen.currentResolution.height - height) / 2);
                //SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_BORDER);//设置无框;
                bool result = SetWindowPos(handler, 0, posx, posy, width + 2, height + 2, SWP_SHOWWINDOW);//exe居中显示;
            }
#endif
            yield return null;
        }
        /// <summary>
        /// 显示系统软键盘(底部居中显示)
        /// </summary>
        public static void ShowTouchKeyboard()
        {
            try
            {
                if (tempProcess != null) tempProcess.Kill();
                //这里将软键盘的文件放在工程同级目录里了,可以指定到system32路径
                tempProcess = ExternalCall(Environment.CurrentDirectory + "/osk.exe", null, false);
                IntPtr intptr = IntPtr.Zero;
                while (IntPtr.Zero == intptr)
                {
                    System.Threading.Thread.Sleep(100);
                    UnityEngine.Debug.Log("寻找键盘");
                    intptr = FindWindow(null, "屏幕键盘");
                }
                // 获取屏幕尺寸
                int ScreenWidth = Screen.width;
                int ScreenHeight = Screen.height;


                // 设置软键盘的显示位置,底部居中 宽度为屏幕宽度,高度为300
                MoveWindow(intptr, 0, ScreenHeight - 400, ScreenWidth, 400, true);
            }
            catch (Exception e)
            {
                if (tempProcess != null) tempProcess.Kill();
                UnityEngine.Debug.Log(e.ToString());
            }
        }
        /// <summary>
        /// 调用Process
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="arguments"></param>
        /// <param name="hideWindow"></param>
        /// <returns></returns>
        public static Process ExternalCall(string filename, string arguments, bool hideWindow)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = filename;
            startInfo.Arguments = arguments;
            //如果只是命令行,则需要不显示窗口界面
            if (hideWindow)
            {
                startInfo.RedirectStandardOutput = true;
                startInfo.RedirectStandardError = true;
                startInfo.UseShellExecute = false;
                startInfo.CreateNoWindow = true;
            }
            Process process = new Process();
            process.StartInfo = startInfo;
            process.Start();
            return process;
        }
    }

后续会将工程打包为package包发出来,有问题可以加我微信S-yu12探讨。

下一篇:多媒体软件通用操作(二、生成快捷方式以及延时启动程序)

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Anson_宋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值