在我的应用程序中,我拍摄了桌面的屏幕截图。之前正确的,我躲在我的应用程序的Window
,所以它不会覆盖桌面的一部分:WPF - 隐藏窗口后如何立即运行代码
MainWindow.Hide();
TakeScreenShot();
的问题是,有时窗口没有得到隐藏的速度不够快,所以我最终采取它的屏幕截图太。我试图从Window.IsVisibleChanged
事件处理程序中截取屏幕截图,但结果相同。
当然,我可以使用Thread.Sleep
或类似的,但我正在寻找更好的解决方案。
更新
我刚切换到Aero主题并启用桌面组合,而现在的情况更糟。现在窗口“淡出”,而不是在拨打Window.Hide
时立即隐藏。所以,现在我的应用程序总是截屏的一个衰落窗口...我会尝试与SetWindowPos
或ShowWindow
winapi,并会发布更新。
更新2
ShowWindow
SetWindowPos
和得到相同的结果Window.Hide
(Window.Hide
使用ShowWindow(HWND, SW_HIDE)
内部)。为了在隐藏窗口时禁用淡出效果,我使用
DwmSetWindowAttribute
。
这样的:
using System.Windows.Interop;
using System.Runtime.InteropServices;
[DllImport("dwmapi.dll")]
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
private const int DWMWA_TRANSITIONS_FORCEDISABLED = 3;
,并在Window.Loaded
事件处理程序:
if (Environment.OSVersion.Version.Major >= 6) {
IntPtr WinHandle = new WindowInteropHelper(this).Handle;
int BOOL_TRUE = 1;
int HR = DwmSetWindowAttribute(WinHandle, DWMWA_TRANSITIONS_FORCEDISABLED, BOOL_TRUE, Marshal.SizeOf(BOOL_TRUE));
if (HR != 0)
Marshal.ThrowExceptionForHR(HR);
}
所以旁边的淡出效果,问题依然存在。
你试过简单的[OnDeactivated](https://msdn.microsoft.com/en-us/library/system.windows.window.deactivated(v = vs.110).aspx)吗?或[OnStateChanged](https://msdn.microsoft.com/en-us/library/system.windows.window.statechanged(v = vs.110).aspx)与过滤器检查“最小化”状态? – quetzalcoatl
@quetzalcoatl:我试图从Window.Deactivated事件处理程序中截取屏幕截图。结果是一样的。我没有尝试从StateChanged,因为我不想最小化窗口。 – Bohoo
@Bohoo你能告诉我你'TakeScreenShot'是怎么样的吗? – Gopichandar