IntPtr始终为零C#

时间:2016-06-15 14:43:04

标签: c#

我正在启动Internet Explorer程序。问题是它总是在p.MainWindowHandle中返回零。 我的目标是获取mainwindowHandler并最小化刚刚启动的特定窗口。相同的代码正在使用chrome浏览器。但在Internet Explorer中,它无法正常工作。 我的代码如下。

Process p = Process.Start("IEXPLORE.EXE", "www.google.com");
ShowWindow(p.MainWindowHandle, 2);

ShowWindow是一个方法调整大小窗口。

1 个答案:

答案 0 :(得分:1)

在众多其他原因中(请参阅问题评论),您必须等待才能将进程创建主窗口:

  // Process is IDisposable, wrap it into using
  using (Process p = Process.Start("IEXPLORE.EXE", "www.google.com")) {
    ...
    // wait till the process creates the main window
    p.WaitForInputIdle();

    // Main window (if any) has been created, so we can ask for its handle
    ShowWindow(p.MainWindowHandle, 2);
  }