internal class MainWindowFinder
{
// Fields
private IntPtr bestHandle;
private int processId;
// Methods
private bool EnumWindowsCallback(IntPtr handle, IntPtr extraParameter)
{
int num;
NativeMethods.GetWindowThreadProcessId(new HandleRef(this, handle), out num);
if ((num == this.processId) && this.IsMainWindow(handle))
{
this.bestHandle = handle;
return false;
}
return true;
}
public IntPtr FindMainWindow(int processId)
{
this.bestHandle = IntPtr.Zero;
this.processId = processId;
NativeMethods.EnumThreadWindowsCallback callback = new NativeMethods.EnumThreadWindowsCallback(this.EnumWindowsCallback);
NativeMethods.EnumWindows(callback, IntPtr.Zero);
GC.KeepAlive(callback);
return this.bestHandle;
}
private bool IsMainWindow(IntPtr handle)
{
return (!(NativeMethods.GetWindow(new HandleRef(this, handle), 4) != IntPtr.Zero) && NativeMethods.IsWindowVisible(new HandleRef(this, handle)));
}
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。