Skip to content

Commit af608dc

Browse files
committed
Fix #644 step 1: correctly bring window back to top when clicked
1 parent 472ccc1 commit af608dc

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

QuickLook/ViewerWindow.xaml.cs

+26-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System;
1919
using System.Windows;
2020
using System.Windows.Input;
21+
using System.Windows.Interop;
2122
using System.Windows.Media.Animation;
2223
using QuickLook.Common.ExtensionMethods;
2324
using QuickLook.Common.Helpers;
@@ -55,16 +56,15 @@ internal ViewerWindow()
5556

5657
StateChanged += (sender, e) => _ignoreNextWindowSizeChange = true;
5758

59+
// bring the window to top when users click in the client area.
60+
// the non-client area is handled by the WndProc inside OnSourceInitialized().
61+
PreviewMouseDown += (sender, e) => this.BringToFront(false);
62+
5863
windowFrameContainer.PreviewMouseMove += ShowWindowCaptionContainer;
5964

6065
Topmost = SettingHelper.Get("Topmost", false);
6166
buttonTop.Tag = Topmost ? "Top" : "Auto";
6267

63-
SourceInitialized += (sender, e) => this.SetNoactivate();
64-
65-
// bring the window to top. use together with SetNoactivate()
66-
PreviewMouseDown += (sender, e) => this.BringToFront(false);
67-
6868
buttonTop.Click += (sender, e) =>
6969
{
7070
Topmost = !Topmost;
@@ -103,6 +103,27 @@ internal ViewerWindow()
103103
buttonOpenWith.Click += (sender, e) => ShareHelper.Share(_path, this, true);
104104
}
105105

106+
// bring the window to top when users click in the non-client area.
107+
protected override void OnSourceInitialized(EventArgs e)
108+
{
109+
base.OnSourceInitialized(e);
110+
111+
this.SetNoactivate();
112+
113+
HwndSource.FromHwnd(new WindowInteropHelper(this).Handle)?.AddHook(
114+
(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) =>
115+
{
116+
switch (msg)
117+
{
118+
case 0x0112: // WM_SYSCOMMAND
119+
this.BringToFront(false);
120+
break;
121+
}
122+
123+
return IntPtr.Zero;
124+
});
125+
}
126+
106127
public override void OnApplyTemplate()
107128
{
108129
base.OnApplyTemplate();

0 commit comments

Comments
 (0)