gpt4 book ai didi

c# - 使用 user32.dll 获取特定窗口的句柄

转载 作者:太空狗 更新时间:2023-10-29 21:06:41 25 4
gpt4 key购买 nike

如何使用 user32.dll 获取特定窗口的句柄?

谁能给我一个简短的例子?

最佳答案

尝试以下操作:

// For Windows Mobile, replace user32.dll with coredll.dll
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

// Find window by Caption only. Note you must pass IntPtr.Zero as the first parameter.

[DllImport("user32.dll", EntryPoint="FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

// You can also call FindWindow(default(string), lpWindowName) or FindWindow((string)null, lpWindowName)

您可以按如下方式使用这些声明

// Find window by Caption
public static IntPtr FindWindow(string windowName)
{
var hWnd = FindWindow(windowName, null);
return hWnd;
}

这是代码的简明版本:

public class WindowFinder
{
// For Windows Mobile, replace user32.dll with coredll.dll
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

public static IntPtr FindWindow(string caption)
{
return FindWindow(String.Empty, caption);
}
}

关于c# - 使用 user32.dll 获取特定窗口的句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5712930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com