使用user32.dll获取特定窗口的句柄
本文关键字:窗口 句柄 user32 dll 获取 使用 | 更新日期: 2023-09-27 17:50:27
如何使用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);
}
}