Skip to content

Commit

Permalink
Fixing maximize and window move
Browse files Browse the repository at this point in the history
  • Loading branch information
Darrell Swiney committed Jul 6, 2023
1 parent d771696 commit 36196b5
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 19 deletions.
15 changes: 8 additions & 7 deletions Prepper/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
WindowStyle="None"
Icon="Images/prepper.ico"
Loaded="Window_Loaded"
SourceInitialized="Window_SourceInitialized"
>
<Window.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
Expand All @@ -42,7 +43,7 @@
<RowDefinition Height="100*" />
<RowDefinition Height="65"/>
</Grid.RowDefinitions>
<Grid Name="ControlBox" Grid.Row="0" Grid.ColumnSpan="4" MouseLeftButtonDown="ControlBox_MouseLeftButtonDown" Background="#1B2626">
<Grid Name="ControlBox" Grid.Row="0" Grid.ColumnSpan="4" MouseLeftButtonDown="rctHeader_PreviewMouseLeftButtonDown" PreviewMouseLeftButtonUp="rctHeader_PreviewMouseLeftButtonUp" PreviewMouseMove="rctHeader_PreviewMouseMove" Background="#1B2626">
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
Expand All @@ -52,14 +53,14 @@
</Grid.ColumnDefinitions>
<TextBlock Name="WindowTitle" Grid.Column="0" FontFamily="MS San Serif" FontSize="16" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="#FFA5D6B7" Margin="10,0,0,0">Prepper</TextBlock>
<StackPanel Margin="0,0,5,0" x:Name="WindowControlPanel" Grid.Column="1" HorizontalAlignment="Right" Orientation="Horizontal">
<Border Margin="0,0,2,0" BorderThickness="1" Width="15" Height="15" BorderBrush="Transparent" CornerRadius="9" Background="Yellow" Opacity="0.65" >
<Button Name="MinimizeButton" Click="MinimizeButton_Click" FontFamily="MS Sans Serif" HorizontalAlignment="Right" VerticalAlignment="Center" Background="Transparent" Width="18" Height="18" BorderThickness="0" Foreground="LightSlateGray"/>
<Border Margin="0,0,2,0" Panel.ZIndex="1000" BorderThickness="1" Width="15" Height="15" BorderBrush="Transparent" CornerRadius="9" Background="Yellow" Opacity="0.65" >
<Button Margin="0,0,-1,0" Name="MinimizeButton" Click="MinimizeButton_Click" FontFamily="MS Sans Serif" HorizontalAlignment="Right" VerticalAlignment="Center" Background="Transparent" Width="15" Height="15" BorderThickness="0" Foreground="LightSlateGray"/>
</Border>
<Border Margin="2,0,2,0" BorderThickness="1" Width="15" Height="15" BorderBrush="Transparent" CornerRadius="9" Background="Green" Opacity="0.65" >
<Button Name="MaximizeButton" Click="MaximizeButton_Click" FontFamily="MS Sans Serif" HorizontalAlignment="Right" VerticalAlignment="Center" Background="Transparent" Width="18" Height="18" BorderThickness="0" Foreground="LightSlateGray"/>
<Border Margin="2,0,2,0" Panel.ZIndex="1000" BorderThickness="1" Width="15" Height="15" BorderBrush="Transparent" CornerRadius="9" Background="Green" Opacity="0.65" >
<Button Name="MaximizeButton" Click="MaximizeButton_Click" Margin="0,0,-1,0" FontFamily="MS Sans Serif" HorizontalAlignment="Right" VerticalAlignment="Center" Background="Transparent" Width="15" Height="15" BorderThickness="0" Foreground="LightSlateGray"/>
</Border>
<Border Margin="2,0,0,0" BorderThickness="1" Width="15" Height="15" BorderBrush="Transparent" CornerRadius="9" Background="Red" Opacity="0.65" >
<Button Name="CloseButton" Click="CloseButton_Click" FontFamily="MS Sans Serif" HorizontalAlignment="Right" VerticalAlignment="Center" Background="Transparent" Width="18" Height="18" BorderThickness="0" Foreground="LightSlateGray" />
<Border Margin="2,0,0,0" Panel.ZIndex="1000" BorderThickness="1" Width="15" Height="15" BorderBrush="Transparent" CornerRadius="9" Background="Red" Opacity="0.65" >
<Button Name="CloseButton" Click="CloseButton_Click" Margin="0,0,-1,0" FontFamily="MS Sans Serif" HorizontalAlignment="Right" VerticalAlignment="Center" Background="Transparent" Width="15" Height="15" BorderThickness="0" Foreground="LightSlateGray" />
</Border>
</StackPanel>
</Grid>
Expand Down
206 changes: 194 additions & 12 deletions Prepper/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;

namespace Prepper
{
Expand All @@ -20,6 +23,7 @@ public partial class MainWindow : Window
private const string TheTvDbPinKeyEnvironmentVariableName = "TvDbPin";
private const string TheTvDbSubscribeUrl = "https://thetvdb.com/subscribe";
private ReferenceSelectionControl selectionControl;
private bool mRestoreIfMove = false;

public MainViewModel ViewModel { get; set; }
public MainWindow()
Expand Down Expand Up @@ -109,12 +113,7 @@ private void MinimizeButton_Click(object sender, RoutedEventArgs e)

private void CloseButton_Click(object sender, RoutedEventArgs e)
{
Close();
}

private void ControlBox_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
DragMove();
Application.Current.Shutdown();
}

private void SelectedDirectoryTextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
Expand Down Expand Up @@ -152,7 +151,7 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
{
Process.Start(new ProcessStartInfo(TheTvDbSubscribeUrl) { UseShellExecute = true });
}
Close();
Application.Current.Shutdown();
return;
}
}
Expand All @@ -173,7 +172,7 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
if (tvDb == null)
{
MessageBox.Show("Unable to get TvDbProvider, closing application");
Close();
Application.Current.Shutdown();
return;
}
this.Opacity = 1;
Expand Down Expand Up @@ -217,13 +216,196 @@ private void ReorderListButton_Click(object sender, RoutedEventArgs e)

private void MaximizeButton_Click(object sender, RoutedEventArgs e)
{
if (this.WindowState.Equals(WindowState.Normal))
SwitchWindowState();
}

void Window_SourceInitialized(object sender, EventArgs e)
{
IntPtr mWindowHandle = (new WindowInteropHelper(this)).Handle;
HwndSource.FromHwnd(mWindowHandle).AddHook(new HwndSourceHook(WindowProc));
}

private static System.IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
switch (msg)
{
case 0x0024:
WmGetMinMaxInfo(hwnd, lParam);
break;
}

return IntPtr.Zero;
}


private static void WmGetMinMaxInfo(System.IntPtr hwnd, System.IntPtr lParam)
{
POINT lMousePosition;
GetCursorPos(out lMousePosition);

IntPtr lPrimaryScreen = MonitorFromPoint(new POINT(0, 0), MonitorOptions.MONITOR_DEFAULTTOPRIMARY);
MONITORINFO lPrimaryScreenInfo = new MONITORINFO();
if (GetMonitorInfo(lPrimaryScreen, lPrimaryScreenInfo) == false)
{
return;
}

IntPtr lCurrentScreen = MonitorFromPoint(lMousePosition, MonitorOptions.MONITOR_DEFAULTTONEAREST);

MINMAXINFO lMmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO))!;

if (lPrimaryScreen.Equals(lCurrentScreen) == true)
{
lMmi.ptMaxPosition.X = lPrimaryScreenInfo.rcWork.Left;
lMmi.ptMaxPosition.Y = lPrimaryScreenInfo.rcWork.Top;
lMmi.ptMaxSize.X = lPrimaryScreenInfo.rcWork.Right - lPrimaryScreenInfo.rcWork.Left;
lMmi.ptMaxSize.Y = lPrimaryScreenInfo.rcWork.Bottom - lPrimaryScreenInfo.rcWork.Top;
}
else
{
lMmi.ptMaxPosition.X = lPrimaryScreenInfo.rcMonitor.Left;
lMmi.ptMaxPosition.Y = lPrimaryScreenInfo.rcMonitor.Top;
lMmi.ptMaxSize.X = lPrimaryScreenInfo.rcMonitor.Right - lPrimaryScreenInfo.rcMonitor.Left;
lMmi.ptMaxSize.Y = lPrimaryScreenInfo.rcMonitor.Bottom - lPrimaryScreenInfo.rcMonitor.Top;
}

Marshal.StructureToPtr(lMmi, lParam, true);
}

private void SwitchWindowState()
{
switch (WindowState)
{
case WindowState.Normal:
{
WindowState = WindowState.Maximized;
break;
}
case WindowState.Maximized:
{
WindowState = WindowState.Normal;
break;
}
}
}

private void rctHeader_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
if ((ResizeMode == ResizeMode.CanResize) || (ResizeMode == ResizeMode.CanResizeWithGrip))
{
SwitchWindowState();
}

return;
}

else if (WindowState == WindowState.Maximized)
{
mRestoreIfMove = true;
return;
}

DragMove();
}


private void rctHeader_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
mRestoreIfMove = false;
}

private void rctHeader_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (mRestoreIfMove)
{
this.WindowState = WindowState.Maximized;
mRestoreIfMove = false;

double percentHorizontal = e.GetPosition(this).X / ActualWidth;
double targetHorizontal = RestoreBounds.Width * percentHorizontal;

double percentVertical = e.GetPosition(this).Y / ActualHeight;
double targetVertical = RestoreBounds.Height * percentVertical;

WindowState = WindowState.Normal;

POINT lMousePosition;
GetCursorPos(out lMousePosition);

//Left = lMousePosition.X - targetHorizontal;
//Top = lMousePosition.Y - targetVertical;

DragMove();
}
else
}

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetCursorPos(out POINT lpPoint);


[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr MonitorFromPoint(POINT pt, MonitorOptions dwFlags);

enum MonitorOptions : uint
{
MONITOR_DEFAULTTONULL = 0x00000000,
MONITOR_DEFAULTTOPRIMARY = 0x00000001,
MONITOR_DEFAULTTONEAREST = 0x00000002
}


[DllImport("user32.dll")]
static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);


[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;

public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
}


[StructLayout(LayoutKind.Sequential)]
public struct MINMAXINFO
{
public POINT ptReserved;
public POINT ptMaxSize;
public POINT ptMaxPosition;
public POINT ptMinTrackSize;
public POINT ptMaxTrackSize;
};


[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class MONITORINFO
{
public int cbSize = Marshal.SizeOf(typeof(MONITORINFO));
public RECT rcMonitor = new RECT();
public RECT rcWork = new RECT();
public int dwFlags = 0;
}


[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left, Top, Right, Bottom;

public RECT(int left, int top, int right, int bottom)
{
this.WindowState = WindowState.Normal;
this.Left = left;
this.Top = top;
this.Right = right;
this.Bottom = bottom;
}
}
}
Expand Down

0 comments on commit 36196b5

Please sign in to comment.