Set window proportions - c#

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

Related

Why after applying Canvas.Left to my Image it goes out from the canvas? (In WPF)

my canvas is 1920*1080 and I'm adding an image that its width is 200 and height is 200 too,
I want the image to go to the top right corner, and after setting the following code and running the application
I see that the image overflows the canvas:
Image img = new Image();
img.Source = SOURCE;
img.Width = 200;
img.Height = 200;
screen.Canvas.Children.Add(img); // I have already created screen obj and a canvas before
img.SetValue(Canvas.LeftProperty, 1720);
img.SetValue(Canvas.TopProperty, 0);
after I checked it manually, I saw that if I put the Canvas.Left 1350 it fit the target,
but I can't understand why it happens
...................
And about the canvas parent:
it's in the window and I maximize the window at startup also I'm using the code below in its window constructor to maximize the canvas too:
var resolution = Screen.PrimaryScreen.Bounds;
Canvas.Width = resolution.Width; // i put the name of the canvas, Canvas :)
Canvas.Height = resolution.Height;
.................
another update:
I find out however I get the screen size 1920x1080 but the real size is 1536x864 so can anyone help how to get the real screen size? :)
So the problem was I wasn't setting the actual width and height for my canvas,
when I use the resolution width and height, what I get is 1920x1080 but also I checked in my display setting that I have 125% scale so it makes the size 1536x864 so I got this number with RenderSize:
screen.Canvas.Width = screen.RenderSize.Width;
screen.Canvas.Height = screen.RenderSize.Height;
And also it's worth mentioning that if you use it in the constructor the RenderSize will be 0
Why my resolution of Thinkpad T450s is 1536px width?

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

Set MinimumSize according the controls residing inside form

Title of my question could be make it look like a duplicate but please read ahead as my problem is a bit different.
I am trying to replicate the minimum size functionality of some popular media players like MPC-HC or VLC where when you try to make it small the minimum size it achieves is when only MenuStrip and Player Controls are visible.
The code I've written to attain this is:
public NewMain()
{
InitializeComponent();
int ClientTop = RectangleToScreen(ClientRectangle).Top;
int height = menuStrip1.Height + panel1.Height + ClientTop - Top;
label4.Text = height.ToString();
MinimumSize = new Size(373, height);
}
The problem is that when it runs, its not working perfectly and the menuStrip1 is still getting blocked a little at bottom from the panel1 (Docked at bottom) where the player controls will be placed.
Below is the Image of what I was able to attain with above code.
Next Image is what I expected:
Note that label on left updates when resize the form and the label on the right is the determined height via code.
My Idea was to add the difference of Form's Top and the Top of total rectangle visible on the screen i.e. the height of the title bar otherwise the resulting height will be even smaller and hide the menuStrip1 completely. I don't want to hardcode any values because it'll make the interface less adaptable to the changes that I might be doing later on.
To correctly determine the minimum height in this case is to keep the calculations relative which can be attained by:
int height = Height - (panel1.Top - menuStrip1.Bottom);
All credit goes to Hans Passant who provided this code. I'm just posting it as an answer to mark my question solved. Thank you.

Set WPF Window Dialogue Dimensions (Width and Height) Relative To User's Main Window Dimensions

I have a game in WPF, and when the user loses this particular game a window shows containing all the game details and game statistics:
The problem is that the window is set as a fixed size, so if the user would have had a smaller screen it would look like this:
I'm unsure how to make it so the Window fits nicely even in smaller screen dimensions. I'm not sure if this is even possible in WPF - but any help regarding this would be appreciated.
Thanks,
For centering, use Window.WindowStartupLocation and Window.Owner:
//"dialog" being your child window
//"this" being your calling window
dialog.Owner = this;
dialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;
Alternatively, you could set WindowStartupLocation="CenterOwner" in the XAML of your child window instead of in code.
For resizing, you can set your child window to be some percentage of your main window's size, or of the user's screen:
//Child window would 25% the size of the caller.
double scale = 0.25;
dialog.Width = this.Width * scale;
dialog.Height = this.Height * scale;
//Or 25% the size of the screen
dialog.Width = System.Windows.SystemParameters.WorkArea.Width * scale;
dialog.Height = System.Windows.SystemParameters.WorkArea.Height * scale;
Note that SystemParameters.WorkArea gives you the dimentions of the "primary screen", so this might not work right in a multi-monitor setup. If that's a problem you can look at this question.

Window form size adjustment

I need to adjust my form according some size..I want that the form always will be half size of the screen, no matter what the size of the screen.
Always when I put some controller into the form and maximize/change the size of the form all the controllers get crazy and they are not in the order I put..
How can I know what the screen size..?
To get the screen size:
Screen.FromControl(yourForm).Bounds;
or, if you only want the working area only (minus taskbar, docked toolbars, docked windows, etc.):
Screen.FromControl(yourForm).WorkingArea;
There is a class Screen which you can use to get certain display information from. If you want to get the screen you can use:
Rectangle screen = Screen.PrimaryScreen.Bounds;
and then from that you could get the height and width by doing:
int height = screen.Height;
int width = screen.Width;

Categories