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

Tim*_*mwi 7 c# windows winapi

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

Pinvoke通常给我一个必要的DllImport声明来使用C#中的WinAPI函数.但是,因为GetNextWindow()它没有条目.所以我试着构建自己的:

[DllImport("user32.dll")]
static extern IntPtr GetNextWindow(IntPtr hWnd, uint wCmd);
Run Code Online (Sandbox Code Playgroud)

不幸的是,当试图打电话给我时,我得到一个EntryPointNotFoundException说法:

Unable to find an entry point named 'GetNextWindow' in DLL 'user32.dll'.
Run Code Online (Sandbox Code Playgroud)

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

当然,如果您可以建议一种完全不同的方式来枚举当前Z顺序中的窗口,我也很高兴听到这一点.

Dav*_*own 25

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
}
Run Code Online (Sandbox Code Playgroud)

(来自Pinvoke.net)