在我的應用程序中,我拍攝了桌面的屏幕截圖。之前正確的,我躲在我的應用程序的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