2016-08-21 51 views
0

在我的应用程序中,我拍摄了桌面的屏幕截图。之前正确的,我躲在我的应用程序的Window,所以它不会覆盖桌面的一部分:WPF - 隐藏窗口后如何立即运行代码

MainWindow.Hide(); 
TakeScreenShot(); 

的问题是,有时窗口没有得到隐藏的速度不够快,所以我最终采取它的屏幕截图太。我试图从Window.IsVisibleChanged事件处理程序中截取屏幕截图,但结果相同。

当然,我可以使用Thread.Sleep或类似的,但我正在寻找更好的解决方案。

更新

我刚切换到Aero主题并启用桌面组合,而现在的情况更糟。现在窗口“淡出”,而不是在拨打Window.Hide时立即隐藏。所以,现在我的应用程序总是截屏的一个衰落窗口...我会尝试与SetWindowPosShowWindow winapi,并会发布更新。

更新2

  1. ShowWindowSetWindowPos和得到相同的结果Window.HideWindow.Hide使用ShowWindow(HWND, SW_HIDE)内部)。

  2. 为了在隐藏窗口时禁用淡出效果,我使用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); 
} 

所以旁边的淡出效果,问题依然存在。

+0

你试过简单的[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

+0

@quetzalcoatl:我试图从Window.Deactivated事件处理程序中截取屏幕截图。结果是一样的。我没有尝试从StateChanged,因为我不想最小化窗口。 – Bohoo

+0

@Bohoo你能告诉我你'TakeScreenShot'是怎么样的吗? – Gopichandar

回答

0

您可以从您试图复制的区域之外(当考虑子窗口恢复问题时)开始放置窗口。

public partial class MainWindow : Window 
{ 

    private DispatcherOperation _action; 
    private int _width = 2000; 
    private int _height = 1000; 
    private double _top; 

    public MainWindow() 
    { 
     InitializeComponent(); 
     Dispatcher.Hooks.OperationCompleted += HooksOnOperationCompleted; 
    } 

    private void HooksOnOperationCompleted(object sender, DispatcherHookEventArgs dispatcherHookEventArgs) 
    { 
     if(dispatcherHookEventArgs.Operation != _action) return; 
     _action.Task.ContinueWith((t) => 
     { 
      Rectangle rect = new Rectangle(0, 0, _width, _height); 
      Bitmap bmp = new Bitmap(rect.Width, rect.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); 
      Graphics g = Graphics.FromImage(bmp); 
      g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy); 
      bmp.Save("help" + DateTime.Now.Ticks + ".jpg", ImageFormat.Jpeg); 
     }).ContinueWith(t => 
     { 
      Dispatcher.BeginInvoke((Action)(() => 
      { 
       Top = _top; 
      })); 
     }); 


    } 

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e) 
    { 
     _top = Top; 
     _action = Dispatcher.BeginInvoke((Action) (() => 
     { 
      Top = 10000; 
     })); 
    } 
} 
+0

感谢您的回答。把窗户放在屏幕外是有效的,但是它是一种黑客......我不知道当我这样做时究竟发生了什么,以及后果如何。由于它是一个生产代码,我宁愿不使用它。你也可以看到我的问题更新。 – Bohoo

+0

好的@Bohoo。但我不明白你说的是什么意思; “当我这样做时究竟发生了什么,以及后果如何”。通过应用Top = Y_POSITION您只需将窗口移动到屏幕上的特定(Y_POSITION)位置。问候。 – Ilan

0

尝试隐藏Window之前尽量减少Window并运行它Asynchronously与最低优先级。像这样的东西。

private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     //Minimize here before hiding. . 
     this.WindowState = WindowState.Minimized; //this is the key 
     this.Visibility = Visibility.Hidden; 
     this.Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle, new Action(() => 
     {     
      CaptureImage(); 
     }));    
    } 

    private void CaptureImage() 
    { 
     System.Drawing.Rectangle bounds = new System.Drawing.Rectangle(0, 0, (int)System.Windows.SystemParameters.PrimaryScreenWidth, (int)System.Windows.SystemParameters.PrimaryScreenHeight); 
     using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height)) 
     { 
      using (Graphics g = Graphics.FromImage(bitmap)) 
      { 
       g.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty, bounds.Size); 
      } 
      bitmap.Save("test.jpg", ImageFormat.Jpeg); 
     } 
    } 

希望这会有所帮助。谢谢。

+0

感谢您的回答。由于涉及到动画,我不想将窗口最小化。使用'DispatcherPriority.SystemIdle'本身并没有帮助。你可以看到我的问题的更新。 – Bohoo