How to set height of the Windows Form forcefully? - c#

I want to set windows form height when I maximize form. I have used Resize Event.
Resize event call automatically when I minimize/restore windows.
Following is my code.
int FormHeight = 260;
private void Form1_Resize(object sender, EventArgs e)
{
this.height = FormHeight;
}
Above code is working fine if size is change when form is open on screen. But when I minimize form, form height is 28px and I can't change it by this.height = FormHeight;
How can I assign this.height forcefully?

Use the MaximumSize property. If you want to restrict the hight only, use it like this:
MaximumSize = new Size(Int32.MaxValue, 260);

You cannot. When the form is minimized, it kind of stops existing for some purposes - for example, it no longer has any client area. That's why the form height is 28px in your case - only the borders actually "exist".
This is just a part of the complex system that handles window rendering - you cannot draw to the form when it's minimized, you can't capture it's contents (remember, there is no client area)...
Raymond has great posts on the topic on his blog - for example, Where did windows minimize to before the task bar was invented? and Why do minimized windows have an apparent size of 160x31?, as well as Obtaining a window's size and position while it is minimized.

Related

How to get size of c# windows forms application at runtime?

When we use this.Width and this.Height to get size of a form they get the width and height before run.
I want to know can i get the width and height of my application (The main form) at runtime? I mean after resizing the form or maximizing the form at runtime?
For example I have a main form with a sub form inside it, and a popupControl1 inside the sub form. So I want to change the size of the popupControl1 according to the size of the main form
i tried to do this:
popupControl1.Width = Convert.ToInt32(this.Width / 1.3);
popupControl1.Height = Convert.ToInt32(this.Height / 1.2);
but it dosn't work because this.Width and this.Height gets the width and height befor running the application, so I may want to change the size of the application at runtime by resizing it or the application may be maximized.
I also replaced this.Width and this.Height with this.Parent.Width and this.Parent.Height to get the size of the main form but they also didn't work because this.parent returned null

How to keep form centered to the middle of the screen after resize

I have a form that is centered according to the screen position that I resize by fontsize when loading. After resizing the location remains the same as it was before resizing so the form is no longer in the center, like I would like.
Let me draw you a sketch:
I've tried calling
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.ResumeLayout(false);
this.PerformLayout();
again after resizing (this is, I believe, the part of the code that centers the form in the beginning). It didn't work. I've also found some similar issues like this:
"Keeping winform control centered after window resize
" but they always only deal with centring a control, not the form itself.
Add method for ResizeEnd event. In method, when ResizeEnd is fired, get current screen size (on multiple monitors, screen that contains current form) and then calculate form's position. Take a look at this example
private void Form1_ResizeEnd(object sender, EventArgs e)
{
Screen myScreen = Screen.FromControl(this);
Rectangle area = myScreen.WorkingArea;
this.Top = (area.Height - this.Height) / 2;
this.Left = (area.Width - this.Width) / 2;
}

How to get real value of Screen Size in C# WPF Application

I am trying to get Screen width and Height of my System on SizeChanged Event.
It works fine in all cases excluding:
Change Screen Size to 1024 X 765 then Run my Application its works fine.
Change Screen Size Again to Another Resolution while my application running
got Width and height of my Previous Resolution not Current.
I have used screen.primaryscreen.bounds.width on Window_SizeChanged Event.
You can monitor the SystemEvents for changes.
using Microsoft.Win32;
Add this to your constructor or the like:
SystemEvents.DisplaySettingsChanged +=
new EventHandler(SystemEvents_DisplaySettingsChanged);
Here's the event handler. I have the data bound to 2 TextBoxes so I could see the values change as I messed with the resolution, including telling it to revert.
void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
{
data.ScreenHeight = Screen.PrimaryScreen.Bounds.Height;
data.ScreenWidth = Screen.PrimaryScreen.Bounds.Width;
}
In your case, just change the assignment to point to where you want to store it.

fullscreen is not always functioning

Good day, I have a winform that can set full screen. But sometimes it's not. It is sometimes 3/4 of the screen but sometimes it is full screen. Below is my Code. Anybody knows why it's not always fullscreen....?
private void Form1_Load(object sender, EventArgs e)
{
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
Width = Screen.PrimaryScreen.WorkingArea.Width;
Height = Screen.PrimaryScreen.WorkingArea.Height;
}
When I run it in my pc. Its fine. But when it is in the production. I saw it did not go well. But sometimes it is ok.
By the way, I want my taskbar to be seen. If I set FormWindowState.Maximized, my taskbar would not be seen.
Thank you.
The issue is that the WorkingArea is not the same with Full Screen. WorkingArea is relative, not the common way of forcing full screen.
There are a couple of ways to attempt to force the full screen.
One of the most popular way to force fill is by setting WindowsState to Maximized (also here and here - quite popular) combined with setting TopMost property to true.
private void Form1_Load(object sender, EventArgs e)
{
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
Alternatively, less popularly, you could also use Screen.Bounds (but may not be absolute too, try to put additional TopMost = true)
And finally, you could also using WorkingArea with conditional statement. To debug and see what is the WorkingArea size in you production PC, you could use GetWorkingArea method. This way, you can debug what is the working area in the production PC and if it is not full screen comparing to the size of the PC, you could force is to full screen. Not so efficient though...
Here is more explanation on working area vs screen area to help you making decisions.
Edit: with bar to be seen, what I could think of as another way to do this is by Get Screen Size. See:
How can I get the active screen dimensions?
Then you should limit the maximum dimension of your form (y/x-axis) such that you have room for your bar - that is, to be little less than the screen size in one of the axis. Then, on form load or other related events, you could control the position of your WinForm such that you won't block your bar.
See StartupPosition property:
C# window positioning
Screen.PrimaryScreen.WorkingArea only gets the screen's working area.
I think you need to change it to
Width = Screen.PrimaryScreen.Bounds.Width;
Height = Screen.PrimaryScreen.Bounds.Height;
You just need to set the WindowState to Maximized at run-time:
WindowState = FormWindowState.Maximized;
Or at design-time, using the WindowState property of the form.

Set window proportions

I would like to set some proportions on a window, like height = half of width. I want to make it adaptive, because I have to consider multiple screen sizes. And I would manage it with a maximum height and width if the window is too big for the screen.
But I've searched on the web and found nothing :D
Edit :
I've tried SizeToContent="WidthAndHeight" but as I have tabs, the window size changes regarding the tabs' content. If possible I would prefer a fixed size.
How to Fetch the Current System Resolution in C#:
int hght = Screen.PrimaryScreen.WorkingArea.Height;
int wght = Screen.PrimaryScreen.WorkingArea.Height;
How to Set the height and width of the Form Size:
On Page_Load() event of the Form write the following code
this.Width = Screen.PrimaryScreen.WorkingArea.Width;
this.Height = Screen.PrimaryScreen.WorkingArea.Height ;
how to develop resolution independent Windows Applications in .NET

Categories