Skip to content

Commit

Permalink
Merge pull request #45 from Aelarion/main
Browse files Browse the repository at this point in the history
Fix double-click maximize issue referenced by #43
  • Loading branch information
pomianowski committed Jan 27, 2022
2 parents cfedcf2 + a6bd85c commit d563784
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
23 changes: 12 additions & 11 deletions WPFUI/Controls/TitleBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using WPFUI.Win32;

namespace WPFUI.Controls
{
Expand All @@ -19,6 +20,8 @@ public class TitleBar : UserControl
{
private Window _parent;

private User32.POINT _doubleClickPoint;

private Tray.NotifyIcon _notifyIcon;

private Common.SnapLayout _snapLayout;
Expand Down Expand Up @@ -457,7 +460,6 @@ private void TitleBar_Loaded(object sender, RoutedEventArgs e)

if (rootGrid != null)
{
rootGrid.MouseDown += RootGrid_MouseDown;
rootGrid.MouseLeftButtonDown += RootGrid_MouseLeftButtonDown;
rootGrid.MouseMove += RootGrid_MouseMove;
}
Expand All @@ -470,6 +472,10 @@ private void RootGrid_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton != MouseButtonState.Pressed || ParentWindow == null) return;

// prevent firing from double clicking when the mouse never actually moved
User32.GetCursorPos(out var currentMousePos);
if (currentMousePos.X == _doubleClickPoint.X && currentMousePos.Y == _doubleClickPoint.Y) return;

if (IsMaximized)
{
var screenPoint = PointToScreen(e.MouseDevice.GetPosition(this));
Expand All @@ -490,9 +496,9 @@ private void RootGrid_MouseMove(object sender, MouseEventArgs e)
ParentWindow.WindowStyle = WindowStyle.None;
ParentWindow.WindowState = WindowState.Normal;
ParentWindow.WindowStyle = style;

ParentWindow.DragMove();
}

ParentWindow.DragMove();
}

private void ParentWindow_StateChanged(object sender, EventArgs e)
Expand All @@ -503,18 +509,13 @@ private void ParentWindow_StateChanged(object sender, EventArgs e)
IsMaximized = ParentWindow.WindowState == WindowState.Maximized;
}

private void RootGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton != MouseButton.Left)
return;

ParentWindow.DragMove();
}

private void RootGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
User32.GetCursorPos(out _doubleClickPoint);
MaximizeWindow();
}
}

private void TemplateButton_OnClick(TitleBar sender, object parameter)
Expand Down
25 changes: 25 additions & 0 deletions WPFUI/Win32/User32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,31 @@ public struct WINCOMPATTRDATA
public int SizeOfData;
}

/// <summary>
/// The POINT structure defines the x- and y-coordinates of a point.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
/// <summary>
/// Specifies the x-coordinate of the point.
/// </summary>
public int X;
/// <summary>
/// Specifies the y-coordinate of the point.
/// </summary>
public int Y;
}

/// <summary>
/// Retrieves the position of the mouse cursor, in screen coordinates.
/// </summary>
/// <param name="lpPoint">A <see cref="POINT"/> structure that receives the screen coordinates of the cursor.</param>
/// <returns></returns>
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetCursorPos(out POINT lpPoint);

/// <summary>
/// Sets various information regarding DWM window attributes.
/// </summary>
Expand Down

0 comments on commit d563784

Please sign in to comment.