private bool haveMainWindow = false;
private IntPtr mainWindowHandle = IntPtr.Zero;
private int processId = 0; private void Window_Loaded(object sender, RoutedEventArgs e)
{
///初始化录音句柄等
processId = Process.GetCurrentProcess().Id;
BaseData.hwnd = GetMainWindowHandle(processId);
} private IntPtr GetMainWindowHandle(int processId)
{
if (!haveMainWindow)
{
this.mainWindowHandle = IntPtr.Zero;
this.processId = processId;
User32API.EnumThreadWindowsCallback callback = new User32API.EnumThreadWindowsCallback(this.EnumWindowsCallback);
User32API.EnumWindows(callback, IntPtr.Zero);
GC.KeepAlive(callback); haveMainWindow = true;
}
return this.mainWindowHandle;
} private bool EnumWindowsCallback(IntPtr handle, IntPtr extraParameter)
{
int num;
User32API.GetWindowThreadProcessId(new HandleRef(this, handle), out num);
if ((num == this.processId) && this.IsMainWindow(handle))
{
this.mainWindowHandle = handle;
return false;
}
return true;
} private bool IsMainWindow(IntPtr handle)
{
return (!(User32API.GetWindow(new HandleRef(this, handle), 4) != IntPtr.Zero) && User32API.IsWindowVisible(new HandleRef(this, handle)));
}
private IntPtr mainWindowHandle = IntPtr.Zero;
private int processId = 0; private void Window_Loaded(object sender, RoutedEventArgs e)
{
///初始化录音句柄等
processId = Process.GetCurrentProcess().Id;
BaseData.hwnd = GetMainWindowHandle(processId);
} private IntPtr GetMainWindowHandle(int processId)
{
if (!haveMainWindow)
{
this.mainWindowHandle = IntPtr.Zero;
this.processId = processId;
User32API.EnumThreadWindowsCallback callback = new User32API.EnumThreadWindowsCallback(this.EnumWindowsCallback);
User32API.EnumWindows(callback, IntPtr.Zero);
GC.KeepAlive(callback); haveMainWindow = true;
}
return this.mainWindowHandle;
} private bool EnumWindowsCallback(IntPtr handle, IntPtr extraParameter)
{
int num;
User32API.GetWindowThreadProcessId(new HandleRef(this, handle), out num);
if ((num == this.processId) && this.IsMainWindow(handle))
{
this.mainWindowHandle = handle;
return false;
}
return true;
} private bool IsMainWindow(IntPtr handle)
{
return (!(User32API.GetWindow(new HandleRef(this, handle), 4) != IntPtr.Zero) && User32API.IsWindowVisible(new HandleRef(this, handle)));
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货