Unity3D 得到 主窗口句柄
2016-12-07 15:29
656 查看
public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);
[DllImport("kernel32.dll")]
public static extern void SetLastError(uint dwErrCode);
public static IntPtr GetProcessWnd()
{
IntPtr ptrWnd = IntPtr.Zero;
uint pid = (uint)Process.GetCurrentProcess().Id; // 当前进程 ID
bool bResult = EnumWindows(new WNDENUMPROC(delegate (IntPtr hwnd, uint lParam)
{
uint id = 0;
if (GetParent(hwnd) == IntPtr.Zero)
{
GetWindowThreadProcessId(hwnd, ref id);
if (id == lParam) // 找到进程对应的主窗口句柄
{
ptrWnd = hwnd; // 把句柄缓存起来
SetLastError(0); // 设置无错误
return false; // 返回 false 以终止枚举窗口
}
}
return true;
}), pid);
return (!bResult && Marshal.GetLastWin32Error() == 0) ? ptrWnd : IntPtr.Zero;
}
[DllImport("user32.dll", SetLastError = true)]
public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);
[DllImport("kernel32.dll")]
public static extern void SetLastError(uint dwErrCode);
public static IntPtr GetProcessWnd()
{
IntPtr ptrWnd = IntPtr.Zero;
uint pid = (uint)Process.GetCurrentProcess().Id; // 当前进程 ID
bool bResult = EnumWindows(new WNDENUMPROC(delegate (IntPtr hwnd, uint lParam)
{
uint id = 0;
if (GetParent(hwnd) == IntPtr.Zero)
{
GetWindowThreadProcessId(hwnd, ref id);
if (id == lParam) // 找到进程对应的主窗口句柄
{
ptrWnd = hwnd; // 把句柄缓存起来
SetLastError(0); // 设置无错误
return false; // 返回 false 以终止枚举窗口
}
}
return true;
}), pid);
return (!bResult && Marshal.GetLastWin32Error() == 0) ? ptrWnd : IntPtr.Zero;
}
相关文章推荐
- 怎样得到窗口句柄
- 如何得到所有可视化窗口的句柄?
- VC++中有关句柄和指针及其转换 1.MFC窗口的句柄和指针的转换 (1) 一般窗口对象都会有一个其对应的句柄变量,所以我们可以取此对象的m_hWnd属性来得到句柄。 (2)
- 从窗口句柄得到菜单句柄(从HWND得到HMENU)
- 【LibUIDK界面库系列文章】得到菜单的菜单窗口句柄
- 创建进程后得到该进程的主窗口句柄
- DELPHI 通過窗口句柄或窗口标题得到进程句柄
- 从窗口句柄得到窗口的对象指针
- 从窗口句柄得到窗口的对象指针 .
- 得到当前窗口句柄
- VB6: 通过窗口句柄得到窗口所在程序的名称和路径(实例)
- 玫举窗口句柄,然后用窗口句柄去取得进程ID,比较得到的进程ID是不是你的进程ID....
- 如何得到桌面窗口的句柄(桌面图标)
- 得到控制台窗口的句柄
- 根据process id和窗口名得到窗口句柄
- 得到控制台窗口的句柄
- VC怎么通过进程ID得到窗口句柄
- 如何从窗口句柄得到与窗口关联的文件路径/文件名
- 如何得到句柄、窗口句柄
- 已知IE窗口的句柄,如何由此得到IHTMLdocument2接口?