Skip to content

Commit

Permalink
fix maximum size with task bar size
Browse files Browse the repository at this point in the history
  • Loading branch information
Platonenkov committed Nov 17, 2021
1 parent a53fc22 commit 7a569b5
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 5 deletions.
204 changes: 204 additions & 0 deletions StyledWindow.WPF/Extensions/Taskbar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/******************************************************************************
* Name: Taskbar.cs
* Description: Class to get the taskbar's position, size and other properties.
* Author: Franz Alex Gaisie-Essilfie
* based on code from https://winsharp93.wordpress.com/2009/06/29/find-out-size-and-position-of-the-taskbar/
*
* Change Log:
* Date | Description
* -------------|--------------------------------------------------------------
* 2017-05-16 | Initial design
*/

using System;
using System.Drawing;
using System.Runtime.InteropServices;

namespace System.Windows.Forms
{
internal enum TaskbarPosition
{
Unknown = -1,
Left,
Top,
Right,
Bottom,
}

internal static class Taskbar
{
private enum ABS
{
AutoHide = 0x01,
AlwaysOnTop = 0x02,
}

////private enum ABE : uint
private enum AppBarEdge : uint
{
Left = 0,
Top = 1,
Right = 2,
Bottom = 3
}

////private enum ABM : uint
private enum AppBarMessage : uint
{
New = 0x00000000,
Remove = 0x00000001,
QueryPos = 0x00000002,
SetPos = 0x00000003,
GetState = 0x00000004,
GetTaskbarPos = 0x00000005,
Activate = 0x00000006,
GetAutoHideBar = 0x00000007,
SetAutoHideBar = 0x00000008,
WindowPosChanged = 0x00000009,
SetState = 0x0000000A,
}

private const string ClassName = "Shell_TrayWnd";
private static APPBARDATA _appBarData;

/// <summary>Static initializer of the <see cref="Taskbar" /> class.</summary>
static Taskbar()
{
_appBarData = new APPBARDATA
{
cbSize = (uint)Marshal.SizeOf(typeof(APPBARDATA)),
hWnd = FindWindow(Taskbar.ClassName, null)
};
}

/// <summary>
/// Gets a value indicating whether the taskbar is always on top of other windows.
/// </summary>
/// <value><c>true</c> if the taskbar is always on top of other windows; otherwise, <c>false</c>.</value>
/// <remarks>This property always returns <c>false</c> on Windows 7 and newer.</remarks>
public static bool AlwaysOnTop
{
get
{
int state = SHAppBarMessage(AppBarMessage.GetState, ref _appBarData).ToInt32();
return ((ABS)state).HasFlag(ABS.AlwaysOnTop);
}
}

/// <summary>
/// Gets a value indicating whether the taskbar is automatically hidden when inactive.
/// </summary>
/// <value><c>true</c> if the taskbar is set to auto-hide is enabled; otherwise, <c>false</c>.</value>
public static bool AutoHide
{
get
{
int state = SHAppBarMessage(AppBarMessage.GetState, ref _appBarData).ToInt32();
return ((ABS)state).HasFlag(ABS.AutoHide);
}
}

/// <summary>Gets the current display bounds of the taskbar.</summary>
public static Rectangle CurrentBounds
{
get
{
var rect = new RECT();
if (GetWindowRect(Handle, ref rect))
return Rectangle.FromLTRB(rect.Left, rect.Top, rect.Right, rect.Bottom);

return Rectangle.Empty;
}
}

/// <summary>Gets the display bounds when the taskbar is fully visible.</summary>
public static Rectangle DisplayBounds
{
get
{
if (RefreshBoundsAndPosition())
return Rectangle.FromLTRB(_appBarData.rect.Left,
_appBarData.rect.Top,
_appBarData.rect.Right,
_appBarData.rect.Bottom);

return CurrentBounds;
}
}

/// <summary>Gets the taskbar's window handle.</summary>
public static IntPtr Handle
{
get { return _appBarData.hWnd; }
}

/// <summary>Gets the taskbar's position on the screen.</summary>
public static TaskbarPosition Position
{
get
{
if (RefreshBoundsAndPosition())
return (TaskbarPosition)_appBarData.uEdge;

return TaskbarPosition.Unknown;
}
}

/// <summary>Hides the taskbar.</summary>
public static void Hide()
{
const int SW_HIDE = 0;
ShowWindow(Handle, SW_HIDE);
}

/// <summary>Shows the taskbar.</summary>
public static void Show()
{
const int SW_SHOW = 1;
ShowWindow(Handle, SW_SHOW);
}

private static bool RefreshBoundsAndPosition()
{
//! SHAppBarMessage returns IntPtr.Zero **if it fails**
return SHAppBarMessage(AppBarMessage.GetTaskbarPos, ref _appBarData) != IntPtr.Zero;
}

#region DllImports

[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);

[DllImport("shell32.dll", SetLastError = true)]
private static extern IntPtr SHAppBarMessage(AppBarMessage dwMessage, [In] ref APPBARDATA pData);

[DllImport("user32.dll")]
private static extern int ShowWindow(IntPtr hwnd, int command);

#endregion DllImports

[StructLayout(LayoutKind.Sequential)]
private struct APPBARDATA
{
public uint cbSize;
public IntPtr hWnd;
public uint uCallbackMessage;
public AppBarEdge uEdge;
public RECT rect;
public int lParam;
}

[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
}
}
4 changes: 0 additions & 4 deletions StyledWindow.WPF/StyledWindow.WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,4 @@
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.37" />
<PackageReference Include="System.Text.Json" Version="5.0.2" />
</ItemGroup>

<ItemGroup>
<Folder Include="Extensions\" />
</ItemGroup>
</Project>
10 changes: 10 additions & 0 deletions StyledWindow.WPF/Themes/StyledWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Forms;
using MaterialDesignThemes.Wpf;
using StyledWindow.WPF.Components;

Expand Down Expand Up @@ -114,6 +115,15 @@ private void _themeBtn_OnLoaded(object Sender, RoutedEventArgs E)

#endregion

public StyledWindow()
{
var Position = Taskbar.Position;
var AlwaysOnTop = Taskbar.AlwaysOnTop ;
var AutoHide = Taskbar.AutoHide;
var CurrentBounds = Taskbar.CurrentBounds;
var DisplayBounds = Taskbar.DisplayBounds;
var Handle = Taskbar.Handle;
}



Expand Down
4 changes: 3 additions & 1 deletion StyledWindow.WPF/Themes/StyledWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@

<Grid x:Name="PART_MainGrid"
Background="{DynamicResource MaterialDesignPaper}">
<b:Interaction.Behaviors>
<behaviors:WindowMaximizationLimitattor/>
</b:Interaction.Behaviors>
<DockPanel>
<!-- Заголовок окна -->
<Grid Name="PART_TitleBar" DockPanel.Dock="Top">
Expand Down Expand Up @@ -458,7 +461,6 @@
</Trigger>
<Trigger Property="WindowState" Value="Maximized">
<Setter TargetName="PART_MaximizeButtonPath" Property="Kind" Value="WindowRestore"/>
<Setter TargetName="PART_MainGrid" Property="Margin" Value="7"/>
<Setter TargetName="PART_Border" Property="Margin" Value="7"/>
<Setter TargetName="PART_Border" Property="BorderThickness" Value="7"/>
<Setter TargetName="PART_ResizeElements" Property="Visibility" Value="Collapsed"/>
Expand Down

0 comments on commit 7a569b5

Please sign in to comment.