I have a C#.2017 project, Home form is none border (set in properties). It always start with maximize and startposition is windowsdefaultlocation/manual (I tried). I try many code but it still runs and hide taskbar.
I want to run the form in none border, maximize/full screen mode, the form doesn't hide the taskbar of windows 10.
Tried this links:
https://social.msdn.microsoft.com/Forums/windows/en-US/e81dc341-720e-474a-9c37-75eac3a130cb/howto-show-window-form-on-top-of-taskbar-in-every-resolution?forum=winforms
https://www.c-sharpcorner.com/UploadFile/shubham0987/display-app-in-full-screen-with-windows-taskbar/
How to display a Windows Form in full screen on top of the taskbar?
private void Form1_Load(object sender, EventArgs e)
{
this.Height = Screen.PrimaryScreen.WorkingArea.Height;
this.Width = Screen.PrimaryScreen.WorkingArea.Width;
this.Location = Screen.PrimaryScreen.WorkingArea.Location;
//Screen currentScreen = Screen.FromHandle(this.Handle);
//this.Size = new System.Drawing.Size(currentScreen.Bounds.Width, currentScreen.Bounds.Height);
}
It doesn't help me anything. If you have some solution better help me please.
Much thank to all.
Setting a Forms WindowState to Maximized when your BorderStyle is none will always lay over the taskbar. This is a typical behaviour of a "Fullscreen" application.
However you are on the right track. If you want to have a semi-fullscreen experience without laying over the taskbar you have to set the Location and Size of your Form manually.
Important here is that you not only set these values manually but also take away control from the OS itself as it will always try to position a Form by some ruleset.
private void Form1_Load(object sender, EventArgs e)
{
//Hiding the Border to simulate a fullscreen-experience
this.FormBorderStyle = FormBorderStyle.None;
//Telling the operating system that we want to set the start position manually
this.StartPosition = FormStartPosition.Manual;
//Actually setting our Width, Height, and Location
this.Height = Screen.PrimaryScreen.WorkingArea.Height;
this.Width = Screen.PrimaryScreen.WorkingArea.Width;
this.Location = Screen.PrimaryScreen.WorkingArea.Location;
}
Just a little side node: You might want to think about people with multiple screens and on which screen your application should appear (maybe let the user decide by some setting etc).
Maybe this one is quite similar to something I need:
this.MaximumSize = Screen.PrimaryScreen.WorkingArea.Size ;
Related
I would like to create an application that have a small window displayed at the bottom corner of desktop. On startup, the window shall be very small and ideally, just a couple of pixels in width.
Here is the code I used to do it:
public partial class DurationT64 : Form
{
private Size fullSize;
private Point fullPos;
private Point compactPos;
public DurationT64()
{
InitializeComponent();
var workingArea = Screen.PrimaryScreen.WorkingArea;
this.MinimumSize = new Size(0, this.Height);
this.MaximumSize = new Size(this.Width, this.Height);
// fullPos: the window location when it is in full size form.
fullPos = new Point(workingArea.Right - this.Width, workingArea.Bottom - this.Height);
this.Location = fullPos;
// fullSize: the size of the windown when it is in full size form.
fullSize = new Size(this.Width, this.Height);
this.Size = fullSize;
// compactPos: the window location when it is in compact size form.
compactPos = new Point(workingArea.Right - 30, fullPos.Y);
this.Width = 1;
this.Location = compactPos;
}
}
As you can see that in this example, I intended to create a window of just 1 pixel in width, placed closed to the right edge of the primary monitor.
However, I realized that the window doesn't go as small as I was expected. It goes down to 20 pixels wide but no less than that. Please refer to this screen capture image below for example:
an image shows that the window is wider than it suppose to be
I did some research regards to this problem and noticed that there was a solution proposed by Zach Johnson (#zach-johnson) back in 2009. Here is the link to it Overcome OS Imposed Windows Form Minimum Size Limit.
However, nether methods proposed in that link (the intercepting WM_ message one proposed by Zach and the SetBoundsCore one proposed by #Ace) works for me.
Can anyone please give me some solution to this question? Preferably, a solution purely based on C#/Winform and does not rely on native Win32 window message loop, if possible.
Many thanks!
It is rather straight-forward, Winforms ensures that the window cannot be made smaller than the system-imposed minimum size of a window, exposed as the SystemInformation.MinWindowTrackSize property in .NET. This is a "safety" setting, it ensures that the user cannot make the window too small when he resizes it, thus losing control over it. Same consideration applies to code.
Bypassing this limit requires no magic, you need to do two things:
Set the FormBorderStyle property to None so the user cannot resize the window.
Set the size after the window is created. The Load event is best.
Some comments about your existing code: be careful about tinkering with the Width/Height/Size properties, you are doing too much of it in your constructor and it cannot work correctly. In the constructor they don't yet match the actual size of the window. And will not be close at all on modern machines with high-resolution monitors, auto-scaling to match the DPI of the video adapter is important today. You have to postpone until the window is created and scaling is complete, the Load event is the proper place for code like this. One of the few reasons to actually use Load.
And note that your Location property calculation is inadequate, it does not consider the location of the taskbar. It doesn't work on my machine, I like the taskbar on the right.
Minimum repro:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
}
protected override void OnLoad(EventArgs e) {
this.Width = 1;
base.OnLoad(e);
}
}
Do keep in mind that you'll need hawk-eyes to find it back on the screen :)
Here is my code
private void button1_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Bounds = Screen.PrimaryScreen.Bounds;
pictureBox1.Dock = DockStyle.Fill;
}
What code I can applied on picturebox2 so that picturebox2 also displaced according to form ...
You can use the Dock and Anchor properties of the controls to determine their behavior when the form is resized. When using docks and anchors in WinForms, you typically decide for a primary control (group) that gets the main part of the screen and another group of controls that are aligned in the remaining area. So if you set DockStyle.Fill for PictureBox1 control, you set the other PictureBox to DockStyle.Right. When the form is resized, the main area is extended. Please note however, that it sometimes depends on the order that the controls were created on how they are aligned and whether it works as expected. It might take some experimenting with putting various controls to foreground in order to reach your goal.
This link lists a lot of tutorials on how to align controls on Windows Forms, specifically on setting anchors and docking controls.
In addition, you can use various layout controls, among them a TableLayoutPanel (Thanks #HansPassant for the hint). For a walk-through see this link.
You need to set anchors to all your controls on your form( by default all your controls are "tied" to the top and left of your form) . If anchors are not enough try using dock panels and dock your control.
You can se the anchors from the visual editor. Select a control and on the properties panel you should set the anchors.
Here you have to scale the child controls as the main window is scaled.
try the Scale method by calculating of the scale factor as below:
the code:
Size st = this.Size;
int height = st.Height;
int width = st.Width;
this.WindowState = FormWindowState.Normal;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Bounds = Screen.PrimaryScreen.Bounds;
Size newSize = this.Size;
SizeF scaleFactor = new SizeF();
scaleFactor.Height = newSize.Height / height;
scaleFactor.Width = newSize.Width / width;
this.Scale(scaleFactor);
Something is wrong with how settings are being saved/read from in Windows Forms apps.
It's not a problem with my code. But I don't know what's wrong.
Form1_Load(object sender, EventArgs e)
{
this.Size = new Size(Properties.Settings.Default.Size.Width, Properties.Settings.Default.Size.Height);
this.Location = new Point(Properties.Settings.Default.Location.X, Properties.Settings.Default.Location.Y);
}
Form1_Closing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.Location = this.Location;
Properties.Settings.Default.Size = this.Size;
Properties.Settings.Default.Save();
}
Now. You can see that the code is right.
Why then, does the Form get bigger and Bigger and BIgger and BIGger and BIGGer and BIGGEr and BIGGER every single time I reopen my app?
It's supposed to stay the same size! You know, since I saved it and everything... Why?
The Size property includes the non-client area of the form, which can fluctuate wildly depending on whether you have enabled themes, Aero, etc.
Try using the ClientSize property instead.
I have a windows form. I'm setting it's size using the following code. My desired behaviour is to have it take the full width and half the height of the primary display.
this.Location = new Point(0, 0);
this.WindowState = FormWindowState.Normal;
this.Size = new System.Drawing.Size(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height / 2);
The problem is there is a space on the right form (it's not taking up the full screen). Does anyone have any suggestions as to why?
Thanks
Try use
Screen.PrimaryScreen.WorkingArea
Two ways to make it fullscreen:
// maximize the form
this.WindowState = FormWindowState.Maximized;
or
// hide the border
this.FormBorderStyle = FormBorderStyle.None;
You can try them together but have to notice the order, hide the border first, then set the window state to maximized.
I'm using Windows Vista and C#.net 3.5, but I had my friend run the program on XP and has the same problem.
So I have a C# program that I have running in the background with an icon in the SystemTray. I have a low level keyboard hook so when I press two keys (Ctr+windows in this case) it'll pull of the application's main form. The form is set to be full screen in the combo key press even handler:
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
So it basically works. When I hit CTR+Windows it brings up the form, no matter what program I have given focus to. But sometimes, the taskbar will still show up over the form, which I don't want. I want it to always be full screen when I hit that key combo.
I figure it has something to do with what application has focus originally. But even when I click on my main form, the taskbar sometimes stays there. So I wonder if focus really is the problem. It just seems like sometimes the taskbar is being stubborn and doesn't want to sit behind my program.
Anyone have any ideas how I can fix this?
EDIT: More details-
I'm trying to achieve the same effect that a web browser has when you put it into fullscreen mode, or when you put powerpoint into presentation mode.
In a windows form you do that by putting the border style to none and maximizing the window. But sometimes the window won't cover the taskbar for some reason. Half the time it will.
If I have the main window topmost, the others will fall behind it when I click on it, which I don't want if the taskbar is hidden.
Try this (where this is your form):
this.Bounds = Screen.PrimaryScreen.Bounds;
this.TopMost = true;
That'll set the form to fullscreen, and it'll cover 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.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F11)
if (FormBorderStyle == FormBorderStyle.None)
{
FormBorderStyle = FormBorderStyle.Sizable;
WindowState = FormWindowState.Normal;
}
else
{
SuspendLayout();
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
ResumeLayout();
}
}
As far as I know, the taskbar is either above or below windows based on the "Keep the taskbar on top of other windows" setting. (At least, that's the wording in XP.) I suppose you could try to see if you can detect this setting and toggle it if needed?
Try resizing the form and bringing it to the front of the z-order like so:
Rectangle screenRect = Screen.GetBounds(this);
this.Location = screenRect.Location;
this.Size = screenRect.Size;
this.BringToFront();