如何在 C# 中使用 GetNextWindow()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/798295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 01:37:01  来源:igfitidea点击:

How can I use GetNextWindow() in C#?

c#windowswinapi

提问by Timwi

The Microsoft WinAPI documentation appears to suggest that user32.dll contains a function called GetNextWindow()which supposedly allows one to enumerate open windows in their Z order by calling this function repeatedly.

Microsoft WinAPI 文档似乎表明 user32.dll 包含一个被调用的函数GetNextWindow(),据称该函数允许通过重复调用此函数以 Z 顺序枚举打开的窗口。

Pinvokeusually gives me the necessary DllImportstatement to use WinAPI functions from C#. However, for GetNextWindow()it doesn't have an entry. So I tried to construct my own:

Pinvoke通常会为我DllImport提供使用 C# 中的 WinAPI 函数的必要语句。但是,因为GetNextWindow()它没有条目。所以我尝试构建自己的:

[DllImport("user32.dll")]
static extern IntPtr GetNextWindow(IntPtr hWnd, uint wCmd);

Unfortunately, when trying to call this, I get an EntryPointNotFoundExceptionsaying:

不幸的是,当试图调用它时,我得到了一个EntryPointNotFoundException说法:

Unable to find an entry point named 'GetNextWindow' in DLL 'user32.dll'.

This seems to apply only to GetNextWindow(); other functions that are listed on Pinvoke are fine. I can call GetTopWindow()and GetWindowText()without throwing an exception.

这似乎只适用于GetNextWindow(); Pinvoke 上列出的其他功能都很好。我可以调用GetTopWindow()并且GetWindowText()不会抛出异常。

Of course, if you can suggest a completely different way to enumerate windows in their current Z order, I'm happy to hear that too.

当然,如果您可以提出一种完全不同的方法来按当前 Z 顺序枚举窗口,我也很高兴听到这个消息。

采纳答案by David Brown

GetNextWindow()is actually a macro for GetWindow(), rather than an actual API method. It's for backward compatibility with the Win16 API.

GetNextWindow()实际上是GetWindow()的宏,而不是实际的 API 方法。它是为了与 Win16 API 向后兼容。

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);

enum GetWindow_Cmd : uint {
    GW_HWNDFIRST = 0,
    GW_HWNDLAST = 1,
    GW_HWNDNEXT = 2,
    GW_HWNDPREV = 3,
    GW_OWNER = 4,
    GW_CHILD = 5,
    GW_ENABLEDPOPUP = 6
}

(From Pinvoke.net)

(来自Pinvoke.net

回答by Richard Szalay

GetNextWindow is a c++ macro that calls GetWindow, so you cannot call it from .NET. Call GetWindow instead.

GetNextWindow 是一个调用 GetWindow 的 C++ 宏,因此您不能从 .NET 调用它。改为调用 GetWindow。

From MSDN:

MSDN

Using this function is the same as calling the GetWindow function with the GW_HWNDNEXT or GW_HWNDPREV flag set

使用此函数与调用设置了 GW_HWNDNEXT 或 GW_HWNDPREV 标志的 GetWindow 函数相同