Window form size adjustment - c#

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;

Related

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.

C# Winforms: rezising keyboard as user control, how to keep keys sizes relative to the control container?

So we wanted to have a custom keyboard on a winforms (it will run on a touch screen computer) so I quickly made one as a Custom UserControl so we can later re-use it if necessary. Here's how it looks now:
We'd also want it to scale if we'd have to resize it in the future. How can I make sure, when I resize my main UserControl (the keyboard itself, not any of the keys), the buttons sizes will all stay relative? So for example if I resized the keyboard to be 25% bigger (length & width), all my buttons would also have to resize that amount. So there'd have to be some empty space left between the buttons as you can see in the previous image.
Right now I've tried playing with the Anchor & Dock properties of the buttons and the UserControl, autosize and their size but I couldn't find the correct configuration that I wanted, unfortunately.
Does anyone know how I can resize my custom UserControl keyboard, while also relatively resizing my buttons inside it?
EDIT: So for example, if I increased the height of the entire UserControl by 100% (say 100px -> 200px), I'd want my 3 buttons "Tab", "Caps Lock" and "Shift" to now also fill those 200px in height, so "stretch" them for example. Same for width. So if I'd change the UserControl by 50% height & width, I'd want my buttons to change as well relatively to howmuch my UserControl has changed.
So if width was changed +100%, I'd still want it filled horizontally with all my buttons. If only width/height is change it will stretch of course.
Source code for this thing would be helpful
But without it:
Did you use row containers to keep controls in place automatically?
If yes,
On control resize, size top docked row and bottom docked row to 1/3 of the total height
And on row container resize, each control in the container should update its width according to its parent height
You probably have to implement this resize functionality manually, because Windows Forms is not really built to support this very well out of the box. Let's say the default size of the whole is W and H.
Now, when the app first loads, you can save the default sizes of all buttons and their locations into a List:
public class DefaultButtonLayout
{
public Button Control {get;set;}
public int X {get;set;}
public int Y {get;set;}
public int Width {get;set;}
public int Height {get;set;}
}
So on first load you will generate the list of defaults and then in the Resize event handler you will first calculate the factor of change of size of the keyboard like xFactor = newW * 1.0 / W and yFactor = newH * 1.0 / H and subsequentely go through the list and set the Control properties:
Control.Left = ( int )( default.X * xFactor );
Control.Top = ( int )( default.Y * yFactor );
Control.Width = ( int )( default.Width * xFactor );
Control.Height = ( int )( default.Height * yFactor );

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

Show only small area of the video with directshow

I have a video graph in c# with DirectShow.
Now I want to show all video sources with its preview. But it should not adjust the video area to the size of the panel.
Currently it shows me the video on the panel, but it adjust the size of the video proportional into the panel.
I want to show only a single area of the video in this panel. For example this picture: http://www.cnet.de/i/story_media/41557373/weitwinkel.jpg
If this would be my video and the smallest area on it would be the size of my panel. I don't want to fit the whole video in my panel size, it should only show a small part of the video.
My code is:
//get the video window from the graph
IVideoWindow videoWindow2 = (IVideoWindow)_graph;
//Set the owner of the videoWindow to an IntPtr of some sort (the Handle of any control - could be a form / button etc.)
int hr = videoWindow2.put_Owner(panel.Handle);
panel is of type Panel.
The solution is to use SetWindowPosition of IVideoWindow.
//get the real video width
hr1 = videoWindow2.get_Width(out videoWidth);
DsError.ThrowExceptionForHR(hr1);
//get the real video height
hr1 = videoWindow2.get_Height(out videoHeight);
DsError.ThrowExceptionForHR(hr1);
//calculate the width when setting the height to the panel height
videoWidthF = (float)videoWidth;
videoHeightF = (float)videoHeight;
panelWidthF = (float)panelWidth;
panelHeightF = (float)panelHeight;
// calculate the margins
int margin = (int)(((panelHeightF / videoHeightF*videoWidthF) - panelWidthF) / 2);
// Position video window in client rect of main application window
hr1 = videoWindow2.SetWindowPosition(-margin, 0, (int)(panelHeightF / videoHeightF * videoWidthF), panel.Height);
Take a look at using windowless mode of the VMR.
IVMRWindowlessControl9::SetVideoPosition is what you are looking for. A quick google search would provide samples.

Categories