I have the below code which I use which includes hiding the taskbar by my form. It works well if there are two screens connected to my computer but if there is only one screen the taskbar shows, I am not sure why?
FormBorderStyle = FormBorderStyle.None;
await Task.Delay(500);
this.WindowState = FormWindowState.Normal;
this.ActiveControl = textBox1;
StartPosition = FormStartPosition.Manual;
Location = new Point(0, 0);
var height = Screen.AllScreens.Max(x => x.WorkingArea.Height + x.WorkingArea.Y);
var width = Screen.AllScreens.Max(x => x.WorkingArea.Width + x.WorkingArea.X);
Size = new Size(width, height);
this.BringToFront();
this.AcceptButton = button1;
this.ControlBox = false;
this.TopMost = true;
this.Size = Size;
Rectangle ru = Rectangle.Union(Screen.AllScreens[0].Bounds , Screen.AllScreens[1].Bounds);
Bounds = ru;
You're getting an "System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'" error because of AllScreens[1] when you only have one screen connected there is only one value in the AllScreen array so index[1] is out of bound.
Change;
Rectangle ru = Rectangle.Union(Screen.AllScreens[0].Bounds , Screen.AllScreens[1].Bounds);
To;
var ru = Screen.AllScreens.Select(a => a.Bounds).Aggregate(Rectangle.Union);
Now because you're using a Select you wil only getting AllScreens[0] instead of guessing what indexes well be there this code now also works is you have three monitors or any other configuration of monitors.
Edit
Also, as a small side note if you're trying to block a user from doing anything outside of your program. This will not block them fully actions like pressing the windows key will still bring up the taskbar or give control back to the user.
Related
I run a triple-monitor setup and I am working on a graphics demo in MonoGame that I decided (heck why not? let's give it the ability to maximize across all displays!) so I used this code:
graphics.IsFullScreen = false;
graphics.ApplyChanges();
//get dimensions of box that will cover all displays and set window to it.
int xPos = System.Windows.Forms.Screen.AllScreens.OrderBy(x => x.Bounds.X).Select(x => x.Bounds.X).First();
int yPos = System.Windows.Forms.Screen.AllScreens.OrderBy(y => y.Bounds.Y).Select(y => y.Bounds.Y).First();
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
form.Location = new System.Drawing.Point(xPos, yPos);
int xWidth = System.Windows.Forms.Screen.AllScreens.OrderByDescending(x => x.Bounds.X).Select(x => x.Bounds.X + x.Bounds.Width).First() - xPos;
int yHeight = System.Windows.Forms.Screen.AllScreens.OrderByDescending(y => y.Bounds.Y).Select(y => y.Bounds.Y + y.Bounds.Height).First() - yPos;
form.MaximumSize = new System.Drawing.Size(0, 0);
form.Width = xWidth;
form.Height = yHeight;
// graphics.PreferredBackBufferWidth = xWidth;
// graphics.PreferredBackBufferHeight = yHeight;
graphics.ApplyChanges();
Properties.Settings.Default.FakeFullScreen = true;
}
and of course a 2nd function to undo it.
This worked fine when I had one of my monitors set above the others for testing, but when I set windows layout to place them all side-by-side (giving a resolution of 5760x1080) I was throwing an invalid parameter error on the graphics.ApplyChanges(). So I commented out the graphics code and set the form width manually and discovered that evidently I am not allowed to have a form wider than 4096 pixels.
Is there a way around this? I am open to all suggestions, including having more than one window side-by-side to draw to, but I would need some code to show me how to target a 2nd form.
Please and thank you.
This is a DirectX 9/Windows Phone 7 limitation of texture sizes limited to 4096 x 4096 by the use of the "Reach Graphics Profile".
The final displayed image is a single texture of the composite of all spritebatches, the size cannot exceed the maximum texture size.
To correct the issue:
Make sure your video card supports larger textures: run dxdiag.exe (most modern video cards do, given enough memory).
Enable the "hidef" profile to allow full DX9, DX10, and DX11 modes.
To enable "Hidef", modify your Game1.cs constructor use this code:
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.GraphicsProfile = GraphicsProfile.HiDef;
graphics.ApplyChanges();
// any additional code goes here
}
The alternative is to use OpenGL, which ignores the graphics profile and uses the optimal version for your video card.
I'm trying to create an application that looks similar to the Windows Vista sidebar. There's an API that allows docking toolbars on the screen (AppBar), but it's not exactly what I'm looking for.
How can I attach a Form to the desktop and dock it to the side of the screen, but without preventing other windows from overlapping it?
With all the following options you get a Sidebar look-a-like (the code below is for a WPF Window):
//width of the sidebar
Width = 300;
//height (remember to add a reference to the System.Windows.Forms dll)
Height = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
//no window style means no border
WindowStyle = WindowStyle.None;
//not resizable
ResizeMode = ResizeMode.NoResize;
//allow a transparent sidebar
AllowsTransparency = true;
//change the color
Background = new SolidColorBrush(Colors.CadetBlue);
//set the opacity (how much transparent)
Opacity = 0.5d;
//offset from the top
Top = 0;
//offset from the left (calculated so it shows on the right side)
Left = SystemParameters.PrimaryScreenWidth - (double)GetValue(WidthProperty);
//set it the topmost window
Topmost = true;
//hide the icon from the taskbar
ShowInTaskbar = false;
Hope this helps!
Update:
Here's a similar solution for when you're using WindowsForms, altough with WPF you have much more possibilities! The differences are minor, everything explains itself. The last line I added hides the window taskbar-icon. Do not place the code in the constructor of the Form but in the Load-event, otherwise the Location will be wrong. In WPF this doesn't matter.
Width = 300;
Height = Screen.PrimaryScreen.Bounds.Height;
FormBorderStyle = FormBorderStyle.None;
BackColor = Color.CadetBlue;
Opacity = 0.5d;
Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - Width, 0);
TopMost = true;
ShowInTaskbar = false;
I have a small tool window that normally has the FormBorderStyle to FixedDialog with no caption text and no control box so it looks like a border-less form with a raised 3d effect.
When the user moves the mouse over the tool window it changes from this border-less FixedDialog mode to a SizableToolWindow w/ caption text and a control box.
The result is the client area moving.
The following code works but i do not want to hard code the top/left delta and I assume it is different depending on what theme/os the user has
void Reposition()
{
var topDelta = 12; // this number is wrong, i have not found the right number for aero yet
var leftDelta = 3;
if (this.Bounds.Contains(MousePosition))
{
if (this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.SizableToolWindow)
{
this.Location = new Point(this.Location.X - leftDelta, this.Location.Y - topDelta);
this.ControlBox = true;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
}
}
else
{
if (this.FormBorderStyle == System.Windows.Forms.FormBorderStyle.SizableToolWindow)
{
this.Location = new Point(this.Location.X + leftDelta, this.Location.Y + topDelta);
this.ControlBox = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
}
}
}
Look into SystemParameters class. You will find the values you are hard-coding in your code there.
I work on VS 2008 with C#. This below code does not work for me. My form was designed in 1024 x 768 resolution.
Our clients laptop is in 1366 x 768 resolution. To solve this problem, I set below code in Form Load event:
this.Location = new Point(0, 0);
this.Size = Screen.PrimaryScreen.WorkingArea.Size;
but the form does not resize as per screen resolution and bottom of my form gets hidden or cut or I miss the scroll bar.
Is there any way to solve this problem? Please show me the syntax.
Can't you start maximized?
Set the System.Windows.Forms.Form.WindowState property to FormWindowState.Maximized
If you want to set the form size programmatically, set the form's StartPosition property to Manual. Otherwise the form's own positioning and sizing algorithm will interfere with yours. This is why you are experiencing the problems mentioned in your question.
Example: Here is how I resize the form to a size half-way between its original size and the size of the screen's working area. I also center the form in the working area.
On computers with multiple monitors, the user probably expects the form to open on the same screen that the mouse pointer is on. We can get it with Screen.FromPoint(Cursor.Position).
public MainView()
{
InitializeComponent();
StartPosition = FormStartPosition.Manual;
Rectangle screen = Screen.FromPoint(Cursor.Position).WorkingArea;
int w = Width >= screen.Width ? screen.Width : (screen.Width + Width) / 2;
int h = Height >= screen.Height ? screen.Height : (screen.Height + Height) / 2;
Location = new Point(screen.Left + (screen.Width - w) / 2, screen.Top + (screen.Height - h) / 2);
Size = new Size(w, h);
}
Note that setting WindowState to FormWindowState.Maximized alone does not change the size of the restored window. So the window might look good as long as it is maximized, but when restored, the window size and location can still be wrong. So I suggest setting size and location even when you intend to open the window as maximized.
Probably a maximized Form helps, or you can do this manually upon form load:
Code Block
this.Location = new Point(0, 0);
this.Size = Screen.PrimaryScreen.WorkingArea.Size;
And then, play with anchoring, so the child controls inside your form automatically fit in your form's new size.
Set the form property to open in maximized state.
this.WindowState = FormWindowState.Maximized;
int h = Screen.PrimaryScreen.WorkingArea.Height;
int w = Screen.PrimaryScreen.WorkingArea.Width;
this.ClientSize = new Size(w , h);
You can simply set the window state
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
simply set Autoscroll = true for ur windows form.. (its not good solution but helpful)..
try for panel also(Autoscroll property = true)
You can always tell the window to start in maximized... it should give you the same result... Like this: this.WindowState = FormWindowState.Maximized;
P.S. You could also try (and I'm not recommending this) to subtract the taskbar height.
This question already has answers here:
How do I make a WinForms app go Full Screen
(10 answers)
Closed 8 years ago.
I have a .net windows application that needs to run in full screen. When the application starts however the taskbar is shown on top of the main form and it only disappears when activating the form by clicking on it or using ALT-TAB. The form's current properties are as follow:
WindowState=FormWindowState.Normal
TopMost=Normal
Size=1024,768 (this is the screen resolution of the machines it's going to be running on)
FormBorderStyle = None
I've tried adding the followings on form load but none worked for me:
this.Focus(); (after giving the focus this.Focus property is always false)
this.BringToFront();
this.TopMost = true; (this however would not be ideal in my scenario)
this.Bounds = Screen.PrimaryScreen.Bounds;
this.Bounds = Screen.PrimaryScreen.Bounds;
Is there a way to do it within .NET or would I have to invoke native windows methods and if so a code snippet would very much be appreciated.
many thanks
Use:
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
And then your form is placed over the taskbar.
I've tried so many solutions, some of them works on Windows XP and all of them did NOT work on Windows 7. After all I write a simple method to do so.
private void GoFullscreen(bool fullscreen)
{
if (fullscreen)
{
this.WindowState = FormWindowState.Normal;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Bounds = Screen.PrimaryScreen.Bounds;
}
else
{
this.WindowState = FormWindowState.Maximized;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
}
}
the order of code is important and will not work if you change the place of WindwosState and FormBorderStyle.
One of the advantages of this method is leaving the TOPMOST on false that allow other forms to come over the main form.
It absolutely solved my problem.
This is how I make forms full screen.
private void button1_Click(object sender, EventArgs e)
{
int minx, miny, maxx, maxy;
inx = miny = int.MaxValue;
maxx = maxy = int.MinValue;
foreach (Screen screen in Screen.AllScreens)
{
var bounds = screen.Bounds;
minx = Math.Min(minx, bounds.X);
miny = Math.Min(miny, bounds.Y);
maxx = Math.Max(maxx, bounds.Right);
maxy = Math.Max(maxy, bounds.Bottom);
}
Form3 fs = new Form3();
fs.Activate();
Rectangle tempRect = new Rectangle(1, 0, maxx, maxy);
this.DesktopBounds = tempRect;
}
My simple fix it turned out to be calling the form's Activate() method, so there's no need to use TopMost (which is what I was aiming at).
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 do I make a WinForms app go Full Screen)
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
I believe that it can be done by simply setting your FormBorderStyle Property to None and the WindowState to Maximized. If you are using Visual Studio both of those can be found in the IDE so there is no need to do so programmatically. Make sure to include some way of closing/exiting the program before doing this cause this will remove that oh so helpful X in the upper right corner.
EDIT:
Try this instead. It is a snippet that I have kept for a long time. I can't even remember who to credit for it, but it works.
/*
* A function to put a System.Windows.Forms.Form in fullscreen mode
* Author: Danny Battison
* Contact: gabehabe#googlemail.com
*/
// a struct containing important information about the state to restore to
struct clientRect
{
public Point location;
public int width;
public int height;
};
// this should be in the scope your class
clientRect restore;
bool fullscreen = false;
/// <summary>
/// Makes the form either fullscreen, or restores it to it's original size/location
/// </summary>
void Fullscreen()
{
if (fullscreen == false)
{
this.restore.location = this.Location;
this.restore.width = this.Width;
this.restore.height = this.Height;
this.TopMost = true;
this.Location = new Point(0,0);
this.FormBorderStyle = FormBorderStyle.None;
this.Width = Screen.PrimaryScreen.Bounds.Width;
this.Height = Screen.PrimaryScreen.Bounds.Height;
}
else
{
this.TopMost = false;
this.Location = this.restore.location;
this.Width = this.restore.width;
this.Height = this.restore.height;
// these are the two variables you may wish to change, depending
// on the design of your form (WindowState and FormBorderStyle)
this.WindowState = FormWindowState.Normal;
this.FormBorderStyle = FormBorderStyle.Sizable;
}
}
I'm not have an explain on how it works, but works, and being cowboy coder is that all I need.
System.Drawing.Rectangle rect = Screen.GetWorkingArea(this);
this.MaximizedBounds = Screen.GetWorkingArea(this);
this.WindowState = FormWindowState.Maximized;
FormBorderStyle = FormBorderStyle.Sizable;
TopMost = false;
WindowState = FormWindowState.Normal;
THIS CODE MAKE YOUR WINDOWS FULL SCREEN THIS WILL ALSO COVER WHOLE SCREEN