Skip to content

Commit

Permalink
Fix issue( #136
Browse files Browse the repository at this point in the history
Fix issue( #136
  • Loading branch information
yanjinhuagood committed Jan 19, 2025
1 parent b89896d commit 9191619
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/WPFDevelopers.Net40/Themes/Generic.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<Setter Property="UseLayoutRounding" Value="True" />
<Setter Property="TextOptions.TextFormattingMode" Value="Ideal" />
<Setter Property="WindowStyle" Value="None" />
<Setter Property="MaxHeight" Value="{x:Static SystemParameters.MaximizedPrimaryScreenHeight}" />
<!--<Setter Property="MaxHeight" Value="{x:Static SystemParameters.MaximizedPrimaryScreenHeight}" />-->
<Setter Property="FontFamily" Value="{DynamicResource WD.NormalFontFamily}" />
<Setter Property="shell:WindowChrome.WindowChrome">
<Setter.Value>
Expand Down
67 changes: 32 additions & 35 deletions src/WPFDevelopers.Net40/Window.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Microsoft.Windows.Shell;
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using WPFDevelopers.Controls;
using WPFDevelopers.Helpers;
using WPFDevelopers.Core.Helpers;
using static WPFDevelopers.Core.Helpers.MonitorHelper;

namespace WPFDevelopers.Net40
{
Expand Down Expand Up @@ -48,8 +48,8 @@ public Window()
CanMinimizeWindow));
CommandBindings.Add(new CommandBinding(SystemCommands.RestoreWindowCommand, RestoreWindow,
CanResizeWindow));

}

public override void OnApplyTemplate()
{
base.OnApplyTemplate();
Expand All @@ -62,6 +62,7 @@ public override void OnApplyTemplate()
}
}


private void Icon_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
Expand Down Expand Up @@ -102,7 +103,7 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
{
hWnd = new WindowInteropHelper(this).Handle;
HwndSource.FromHwnd(hWnd).AddHook(WindowProc);
if(TitleBarMode == TitleBarMode.Normal)
if (TitleBarMode == TitleBarMode.Normal)
TitleHeight = SystemParameters2.Current.WindowNonClientFrameThickness.Top;
}

Expand Down Expand Up @@ -132,52 +133,48 @@ private void CloseWindow(object sender, ExecutedRoutedEventArgs e)

private void MaximizeWindow(object sender, ExecutedRoutedEventArgs e)
{
SystemCommands.MaximizeWindow(this);
if (WindowState == WindowState.Normal)
{
WindowStyle = WindowStyle.SingleBorderWindow;
WindowState = WindowState.Maximized;
WindowStyle = WindowStyle.None;
}
}

private void MinimizeWindow(object sender, ExecutedRoutedEventArgs e)
{
SendMessage(hWnd, ApiCodes.WM_SYSCOMMAND, new IntPtr(ApiCodes.SC_MINIMIZE), IntPtr.Zero);
SendMessage(hWnd, MonitorHelper.WindowsMessageCodes.WM_SYSCOMMAND, new IntPtr(MonitorHelper.WindowsMessageCodes.SC_MINIMIZE), IntPtr.Zero);
}

private void RestoreWindow(object sender, ExecutedRoutedEventArgs e)
{
SystemCommands.RestoreWindow(this);
}


internal class ApiCodes
{
public const int SC_RESTORE = 0xF120;
public const int SC_MINIMIZE = 0xF020;
public const int WM_SYSCOMMAND = 0x0112;
}

private IntPtr hWnd;

[DllImport(Win32.User32)]
public static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);


private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == ApiCodes.WM_SYSCOMMAND)
switch (msg)
{
if (wParam.ToInt32() == ApiCodes.SC_MINIMIZE)
{
_windowStyle = WindowStyle;
if (WindowStyle != WindowStyle.SingleBorderWindow)
WindowStyle = WindowStyle.SingleBorderWindow;
WindowState = WindowState.Minimized;
handled = true;
}
else if (wParam.ToInt32() == ApiCodes.SC_RESTORE)
{
WindowState = WindowState.Normal;
WindowStyle = WindowStyle.None;
if(WindowStyle.None != _windowStyle)
WindowStyle = _windowStyle;
handled = true;
}
case WindowsMessageCodes.WM_SYSCOMMAND:
if (wParam.ToInt32() == WindowsMessageCodes.SC_MINIMIZE)
{
_windowStyle = WindowStyle;
if (WindowStyle != WindowStyle.SingleBorderWindow)
WindowStyle = WindowStyle.SingleBorderWindow;
WindowState = WindowState.Minimized;
handled = true;
}
else if (wParam.ToInt32() == WindowsMessageCodes.SC_RESTORE)
{
WindowState = WindowState.Normal;
WindowStyle = WindowStyle.None;
if (WindowStyle.None != _windowStyle)
WindowStyle = _windowStyle;
handled = true;
}
break;
}
return IntPtr.Zero;
}
Expand Down
5 changes: 3 additions & 2 deletions src/WPFDevelopers.Net45x/Themes/Generic.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
<Setter Property="UseLayoutRounding" Value="True" />
<Setter Property="TextOptions.TextFormattingMode" Value="Ideal" />
<Setter Property="WindowStyle" Value="None" />
<Setter Property="MaxHeight" Value="{x:Static SystemParameters.MaximizedPrimaryScreenHeight}" />
<Setter Property="MaxWidth" Value="{x:Static SystemParameters.MaximizedPrimaryScreenWidth}" />
<!--<Setter Property="SizeToContent" Value="WidthAndHeight" />-->
<!--<Setter Property="MaxHeight" Value="{x:Static SystemParameters.MaximizedPrimaryScreenHeight}" />
<Setter Property="MaxWidth" Value="{x:Static SystemParameters.MaximizedPrimaryScreenWidth}" />-->
<Setter Property="FontFamily" Value="{DynamicResource WD.NormalFontFamily}" />
<Setter Property="WindowChrome.WindowChrome">
<Setter.Value>
Expand Down
64 changes: 29 additions & 35 deletions src/WPFDevelopers.Net45x/Window.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using Standard;
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using WPFDevelopers.Controls;
using WPFDevelopers.Helpers;
using WPFDevelopers.Core.Helpers;
using static WPFDevelopers.Core.Helpers.MonitorHelper;

namespace WPFDevelopers.Net45x
{
Expand Down Expand Up @@ -140,56 +138,52 @@ private void CloseWindow(object sender, ExecutedRoutedEventArgs e)

private void MaximizeWindow(object sender, ExecutedRoutedEventArgs e)
{
SystemCommands.MaximizeWindow(this);
if (WindowState == WindowState.Normal)
{
WindowStyle = WindowStyle.SingleBorderWindow;
WindowState = WindowState.Maximized;
WindowStyle = WindowStyle.None;
}
}

private void MinimizeWindow(object sender, ExecutedRoutedEventArgs e)
{
SendMessage(hWnd, ApiCodes.WM_SYSCOMMAND, new IntPtr(ApiCodes.SC_MINIMIZE), IntPtr.Zero);
MonitorHelper.SendMessage(hWnd, MonitorHelper.WindowsMessageCodes.WM_SYSCOMMAND, new IntPtr(MonitorHelper.WindowsMessageCodes.SC_MINIMIZE), IntPtr.Zero);
}

private void RestoreWindow(object sender, ExecutedRoutedEventArgs e)
{
SystemCommands.RestoreWindow(this);
}

internal class ApiCodes
{
public const int SC_RESTORE = 0xF120;
public const int SC_MINIMIZE = 0xF020;
public const int WM_SYSCOMMAND = 0x0112;
}

private IntPtr hWnd;

[DllImport(Win32.User32)]
public static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);

private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == ApiCodes.WM_SYSCOMMAND)
switch (msg)
{
if (wParam.ToInt32() == ApiCodes.SC_MINIMIZE)
{
_windowStyle = WindowStyle;
if (WindowStyle != WindowStyle.SingleBorderWindow)
WindowStyle = WindowStyle.SingleBorderWindow;
WindowState = WindowState.Minimized;
handled = true;
}
else if (wParam.ToInt32() == ApiCodes.SC_RESTORE)
{
WindowState = WindowState.Normal;
WindowStyle = WindowStyle.None;
if (WindowStyle.None != _windowStyle)
WindowStyle = _windowStyle;
handled = true;
}
case WindowsMessageCodes.WM_SYSCOMMAND:
if (wParam.ToInt32() == WindowsMessageCodes.SC_MINIMIZE)
{
_windowStyle = WindowStyle;
if (WindowStyle != WindowStyle.SingleBorderWindow)
WindowStyle = WindowStyle.SingleBorderWindow;
WindowState = WindowState.Minimized;
handled = true;
}
else if (wParam.ToInt32() == WindowsMessageCodes.SC_RESTORE)
{
WindowState = WindowState.Normal;
WindowStyle = WindowStyle.None;
if (WindowStyle.None != _windowStyle)
WindowStyle = _windowStyle;
handled = true;
}
break;
}

return IntPtr.Zero;
}

private void ShowSystemMenu(object sender, ExecutedRoutedEventArgs e)
{
var element = e.OriginalSource as FrameworkElement;
Expand Down
31 changes: 31 additions & 0 deletions src/WPFDevelopers.Shared/Core/Helpers/MonitorHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Standard;
using System;
using System.Runtime.InteropServices;
using WPFDevelopers.Helpers;

namespace WPFDevelopers.Core.Helpers
{
public static class MonitorHelper
{
internal class WindowsMessageCodes
{
public const int SC_RESTORE = 0xF120;
public const int SC_MINIMIZE = 0xF020;
public const int WM_SYSCOMMAND = 0x0112;
public const int WM_DEADCHAR = 0x0024;
}

[DllImport(Win32.User32)]
internal static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);

/// <summary>
///
/// </summary>
[DllImport(Win32.User32)]
internal static extern IntPtr MonitorFromWindow(IntPtr handle, int flags);

[DllImport(Win32.User32)]
internal static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);

}
}
1 change: 1 addition & 0 deletions src/WPFDevelopers.Shared/WPFDevelopers.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Core\Helpers\Helper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core\Helpers\ImagingHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core\Converts\WidthHeightToRectConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core\Helpers\MonitorHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core\Helpers\PanelHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core\Helpers\TextBoxHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core\Utilities\FormatExtensions.cs" />
Expand Down

0 comments on commit 9191619

Please sign in to comment.