Fix #27: do not stay TopMost

This commit is contained in:
Paddy Xu
2017-06-14 20:58:17 +03:00
parent 3f82076c48
commit ce74c836c4
4 changed files with 47 additions and 11 deletions

View File

@@ -38,6 +38,15 @@ namespace QuickLook.Helpers
new Size(screen.Width / scaleX, screen.Height / scaleY));
}
public static void BringToFront(this Window window)
{
var handle = new WindowInteropHelper(window).Handle;
User32.SetWindowPos(handle, User32.HWND_TOPMOST, 0, 0, 0, 0,
User32.SWP_NOMOVE | User32.SWP_NOSIZE | User32.SWP_NOACTIVATE);
User32.SetWindowPos(handle, User32.HWND_NOTOPMOST, 0, 0, 0, 0,
User32.SWP_NOMOVE | User32.SWP_NOSIZE | User32.SWP_NOACTIVATE);
}
public static void MoveWindow(this Window window,
double left,
double top,
@@ -53,8 +62,7 @@ namespace QuickLook.Helpers
window.TransformToPixels(width, height,
out pxWidth, out pxHeight);
var helper = new WindowInteropHelper(window);
User32.MoveWindow(helper.Handle, pxLeft, pxTop, pxWidth, pxHeight, true);
User32.MoveWindow(new WindowInteropHelper(window).Handle, pxLeft, pxTop, pxWidth, pxHeight, true);
}
private static void TransformToPixels(this Visual visual,

View File

@@ -89,6 +89,10 @@ namespace QuickLook
return;
}
// if this is a new window, place it to top
if (Visibility != Visibility.Visible)
this.BringToFront();
var screen = WindowHelper.GetCurrentWindowRect();
// if the window is visible, place new window in respect to the old center point.

View File

@@ -28,6 +28,10 @@ namespace QuickLook.NativeMethods
public static extern int MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight,
[MarshalAs(UnmanagedType.Bool)] bool bRepaint);
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy,
uint uFlags);
[DllImport("user32.dll")]
internal static extern IntPtr SetWindowsHookEx(int idHook, KeyboardHookProc callback, IntPtr hInstance,
uint threadId);
@@ -78,13 +82,34 @@ namespace QuickLook.NativeMethods
}
// ReSharper disable InconsistentNaming
internal const int WH_KEYBOARD_LL = 13;
internal const int WM_KEYDOWN = 0x100;
internal const int WM_KEYUP = 0x101;
internal const int WM_SYSKEYDOWN = 0x104;
internal const int WM_SYSKEYUP = 0x105;
internal const int GWL_EXSTYLE = -20;
internal const int WS_EX_NOACTIVATE = 0x08000000;
public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
public static readonly IntPtr HWND_TOP = new IntPtr(0);
public static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
public const uint SWP_NOSIZE = 0x0001;
public const uint SWP_NOMOVE = 0x0002;
public const uint SWP_NOZORDER = 0x0004;
public const uint SWP_NOREDRAW = 0x0008;
public const uint SWP_NOACTIVATE = 0x0010;
public const uint SWP_DRAWFRAME = 0x0020;
public const uint SWP_FRAMECHANGED = 0x0020;
public const uint SWP_SHOWWINDOW = 0x0040;
public const uint SWP_HIDEWINDOW = 0x0080;
public const uint SWP_NOCOPYBITS = 0x0100;
public const uint SWP_NOOWNERZORDER = 0x0200;
public const uint SWP_NOREPOSITION = 0x0200;
public const uint SWP_NOSENDCHANGING = 0x0400;
public const uint SWP_DEFERERASE = 0x2000;
public const uint SWP_ASYNCWINDOWPOS = 0x4000;
public const int WH_KEYBOARD_LL = 13;
public const int WM_KEYDOWN = 0x100;
public const int WM_KEYUP = 0x101;
public const int WM_SYSKEYDOWN = 0x104;
public const int WM_SYSKEYUP = 0x105;
public const int GWL_EXSTYLE = -20;
public const int WS_EX_NOACTIVATE = 0x08000000;
// ReSharper restore InconsistentNaming
}
}

View File

@@ -203,8 +203,7 @@ namespace QuickLook
: _viewWindowNoTransparent;
if (!ReferenceEquals(oldWindow, _currentMainWindow))
oldWindow.BeginHide();
_currentMainWindow.Topmost = !Debugger.IsAttached && topMost;
_currentMainWindow.BeginShow(matchedPlugin, _path, CurrentPluginFailed);
}