full screen mode, but don't cover the taskbar - c#

I have a WinForms application, which is set to full screen mode when I login.
My problem is it's covering the Windows taskbar also. I don't want my application to cover the taskbar.
How can this be done?

The way I do it is via this code:
this.MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea;
this.WindowState = FormWindowState.Maximized;

This is probably what you want. It creates a 'maximized' window without hiding the taskbar.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load( object sender, EventArgs e )
{
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
Left = Top = 0;
Width = Screen.PrimaryScreen.WorkingArea.Width;
Height = Screen.PrimaryScreen.WorkingArea.Height;
}
}

I had answer it here:
One thing I left out of the description--I'd turned off the maximize button. When I tested turning that property back on, the task bar showed up again. Apparently it assumes if you don't want a maximize button you are creating a kiosk-style application where you don't want your users to see anything but the application screen. Not exactly what I'd expect, but works I guess.
I had this problem and solved it by Jeff's help. First, set the windowstate to Maximized. but Do not disable the MaximizeBox. Then if you want MaximizeBox to be disabled you should do it programmatically:
private void frmMain_Load(object sender, EventArgs e)
{
this.MaximizeBox = false;
}

Arcanox's answer is great for a single monitor, but if you try it on any screen other than the leftmost, it will just make the form disappear. I used the following code instead.
var workingArea = Screen.FromHandle(Handle).WorkingArea;
MaximizedBounds = new Rectangle(0, 0, workingArea.Width, workingArea.Height);
WindowState = FormWindowState.Maximized;
The only difference is I'm overriding the top & left values to be 0, 0 since they will be different on other screens.

If you have multiple screens, you have to reset location of MaximizedBounds :
Rectangle rect = Screen.FromHandle(this.Handle).WorkingArea;
rect.Location = new Point(0, 0);
this.MaximizedBounds = rect;
this.WindowState = FormWindowState.Maximized;

I'm not good at explaining but this is the code I used to maximize or to full screen the winforms which would not cover up the taskbar. Hope it helps. ^^
private void Form_Load(object sender, EventArgs e)
{
this.Height = Screen.PrimaryScreen.WorkingArea.Height;
this.Width = Screen.PrimaryScreen.WorkingArea.Width;
this.Location = Screen.PrimaryScreen.WorkingArea.Location;
}

If you want to use WindowState = Maximized;, you should first indicate the size limits of the form maximized by the MaximizedBounds property...
Example:
MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea;
WindowState = FormWindowState.Maximized;
Where are you limiting the size of your form to the work area that is the desktop area of ​​the display

If Maximizing isn't what you're looking for, then you'll need to calculate the window size yourself by checking for the location and size of the taskbar:
find-out-size-and-position-of-the-taskbar

Try without FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; and comment line like :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load( object sender, EventArgs e )
{
// FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
Left = Top = 0;
Width = Screen.PrimaryScreen.WorkingArea.Width;
Height = Screen.PrimaryScreen.WorkingArea.Height;
}
}

private void frmGateEntry_Load(object sender, EventArgs e)
{
// set default start position to manual
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
// set position and size to the Form.
this.Bounds = Screen.PrimaryScreen.WorkingArea;
}

This was very useful to me:
private void Form1_Load(object sender, EventArgs e)
{
this.MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea;
this.WindowState = FormWindowState.Maximized;
}

I know it's a bit too late, but it will help others too in the future.
the answered code above is working but still in my case if the taskbar is auto-hiding and showing it wont show the taskbar once it hides from the screen. I solved this problem by adding -1` to the working area height.
var workingArea = Screen.FromHandle(Handle).WorkingArea;
MaximizedBounds = new Rectangle(0, 0, workingArea.Width, workingArea.Height - 1);

Related

Work with multiple monitors with WPF [duplicate]

How can you program a dotNet Windows (or WPF) Application in order to let it going fullscreen on the secondary monitor?
Extension method to Maximize a window to the secondary monitor (if there is one).
Doesn't assume that the secondary monitor is System.Windows.Forms.Screen.AllScreens[2];
using System.Linq;
using System.Windows;
namespace ExtendedControls
{
static public class WindowExt
{
// NB : Best to call this function from the windows Loaded event or after showing the window
// (otherwise window is just positioned to fill the secondary monitor rather than being maximised).
public static void MaximizeToSecondaryMonitor(this Window window)
{
var secondaryScreen = System.Windows.Forms.Screen.AllScreens.Where(s => !s.Primary).FirstOrDefault();
if (secondaryScreen != null)
{
if (!window.IsLoaded)
window.WindowStartupLocation = WindowStartupLocation.Manual;
var workingArea = secondaryScreen.WorkingArea;
window.Left = workingArea.Left;
window.Top = workingArea.Top;
window.Width = workingArea.Width;
window.Height = workingArea.Height;
// If window isn't loaded then maxmizing will result in the window displaying on the primary monitor
if ( window.IsLoaded )
window.WindowState = WindowState.Maximized;
}
}
}
}
For WPF apps look at this post. Ultimately it depends on when the WindowState is set to Maximized. If you set it in XAML or in window constructor (i.e. before the window is loaded) it will always be maximized onto primary display. If, on the other hand, you set WindowState to Maximized when the window is loaded - it will maximise on the screen on which it was maximized before.
I notice an answer which advocates setting the position in the Loaded event, but this causes flicker when the window is first shown normal then maximized. If you subscribe to the SourceInitialized event in your constructor and set the position in there it will handle maximizing onto secondary monitors without flicker - I'm assuming WPF here
public MyWindow()
{
SourceInitialized += MyWindow_SourceInitialized;
}
void MyWindow_SourceInitialized(object sender, EventArgs e)
{
Left = 100;
Top = 50;
Width = 800;
Height = 600;
WindowState = WindowState.Maximized;
}
Substitute coords for any on your secondary monitor
See this codeproject article.
The code in there will work, but default to your primary monitor. To change this, you'll need to replace the calls to GetSystemMetrics will calls to GetMonitorInfo. Using GetMonitorInfo, you can get the appropriate RECT to pass to SetWindowPos.
GetMonitorInfo allows you to get the RECT for any monitor.
There is an MSDN Article on Position Apps in Multi-Monitor Setups that might help explain things a bit better.
private void Form1_Load(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.None;
this.Bounds = GetSecondaryScreen().Bounds;
}
private Screen GetSecondaryScreen()
{
foreach (Screen screen in Screen.AllScreens)
{
if (screen != Screen.PrimaryScreen)
return screen;
}
return Screen.PrimaryScreen;
}
It's not clear from your question if you are looking for a way to move the window to the secondary monitor and then go fullscreen, or if you are just looking to support fullscreen mode on whatever monitor the window is on (which may be primary or secondary).
If the later, for a WPF window, though not quite the same as fullscreen mode, you can remove the borders when it is maximized and restore the border when not maximized. No need to check for which monitor, etc. The display of the caption/title bar is controlled by the border state.
protected override void OnStateChanged(EventArgs e)
{
if (WindowState == WindowState.Maximized)
{
if (WindowStyle.None != WindowStyle)
WindowStyle = WindowStyle.None;
}
else if (WindowStyle != WindowStyle.SingleBorderWindow)
WindowStyle = WindowStyle.SingleBorderWindow;
base.OnStateChanged(e);
}
Credit goes to Pavel for his Forms-based answer in the current question and to Nir for his answer in this question.
#jay-evans answer did the trick for me. However, I also needed to add the WpfscreenHelper Nuget package to get the screen information while not depending on the old System.Windows.Forms namespace as in #grantnz's answer.
Also note that setting the dimensions only (left, top, width + height) left me with a small border on each side. Only after setting WindowState.Maximized is it that the real full-screen effect was achieved.
Since I have monitors with varying DPI configurations, I also had to use WpfWorkingArea instead of WorkingArea, since the coordinates were coming back incorrectly.
Posting here for completeness, this is using WPF with .NET 7.0 and is confirmed to work with varying DPI configurations:
public MainWindow()
{
InitializeComponent();
SourceInitialized += MainWindow_SourceInitialized;
}
private void MainWindow_SourceInitialized(object? sender, EventArgs e)
{
var screen = WpfScreenHelper.Screen.AllScreens.FirstOrDefault(s => !s.Primary);
if (screen != null)
{
this.WindowState = WindowState.Normal;
this.Left = screen.WpfWorkingArea.Left;
this.Top = screen.WpfWorkingArea.Top;
this.Width = screen.WpfWorkingArea.Width;
this.Height = screen.WpfWorkingArea.Height;
this.WindowState = WindowState.Maximized;
}
}
In WPF: Set the WindowState property in Normal (not Maximixed) and create the event Loaded. In the event write this code:
this.Left = SystemParameters.PrimaryScreenWidth + 100;
this.WindowState = System.Windows.WindowState.Maximized;

Winforms taskbar hide/overlay

I m trying to solve problem in my fullscreen application. When I start the app it is fullscreen. That is handled by this code
public MainForm()
{
//this.TopMost = true;
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
}
private void Hlavni_Load (object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
//this.Bounds = Screen.PrimaryScreen.Bounds;
}
When opening another fullscreen Form, everything works fine. Taskbar is cover with and doesn't show up. The problem occurs when I want to open 3rd form. This one is not meant to be fullscreen and serves for purpose similar to MessageBox.
While is this form open, taskbar is also visible untill the form is closed again. I tried somehow to update/change bounds of that form but it only led to making this form fullscreen.
Any idea how to show desired form and keep taskbar hidden?
Thanks in advance
EDIT
Adding the code for 3rd form
public RadForm3()
{
InitializeComponent();
//this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
this.ShowInTaskbar = false;
}
private void RadForm3_Load(object sender, EventArgs e)
{
//this.Bounds = Screen.PrimaryScreen.Bounds;
this.CenterToScreen();
...
}

WindowState Maximized covers traybar when forms borderstyle is none

I am using window form with borderstyle = none.
When I use the following code to maximize my window, it maximizes so that it covers the traybar.
private void pb_max_Click(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Maximized)
{
WindowState = FormWindowState.Normal;
pb_max.Image = GomeeSoft.Properties.Resources.buttonmax;
}
else
{
WindowState = FormWindowState.Maximized;
pb_max.Image = GomeeSoft.Properties.Resources.buttonreturn;
}
}
How do you maximize safely so that the frame is maximized only in the workspace?
Perhaps something like this:
this.MaximumSize = Screen.PrimaryScreen.WorkingArea.Size;
From MSDN:
The working area is the desktop area of the display,
excluding taskbars, docked windows, and docked tool bars.

C# Transparent Form in Panel

im trying to create a semi-transparent form which is displayed in a panel. i can display the form in the panel but the opacity property wont work and the form is non-transparent.
private void button1_Click(object sender, EventArgs e)
{
Form fr = new Form();
fr.FormBorderStyle = FormBorderStyle.None;
fr.BackColor = Color.Black;
fr.TopLevel = false;
fr.Opacity = 0.5;
this.panel1.Controls.Add(fr);
fr.Show();
}
any ideas how i can handle that?
Thanks for your answeres!
Winforms only supports partial transparency for top-level forms. If you want to create an application with partially-transparent UI elements, you either need to use WPF, or handle all the drawing yourself. Sorry to be the bearer of bad news.
Your form is added as a child control of panel1 which is child of the main form which is of default Opacity = 1.
To see Opacity at work, try this:
private void button1_Click(object sender, EventArgs e)
{
Form fr = new Form();
fr.FormBorderStyle = FormBorderStyle.None;
fr.BackColor = Color.Blue;
fr.TopLevel = false;
//fr.Opacity = 0.5;
this.Opacity = 0.5; // add this
this.panel1.Controls.Add(fr);
fr.Show();
}
I guess you want the panel to look semi-transparent, you have to use another method and work with the form itself.

How do I make a WinForms app go Full Screen

I have a WinForms app that I am trying to make full screen (somewhat like what VS does in full screen mode).
Currently I am setting FormBorderStyle to None and WindowState to Maximized which gives me a little more space, but it doesn't cover over the taskbar if it is visible.
What do I need to do to use that space as well?
For bonus points, is there something I can do to make my MenuStrip autohide to give up that space as well?
To the base question, the following will do the trick (hiding the taskbar)
private void Form1_Load(object sender, EventArgs e)
{
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
But, interestingly, if you swap those last two lines the Taskbar remains visible. I think the sequence of these actions will be hard to control with the properties window.
A tested and simple solution
I've been looking for an answer for this question in SO and some other sites, but one gave an answer was very complex to me and some others answers simply doesn't work correctly, so after a lot code testing I solved this puzzle.
Note: I'm using Windows 8 and my taskbar isn't on auto-hide mode.
I discovered that setting the WindowState to Normal before performing any modifications will stop the error with the not covered taskbar.
The code
I created this class that have two methods, the first enters in the "full screen mode" and the second leaves the "full screen mode". So you just need to create an object of this class and pass the Form you want to set full screen as an argument to the EnterFullScreenMode method or to the LeaveFullScreenMode method:
class FullScreen
{
public void EnterFullScreenMode(Form targetForm)
{
targetForm.WindowState = FormWindowState.Normal;
targetForm.FormBorderStyle = FormBorderStyle.None;
targetForm.WindowState = FormWindowState.Maximized;
}
public void LeaveFullScreenMode(Form targetForm)
{
targetForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
targetForm.WindowState = FormWindowState.Normal;
}
}
Usage example
private void fullScreenToolStripMenuItem_Click(object sender, EventArgs e)
{
FullScreen fullScreen = new FullScreen();
if (fullScreenMode == FullScreenMode.No) // FullScreenMode is an enum
{
fullScreen.EnterFullScreenMode(this);
fullScreenMode = FullScreenMode.Yes;
}
else
{
fullScreen.LeaveFullScreenMode(this);
fullScreenMode = FullScreenMode.No;
}
}
I have placed this same answer on another question that I'm not sure if is a duplicate or not of this one. (Link to the other question: How to display a Windows Form in full screen on top of the taskbar?)
And for the menustrip-question, try set
MenuStrip1.Parent = Nothing
when in fullscreen mode, it should then disapear.
And when exiting fullscreenmode, reset the menustrip1.parent to the form again and the menustrip will be normal again.
You can use the following code to fit your system screen and task bar is visible.
private void Form1_Load(object sender, EventArgs e)
{
// hide max,min and close button at top right of Window
this.FormBorderStyle = FormBorderStyle.None;
// fill the screen
this.Bounds = Screen.PrimaryScreen.Bounds;
}
No need to use:
this.TopMost = true;
That line interferes with alt+tab to switch to other application. ("TopMost" means the window stays on top of other windows, unless they are also marked "TopMost".)
I worked on Zingd idea and made it simpler to use.
I also added the standard F11 key to toggle fullscreen mode.
Setup
Everything is now in the FullScreen class, so you don't have to declare a bunch of variables in your Form. You just instanciate a FullScreen object in your form's constructor :
FullScreen fullScreen;
public Form1()
{
InitializeComponent();
fullScreen = new FullScreen(this);
}
Please note this assumes the form is not maximized when you create the FullScreen object.
Usage
You just use one of the classe's functions to toggle the fullscreen mode :
fullScreen.Toggle();
or if you need to handle it explicitly :
fullScreen.Enter();
fullScreen.Leave();
Code
using System.Windows.Forms;
class FullScreen
{
Form TargetForm;
FormWindowState PreviousWindowState;
public FullScreen(Form targetForm)
{
TargetForm = targetForm;
TargetForm.KeyPreview = true;
TargetForm.KeyDown += TargetForm_KeyDown;
}
private void TargetForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.F11)
{
Toggle();
}
}
public void Toggle()
{
if (TargetForm.WindowState == FormWindowState.Maximized)
{
Leave();
}
else
{
Enter();
}
}
public void Enter()
{
if (TargetForm.WindowState != FormWindowState.Maximized)
{
PreviousWindowState = TargetForm.WindowState;
TargetForm.WindowState = FormWindowState.Normal;
TargetForm.FormBorderStyle = FormBorderStyle.None;
TargetForm.WindowState = FormWindowState.Maximized;
}
}
public void Leave()
{
TargetForm.FormBorderStyle = FormBorderStyle.Sizable;
TargetForm.WindowState = PreviousWindowState;
}
}
I recently made a Mediaplayer application and I used API calls to make sure the taskbar was hidden when the program was running fullscreen and then restored the taskbar when the program was not in fullscreen or not had the focus or was exited.
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
Sub HideTrayBar()
Try
Dim tWnd As Integer = 0
Dim bWnd As Integer = 0
tWnd = FindWindow("Shell_TrayWnd", vbNullString)
bWnd = FindWindowEx(tWnd, bWnd, "BUTTON", vbNullString)
ShowWindow(tWnd, 0)
ShowWindow(bWnd, 0)
Catch ex As Exception
'Error hiding the taskbar, do what you want here..'
End Try
End Sub
Sub ShowTraybar()
Try
Dim tWnd As Integer = 0
Dim bWnd As Integer = 0
tWnd = FindWindow("Shell_TrayWnd", vbNullString)
bWnd = FindWindowEx(tWnd, bWnd, "BUTTON", vbNullString)
ShowWindow(bWnd, 1)
ShowWindow(tWnd, 1)
Catch ex As Exception
'Error showing the taskbar, do what you want here..'
End Try
End Sub
You need to set your window to be topmost.
I don't know if it will work on .NET 2.0, but it worked me on .NET 4.5.2. Here is the code:
using System;
using System.Drawing;
using System.Windows.Forms;
public partial class Your_Form_Name : Form
{
public Your_Form_Name()
{
InitializeComponent();
}
// CODE STARTS HERE
private System.Drawing.Size oldsize = new System.Drawing.Size(300, 300);
private System.Drawing.Point oldlocation = new System.Drawing.Point(0, 0);
private System.Windows.Forms.FormWindowState oldstate = System.Windows.Forms.FormWindowState.Normal;
private System.Windows.Forms.FormBorderStyle oldstyle = System.Windows.Forms.FormBorderStyle.Sizable;
private bool fullscreen = false;
/// <summary>
/// Goes to fullscreen or the old state.
/// </summary>
private void UpgradeFullscreen()
{
if (!fullscreen)
{
oldsize = this.Size;
oldstate = this.WindowState;
oldstyle = this.FormBorderStyle;
oldlocation = this.Location;
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
fullscreen = true;
}
else
{
this.Location = oldlocation;
this.WindowState = oldstate;
this.FormBorderStyle = oldstyle;
this.Size = oldsize;
fullscreen = false;
}
}
// CODE ENDS HERE
}
Usage:
UpgradeFullscreen(); // Goes to fullscreen
UpgradeFullscreen(); // Goes back to normal state
// You don't need arguments.
Notice:
You MUST place it inside your Form's class (Example: partial class Form1 : Form { /* Code goes here */ } ) or it will not work because if you don't place it on any form, code this will create an exception.
On the Form Move Event add this:
private void Frm_Move (object sender, EventArgs e)
{
Top = 0; Left = 0;
Size = new System.Drawing.Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
}
If you want to keep the border of the form and have it cover the task bar, use the following:
Set FormBoarderStyle to either FixedSingle or Fixed3D
Set MaximizeBox to False
Set WindowState to Maximized

Categories

Resources