Process.MainWindowHandle的意外行为 [英] Unexpected behaviour of Process.MainWindowHandle

查看:300
本文介绍了Process.MainWindowHandle的意外行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图理解<一个href="http://msdn.microsoft.com/en-gb/library/system.diagnostics.process.mainwindowhandle.aspx">Process.MainWindowHandle.

根据MSDN; 主窗口是当开始的过程。在初始化后,其他的窗口可以被打开,包括模态和顶层的窗户,但创建窗口的与过程相关联的第一窗口保持在主窗口。 (强调)

According to MSDN; "The main window is the window that is created when the process is started. After initialization, other windows may be opened, including the Modal and TopLevel windows, but the first window associated with the process remains the main window." (Emphasis added)

不过,在调试时,我注意到,MainWindowHandle似乎改变价值...我没有料到,特别是咨询上述文件后。

But while debugging I noticed that MainWindowHandle seemed to change value... which I wasn't expecting, especially after consulting the documentation above.

要确认我创建了一个独立的WinForms应用程序具有定时检查devenv的(Visual Studio中)过程中每100ms的MainWindowHandle的行为。

To confirm the behaviour I created a standalone WinForms app with a timer to check the MainWindowHandle of the "DEVENV" (Visual Studio) process every 100ms.

下面是本次测试的应用程序的有趣的部分...

Here's the interesting part of this test app...

    IntPtr oldHWnd = IntPtr.Zero;

    void GetMainwindowHandle()
    {
        Process[] processes = Process.GetProcessesByName("DEVENV");

        if (processes.Length!=1)
            return;

        IntPtr newHWnd = processes[0].MainWindowHandle;

        if (newHWnd != oldHWnd)
        {
            oldHWnd = newHWnd;
            textBox1.AppendText(processes[0].MainWindowHandle.ToString("X")+"\r\n");
        }

    }

    private void timer1Tick(object sender, EventArgs e)
    {
        GetMainwindowHandle();
    }

您可以看到MainWindowHandle改变值时(例如),点击里面VS下拉菜单中。

You can see the value of MainWindowHandle changing when you (for example) click on a drop-down menu inside VS.

也许我误解的文件。

任何人都可以揭示?

推荐答案

@ EDG

我想这是MSDN中的一个错误。你可以清楚地看到Relfector,说:主窗口为您在.NET中的样子:

I guess it's an error in MSDN. You can clearly see in Relfector, that "Main window" check in .NET looks like:

private bool IsMainWindow(IntPtr handle)
{
    return (!(NativeMethods.GetWindow(new HandleRef(this, handle), 4) != IntPtr.Zero)  
             && NativeMethods.IsWindowVisible(new HandleRef(this, handle)));
}

在.NET code枚举窗口,这是pretty的明显,第一个可见窗口(即顶层窗口)将符合这一标准。

When .NET code enumerates windows, it's pretty obvious that first visible window (i.e. top level window) will match this criteria.

这篇关于Process.MainWindowHandle的意外行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆