From 10254c8af7e646bdb176a0b6e4fac0845c59f029 Mon Sep 17 00:00:00 2001
From: chylex <contact@chylex.com>
Date: Thu, 31 May 2018 02:58:25 +0200
Subject: [PATCH] Fix tweet screenshots with Aero disabled by making the window
 visible

Closes #223
---
 .../Screenshot/FormNotificationScreenshotable.cs       |  4 ++++
 Core/Utils/NativeMethods.cs                            |  3 +++
 Core/Utils/WindowsUtils.cs                             | 10 +++++++---
 3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/Core/Notification/Screenshot/FormNotificationScreenshotable.cs b/Core/Notification/Screenshot/FormNotificationScreenshotable.cs
index 802ca412..03968951 100644
--- a/Core/Notification/Screenshot/FormNotificationScreenshotable.cs
+++ b/Core/Notification/Screenshot/FormNotificationScreenshotable.cs
@@ -68,6 +68,10 @@ public bool TakeScreenshot(bool ignoreHeightError = false){
                     return false;
                 }
             }
+
+            if (!WindowsUtils.IsAeroEnabled){
+                MoveToVisibleLocation(); // TODO make this look nicer I guess
+            }
             
             IntPtr context = NativeMethods.GetDC(this.Handle);
 
diff --git a/Core/Utils/NativeMethods.cs b/Core/Utils/NativeMethods.cs
index 5f1c6368..0b3b3500 100644
--- a/Core/Utils/NativeMethods.cs
+++ b/Core/Utils/NativeMethods.cs
@@ -71,6 +71,9 @@ private struct MSLLHOOKSTRUCT{
         [return: MarshalAs(UnmanagedType.Bool)]
         private static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);
         
+        [DllImport("dwmapi.dll")]
+        public static extern int DwmIsCompositionEnabled(out bool enabled);
+        
         [DllImport("user32.dll")]
         [return: MarshalAs(UnmanagedType.Bool)]
         public static extern bool ShowScrollBar(IntPtr hWnd, int wBar, bool bShow);
diff --git a/Core/Utils/WindowsUtils.cs b/Core/Utils/WindowsUtils.cs
index 87035959..80dd9213 100644
--- a/Core/Utils/WindowsUtils.cs
+++ b/Core/Utils/WindowsUtils.cs
@@ -15,10 +15,12 @@ static class WindowsUtils{
         private static readonly Lazy<Regex> RegexStripHtmlStyles = new Lazy<Regex>(() => new Regex(@"\s?(?:style|class)="".*?"""), false);
         private static readonly Lazy<Regex> RegexOffsetClipboardHtml = new Lazy<Regex>(() => new Regex(@"(?<=EndHTML:|EndFragment:)(\d+)"), false);
 
+        private static readonly bool IsWindows8OrNewer;
+        private static bool HasMicrosoftBeenBroughtTo2008Yet;
+
         public static int CurrentProcessID { get; }
         public static bool ShouldAvoidToolWindow { get; }
-        
-        private static bool HasMicrosoftBeenBroughtTo2008Yet;
+        public static bool IsAeroEnabled => IsWindows8OrNewer || (NativeMethods.DwmIsCompositionEnabled(out bool isCompositionEnabled) == 0 && isCompositionEnabled);
 
         static WindowsUtils(){
             using(Process me = Process.GetCurrentProcess()){
@@ -26,7 +28,9 @@ static WindowsUtils(){
             }
 
             Version ver = Environment.OSVersion.Version;
-            ShouldAvoidToolWindow = ver.Major == 6 && ver.Minor == 2; // windows 8/10
+            IsWindows8OrNewer = ver.Major == 6 && ver.Minor == 2; // windows 8/10
+
+            ShouldAvoidToolWindow = IsWindows8OrNewer;
         }
         
         public static void EnsureTLS12(){