Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 隐藏windows窗体后,无法将其显示回来_C#_.net - Fatal编程技术网

C# 隐藏windows窗体后,无法将其显示回来

C# 隐藏windows窗体后,无法将其显示回来,c#,.net,C#,.net,下面是我的代码。在这里,您可以看到我使用常量来隐藏/显示窗口。在C#.net中对另一个应用程序隐藏另一个应用程序 可见时,MainWindowHandle为非零,隐藏窗口后,句柄设置为0。我还没有找到一个方法来获得所需的句柄-也许一个解决办法是维护一个你隐藏的窗口列表 List<int> HiddenWindows = new List<int>(); private void btnHide_Click(object sender, RoutedEventArgs e

下面是我的代码。在这里,您可以看到我使用常量来隐藏/显示窗口。在C#.net中对另一个应用程序隐藏另一个应用程序


可见时,MainWindowHandle为非零,隐藏窗口后,句柄设置为0。我还没有找到一个方法来获得所需的句柄-也许一个解决办法是维护一个你隐藏的窗口列表

List<int> HiddenWindows = new List<int>();

private void btnHide_Click(object sender, RoutedEventArgs e)
{
  Process[] processRunning = Process.GetProcessesByName(FileName);
  foreach (Process pr in processRunning)
  {
    int hWnd = pr.MainWindowHandle.ToInt32();
    if (hWnd == 0)
      continue;
    ShowWindow(hWnd, SW_HIDE);
    HiddenWindows.Add(hWnd);
  }
}

private void btnShow_Click(object sender, RoutedEventArgs e)
{
  foreach (int hWnd in HiddenWindows)
  {
    ShowWindow(hWnd, SW_SHOW);
  }
  HiddenWindows.Clear();
}
注意,我已经修改了ShowWindow定义,将IntPtr用作窗口句柄的类型-这是首选类型&它是MainWindowHandle的实际类型。在隐藏方法中,只需将代码更改为 是

或者整个循环到

foreach (Process pr in processRunning)
{
  ShowWindow(pr.MainWindowHandle, SW_HIDE);
}

这就是
Process.GetProcess()
的工作原理

一旦您隐藏了进程的主窗口,它的
MainWindowHandle
用于下一次迭代的进程将变为
IntPtr.Zero
。我想这与如何检索
MainWindowHandle
有关

解决方案:

  • 枚举进程一次
  • 存储
    hWnd
    (例如在
    字典中
    )并调用
    ShowWindow()
    ,而无需再次检索进程

  • “无法将其显示”这是什么意思?您是否收到任何错误,某些代码执行不正常?如果您将其显示,您仍然需要激活以查看它。。。如果它在后台:-),那么使用SW_RESTORE=9:可能重复No,我不能得到任何错误,但是'pr.MainWindowHandle'返回0(零)?谢谢PaulF。你的解决方案是正确的。但是在我的场景中,我想在隐藏另一个应用程序之后关闭第一个应用程序。同样,当我启动第一个应用程序时,我想显示隐藏的应用程序。不幸的是,一旦隐藏,应用程序就没有窗口-因此应用程序MainWindowHandle设置为零。我添加的链接中的答案应该可以做到这一点——获取进程ID,然后对所有可用窗口进行强制搜索,以找到与ID匹配的附加到进程的窗口。我尝试按照链接中给出的方法,但它不起作用,我尝试ShowWindow(句柄,SW_还原);//恢复Windows ShowWindow(手柄、开关显示)//显示窗口SetForegroundWindow(句柄)@Ankitupadhaya:我已经更新了基于原始链接代码的代码。我在记事本上尝试了一下,效果很好,我的问题是在某些情况下应用程序启动时没有UI。那么在没有加载UI的情况下,如何显示UI?
    private const int SW_SHOW = 5;
    private String FileName = "notepad";
    
    [DllImport("User32")]
    private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
    [DllImport("User32.dll")]
    private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string strClassName, string strWindowName);
    
    [DllImport("user32.dll")]
    private static extern int GetWindowThreadProcessId(IntPtr hWnd, out int ProcessId);
    
    private void btnShow2_Click(object sender, RoutedEventArgs e)
    {
      //an array of all processes with name "processName"
      Process[] localAll = Process.GetProcessesByName(FileName);
    
      foreach (var pr in localAll)
      {
        IntPtr hWnd = IntPtr.Zero;
        int prcsId = 0;
    
        //check all open windows (not only the process we are looking) begining from the
        //child of the desktop, handle = IntPtr.Zero initialy.
        do
        {
          //get child handle of window who's handle is "handle".
          hWnd = FindWindowEx(IntPtr.Zero, hWnd, null, null);
          GetWindowThreadProcessId(hWnd, out prcsId); //get ProcessId from "handle"
    
          //if it matches what we are looking
          //Note there maybe multiple Windows found - so try all of them
          if (prcsId == pr.Id)
            ShowWindow(hWnd, SW_SHOW); //Show Window
        } while (hWnd != IntPtr.Zero);
      }
    }
    
    IntPtr hWnd = pr.MainWindowHandle;
    
    foreach (Process pr in processRunning)
    {
      ShowWindow(pr.MainWindowHandle, SW_HIDE);
    }