unity打开外部exe,并将其置顶

直接上代码:

调用代码

ProcessStartInfo startinfo1 = new ProcessStartInfo();
startinfo1.FileName = "E:\\fastdds\\run\\" + PlanType + ".exe";
startinfo1.Arguments = winInfo;
pss = Process.Start(startinfo1);

//做延时的原因是window打开exe时会有一定的时间,这个时间立刻执行是获取不到句柄的
Invoke("OpenZhiding",0.5f);

延时代码内容

    public void OpenZhiding()
    {
        hWnd = Zhiding.GetProcessWnd(pss.MainWindowHandle);
        Active();
    }

激活窗口

/// <summary>
    /// 激活窗口
    /// </summary>
    void Active()
    {
        if (KeepForeground)
        {
            ShowWindow(hWnd, 1);
            SetForegroundWindow(hWnd);
            SetWindowPos(hWnd, HWND_TOPMOST, 302, 318, 1575, 591, SWP_NOSIZE | SWP_NOMOVE);
        }
    }

获取句柄等操作 zhiding.cs

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

public class Zhiding
{
    public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);
    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr GetParent(IntPtr hWnd);
    [DllImport("user32.dll")]
    public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);

    [DllImport("kernel32.dll")]
    public static extern void SetLastError(uint dwErrCode);

    public static IntPtr GetProcessWnd(IntPtr Ptr)
    {
        IntPtr ptrWnd = Ptr;
        uint pid = 0;
        foreach (var item in Process.GetProcesses())
        {
            //User.JiemianPlanModel是启动的exe名称,不包含.exe后缀
            if (item.ProcessName == User.JiemianPlanModel)
            {
                pid = (uint)item.Id;
            }
        }
        MgrTips.Instance.ShowTip(pid.ToString());
        bool bResult = EnumWindows(new WNDENUMPROC(delegate (IntPtr hwnd, uint lParam)
        {
            uint id = 0;
            if (GetParent(hwnd) == IntPtr.Zero)
            {
                GetWindowThreadProcessId(hwnd, ref id);
                if (id == lParam)    // 找到进程对应的主窗口句柄  
                {
                    ptrWnd = hwnd;   // 把句柄缓存起来  
                    MgrTips.Instance.ShowTip(ptrWnd.ToString());
                    SetLastError(0);    // 设置无错误  
                    return false;   // 返回 false 以终止枚举窗口  
                }
            }
            return true;

        }), pid);
        return  (!bResult && Marshal.GetLastWin32Error() == 0) ? ptrWnd : IntPtr.Zero;
    }
}

相关推荐

  1. unity打开外部exe

    2024-02-02 23:36:01       53 阅读
  2. vue页面带滚动条,打开新页面页面不的问题

    2024-02-02 23:36:01       28 阅读
  3. qt窗口

    2024-02-02 23:36:01       27 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-02 23:36:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-02 23:36:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-02 23:36:01       87 阅读
  4. Python语言-面向对象

    2024-02-02 23:36:01       96 阅读

热门阅读

  1. Kubernetes实战(二十二)-Pod时区修改

    2024-02-02 23:36:01       58 阅读
  2. C# Newtonsoft.Json解析json笔记

    2024-02-02 23:36:01       46 阅读
  3. 【Git系列】修改远程分支名

    2024-02-02 23:36:01       58 阅读
  4. virtualBox虚拟机安装ubuntu后的必要配置

    2024-02-02 23:36:01       59 阅读
  5. 备考蓝桥杯每日一题——C++分支结构“ABC”

    2024-02-02 23:36:01       46 阅读
  6. 原子计数器缓冲区 Atomic Counter Buffers

    2024-02-02 23:36:01       43 阅读
  7. 蓝桥杯-景区导游-DFS

    2024-02-02 23:36:01       41 阅读
  8. 代码随想录算法训练营第二十四天|77. 组合

    2024-02-02 23:36:01       47 阅读
  9. 安卓之代码检查工具优劣分析以及应用场景

    2024-02-02 23:36:01       52 阅读
  10. 【Python】websockets库的介绍及用法

    2024-02-02 23:36:01       48 阅读