如何从Windows窗体C#启动进程而不创建任务栏图标

问题描述

我有一个Windows Forms应用程序,该应用程序带有一个应该打开exe文件的按钮。

Windows的认行为是exe文件一旦打开,将在任务栏中显示其自己的图标,但是要求它不要具有该图标,并且窗口必须在图标内“嵌套”显示源于它的Windows Form应用程序,以便向用户隐藏exe文件的位置。

我一直在寻找解决方案,我的理解是,要实现此目的,我必须使用user32.dll库。

我在网上找到了一些尝试执行类似操作的代码(没有窗口,没有任务栏图标),并且我正在尝试对其进行调整,以使其适合我的需求,但到目前为止,我仍然受困。

这就是我所拥有的:

    private int    GWL_STYLE        = -16;
    private int    GWL_EXSTYLE      = -20;
    private int    WS_EX_APPWINDOW  = 262144;
    private UInt32 WS_POPUP         = 0x80000000;
    private UInt32 WS_CHILD         = 0x40000000;


    [DllImport("user32.dll")]
    private static extern int getwindowlong(IntPtr hWnd,int nIndex);

    [DllImport("user32.dll")]
    private static extern int SetwindowLong(IntPtr hWnd,int nIndex,int dwNewLong);

    [DllImport("user32.dll",SetLastError = true)]
    private static extern IntPtr SetParent(IntPtr hWndChild,IntPtr hWndNewParent);

    public Process HideProcess(string executablesPath,System.Windows.Forms.Form parentForm) {
        Process valuetoReturn = null;
        if (executablesPath != null && executablesPath.Equals(String.Empty) == false && File.Exists(executablesPath) == true) {
            processstartinfo startInfo = new processstartinfo(executablesPath);
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            valuetoReturn = Process.Start(startInfo);

            int style = getwindowlong(valuetoReturn.MainWindowHandle,GWL_EXSTYLE) & ~WS_EX_APPWINDOW;
            SetwindowLong(valuetoReturn.MainWindowHandle,GWL_EXSTYLE,style);
            SetParent(valuetoReturn.MainWindowHandle,parentForm.Handle);
        }
        return valuetoReturn;
    }

我已经尝试了这种方法的几种不同变体,但是我总是很接近,但不完全是我想要的位置。 我发现的文档似乎有些混乱,因此我有些困惑,不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)