通过调用Win32 API实现。
调用User32API.GetCurrentWindowHandle()即可返回当前进程的主窗口句柄,如果获取失败则返回IntPtr.Zero。
2008年10月7日补充:微软实现的获取进程主窗口句柄代码
通过调用Win32 API实现。
public class User32API
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
private static Hashtable processWnd = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
static User32API()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (processWnd == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
processWnd = new Hashtable();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DllImport("user32.dll", EntryPoint = "EnumWindows", SetLastError = true)]
public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
public static extern IntPtr GetParent(IntPtr hWnd);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DllImport("user32.dll", EntryPoint = "IsWindow")]
public static extern bool IsWindow(IntPtr hWnd);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DllImport("kernel32.dll", EntryPoint = "SetLastError")]
public static extern void SetLastError(uint dwErrCode);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static IntPtr GetCurrentWindowHandle()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
IntPtr ptrWnd = IntPtr.Zero;
uint uiPid = (uint)Process.GetCurrentProcess().Id; // 当前进程 ID
object objWnd = processWnd[uiPid];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (objWnd != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ptrWnd = (IntPtr)objWnd;
if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd)) // 从缓存中获取句柄
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return ptrWnd;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ptrWnd = IntPtr.Zero;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);
// 枚举窗口返回 false 并且没有错误号时表明获取成功
if (!bResult && Marshal.GetLastWin32Error() == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
objWnd = processWnd[uiPid];
if (objWnd != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ptrWnd = (IntPtr)objWnd;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return ptrWnd;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static bool EnumWindowsProc(IntPtr hwnd, uint lParam)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
uint uiPid = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (GetParent(hwnd) == IntPtr.Zero)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
GetWindowThreadProcessId(hwnd, ref uiPid);
if (uiPid == lParam) // 找到进程对应的主窗口句柄
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
processWnd[uiPid] = hwnd; // 把句柄缓存起来
SetLastError(0); // 设置无错误
return false; // 返回 false 以终止枚举窗口
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return true;
}
}
调用User32API.GetCurrentWindowHandle()即可返回当前进程的主窗口句柄,如果获取失败则返回IntPtr.Zero。
--EOF--
2008年10月7日补充:微软实现的获取进程主窗口句柄代码
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
public class MyProcess
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
private bool haveMainWindow = false;
private IntPtr mainWindowHandle = IntPtr.Zero;
private int processId = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public IntPtr GetMainWindowHandle(int processId)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!this.haveMainWindow)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.mainWindowHandle = IntPtr.Zero;
this.processId = processId;
EnumThreadWindowsCallback callback = new EnumThreadWindowsCallback(this.EnumWindowsCallback);
EnumWindows(callback, IntPtr.Zero);
GC.KeepAlive(callback);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.haveMainWindow = true;
}
return this.mainWindowHandle;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private bool EnumWindowsCallback(IntPtr handle, IntPtr extraParameter)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int num;
GetWindowThreadProcessId(new HandleRef(this, handle), out num);
if ((num == this.processId) && this.IsMainWindow(handle))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.mainWindowHandle = handle;
return false;
}
return true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private bool IsMainWindow(IntPtr handle)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return (!(GetWindow(new HandleRef(this, handle), 4) != IntPtr.Zero) && IsWindowVisible(new HandleRef(this, handle)));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowThreadProcessId(HandleRef handle, out int processId);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetWindow(HandleRef hWnd, int uCmd);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool IsWindowVisible(HandleRef hWnd);
}