I would like to make my application such that it can maximize to full screen means it hide the windows task bar and the title bar as well. And it should triggered by a butto
You need to set the WindowStyle to none as well as WindowState to Maximized
<Window ...
WindowStyle="None"
WindowState="Maximized">
I had this issue with the taskbar staying on top of my window. The current solution i use is setting the window as Topmost for a short time, then setting it back to false (i want my window to work nice with Alt+Tab)
private Timer t;
public void OnLoad()
{
var window = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
StartTopmostTimer(window);
}
private void StartTopmostTimer(Window window)
{
t = new Timer(o => SetTopMostFalse(window), null, 1000, Timeout.Infinite);
}
private void SetTopMostFalse(Window window)
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
window.Topmost = false;
}));
t.Dispose();
}
I also use this code to switch between full screen and window mode:
public void SwitchFullScreen()
{
var window = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
if (window != null)
{
if (window.WindowStyle == WindowStyle.None)
{
window.WindowStyle = WindowStyle.SingleBorderWindow;
window.WindowState = state;
}
else
{
state = window.WindowState;
window.WindowStyle = WindowStyle.None;
window.WindowState = WindowState.Maximized;
window.Topmost = true;
StartTopmostTimer(window);
}
}
}
None of the above worked for me. Even with the Windows API the taskbar does hide but there is still the space after the window is maximized. What worked is to not set the Maximized property, get the size of the desktop and set these:
Top = 0;
Left = 0;
Width = width_of_your_desktop;
Height = height_of_your_desktop;
Don't even need to set the Topmost! To get the screen size you can either use the value from SystemParameters.PrimaryScreenHeight and PrimaryScreenWidth, or if you want to get the screen where the window is currently on, use GetMonitorFromWindow from below:
[StructLayout(LayoutKind.Sequential)]
private struct MonitorInfo
{
public uint cbSize;
public Rect2 rcMonitor;
public Rect2 rcWork;
public uint dwFlags;
}
[StructLayout(LayoutKind.Sequential)]
private struct Rect2
{
public int left;
public int top;
public int right;
public int bottom;
}
private const int MONITOR_DEFAULTTONULL = 0;
private const int MONITOR_DEFAULTTOPRIMARY = 1;
private const int MONITOR_DEFAULTTONEAREST = 2;
[DllImport("user32.dll")]
private static extern IntPtr MonitorFromWindow(IntPtr hwnd, int flags);
[DllImport("user32.dll")]
private static extern bool GetMonitorInfo(IntPtr hwnd, ref MonitorInfo mInfo);
public static Rect GetMonitorFromWindow(Window win) {
var mi = new MonitorInfo();
mi.cbSize = (uint)Marshal.SizeOf(mi);
var hwmon = MonitorFromWindow(new System.Windows.Interop.WindowInteropHelper(win).EnsureHandle(), MONITOR_DEFAULTTONULL);
if (hwmon != null && GetMonitorInfo(hwmon, ref mi)) {
//convert to device-independent vaues
var mon = mi.rcMonitor;
Point realp1;
Point realp2;
var trans = PresentationSource.FromVisual(win).CompositionTarget.TransformFromDevice;
realp1 = trans.Transform(new Point(mon.left, mon.top));
realp2 = trans.Transform(new Point(mon.right, mon.bottom));
return new Rect(realp1, realp2);
}
else
throw new Exception("Failed to get monitor info.");
}
If the taskbar doesn't disappear, it may help to change the Window visibility before and after changing window style, like this:
private void MainWindow_StateChanged(object sender, EventArgs e) {
if (this.WindowState == WindowState.Maximized) {
// hide the window before changing window style
this.Visibility = Visibility.Collapsed;
this.Topmost = true;
this.WindowStyle = WindowStyle.None;
this.ResizeMode = ResizeMode.NoResize;
// re-show the window after changing style
this.Visibility = Visibility.Visible;
}
else {
this.Topmost = false;
this.WindowStyle = WindowStyle.SingleBorderWindow;
this.ResizeMode = ResizeMode.CanResize;
}
}
Step 1: Write class with static methods Hide() and Show() for taskbar
public class Taskbar
{
[DllImport("user32.dll")]
private static extern int FindWindow(string className, string windowText);
[DllImport("user32.dll")]
private static extern int ShowWindow(int hwnd, int command);
private const int SW_HIDE = 0;
private const int SW_SHOW = 1;
protected static int Handle
{
get
{
return FindWindow("Shell_TrayWnd", "");
}
}
private Taskbar()
{
// hide ctor
}
public static void Show()
{
ShowWindow(Handle, SW_SHOW);
}
public static void Hide()
{
ShowWindow(Handle, SW_HIDE);
}
}
Step 2: Connect to window Closing event to get taskbar back when window will close with Alt+F4
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Taskbar.Show();
}
Hide taskbar and show fullscreen:
Taskbar.Hide();
WindowStyle = WindowStyle.None;
WindowState = WindowState.Maximized;
ResizeMode = ResizeMode.NoResize;
Width = System.Windows.SystemParameters.PrimaryScreenWidth;
Height = System.Windows.SystemParameters.PrimaryScreenHeight;
Topmost = true;
Left = 0;
Top = 0;
Show taskbar and run in window
Taskbar.Show();
WindowStyle = WindowStyle.SingleBorderWindow;
WindowState = WindowState.Normal;
ResizeMode = ResizeMode.CanResize;
Width = System.Windows.SystemParameters.WorkArea.Width-100;
Height = System.Windows.SystemParameters.WorkArea.Height-100;
Topmost = false;
Left = 0;
Top = 0;
Tested on Windows 10 1703 (Creators Update)
You just need to set the WindowStyle to none:
<Window ...
WindowStyle="None">