c#:当你使用Win32API在c#中启动其他程序时,我如何隐藏?

本文关键字:何隐藏 隐藏 程序 当你 Win32API 启动 其他 | 更新日期: 2023-09-27 18:10:39

我使用以下代码:

    /********** This code is First way **********/
    String filePath = "ex";
    StratInfo psi = new StartInfo(filePath);
    psi.WindowStyle = ProcessWindowStyle.Hidden;
    Process process = Process.Start(psi);
    process.WaitForInputIdle();
    /********************************************/
    /********** This code is Second way **********/
    String filePath = "ex";
    Process process = Process.Start(new StartInfo(filePath));
    process.WaitForInputIdle();
    [DllImport("User32.dll")]
    private static extern int ShowWindow (int hwnd, int nCmdShow);
    int hWnd = FindWindow(null, "Main Caption");
    if (hWnd != 0) {
        ShowWindow(hWnd, 0);
    }
    /***********************************************/

大多数程序都是这样解决的。

但是,有些程序不是这样工作的。

第一种方法:根本不可能。

第二种方式:只有一部分,而且慢。我可以隐藏主窗口。但是MessageBox仍然显示

如何解决这个问题?谢谢你的回答。

c#:当你使用Win32API在c#中启动其他程序时,我如何隐藏?

查看WinExec函数,它允许您传递一个可见性参数:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms687393 (v = vs.85) . aspx

这可能会给process带来类似的结果,但是,值得尝试一下。

你提到了消息框显示——你无法控制它,因为在你的桌面上运行的程序有能力对桌面做它想做的事情,包括随机弹出最上面的窗口。

尝试将程序作为服务运行:(这会抑制程序的UI):

http://www.howtogeek.com/50786/using-srvstart-to-run-any-application-as-a-windows-service/

当然,现在,当一个消息框被程序引发时,windows不会显示它,那个程序基本上被"锁定"在那里;而且,您可能也没有将程序作为服务运行的权限。

你到底为什么要这样做?您可能会对社区建议的良好解决方案感到惊讶。

祝你好运。

为了避免所有GUI元素,您可能希望在进程自己的桌面上运行该进程。查看这个线程以获得一些额外的信息:

创建在新的Windows桌面上运行IE的进程

MSDN:关于windows工作站和桌面

尽管您仍然需要处理等待用户输入的进程(对话框、消息框等)