How can I get WPF go fullscreen - c#

when I click the btnFullScreen it doesn't go full screen, but when I resize the mainWindow and then click btnFullScreen it goes full screen, how can I make it go full screen without resizing it first?

is there any specific reason why you are setting WindowState, WindowStyle twice?
Just use
mainWindow.WindowState = WindowState.Maximized;
mainWindow.WindowStyle = WindowStyle.None;
once And Kaboom!!! You are Done.

Related

App opens maximized when it should not

I have a C# winforms application with a fixed-size starting dialog. After making some changes entirely unrelated to this form, the form now opens maximized, filling the entire screen with the actual content still at the regular size in upper left corner. Dragging the title bar down a bit with the mouse causes it to restore to its appropriate size.
The form has
MaximizeBox = False
StartPosition = CenterScreen
FormBorderStyle = Fixed3D
Does anyone have any suggestions what might be causing it to open maximized, and how to stop it?
Edit:
WindowState = Normal
is also set.
Edit 2:
I found the problem. My update routine was shutting down the original process so it could be updated (inappropriately, but that is a different bug), then restarting it with code I had borrowed from elsewhere, and part of that restart code was:
process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
Thank you for the assistance. Though no direct answer was given (nor expected with the spotty information), it did point me in the right direction.
The Window being Maximised is normally set on the forms "WindowState", Light Reading. You need this to be set to WindowState.Normal. It's possible someone's set it to maximised in the designer? or somewhere else in the code. the 3 lines of code you have shown are not the problem.
Edit:
You need to do some investigation if you cant give us code from your form, Hook into some resize events, and see what's triggered it off. Forms dont just start up maximised unless they're told to and maximizing a window triggers a resize event.

Winforms - Disable that when user drags form by the border to move it, it resizes the form

I have a Winforms project with a form in it that is maximized (this.WindowState = System.Windows.Forms.FormWindowState.Maximized).
When the user drags the form by its border to move the form around, it resizes to the size that I have set (which is not the maximum screen size since I do not know exactly to which values to set the MinimumSize property for it to be maximized on every screen).
I do not want this behaviour. I just want the form to stay maximized. I have set the FormBorderStyle to FixedDialog, so that the user can not resize the form by dragging the borders. I have tried to re-set the maximized window state in all kinds of events, but they do not seem to work.
Does anyone know how to fix this?
This might do the trick for you
this.MinimumSize = this.MaximumSize;
this.SizeGripStyle = SizeGripStyle.Hide;
and you can also try to write onResize event of the form
this.WindowState = System.Windows.Forms.FormWindowState.Maximized
Change the FormBorderStyle to one of the Fixed values: FixedSingle, Fixed3D, FixedDialog or FixedToolBar
// Define the border style of the form to a dialog box.
this.FormBorderStyle = FormBorderStyle.FixedDialog;
// Set the MaximizeBox to false to remove the maximize box.
this.MaximizeBox = false;
// Set the MinimizeBox to false to remove the minimize box.
this.MinimizeBox = false;
Have you tried
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
in combination with
this.WindowState = System.Windows.Forms.FormWindowState.Maximized
When you set this, you can't drag or resize the form.
However you will need to make your own close button or use "Alt+F4" to close the window.
Try setting the size, maximumsize and minimumsize to the same value.

Strange effect of the form minimization in Windows Forms

I have my own minimize button with the following code on the Click event:
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
this.notifyIcon.Visible = true;
Sometimes when I click on it, the form appears in the left bottom corner of the screen like this:
Why? What am I doing wrong? How can I fix it?
Thanks in advance.
It is a counter-measure against you setting ShowInTaskbar to false. The user won't be able to restore it so Windows makes it a minimum size window instead of hiding it. Otherwise without paying attention to the fact that you've prevented that from working as well by making it a borderless window.
You need to call Hide() instead.
According to this link
you need to add
this.FormBorderStyle = FormBorderStyle.None;
to your minimization event.

Preventing Winform from being maximized?

I want to prevent my desktop application from being maximized. It should not become maximized by any means - by double clicking on title bar, or by clicking Windows + Up arrow on the keyboard, etc.
-> I disable both the MaximizeBox and MinimizeBox.
-> I also set the MaximumSize and MinimumSize for my WinForm
Still when I press Windows + Up arrow, my win form Shifts to top left of the screen, I mean it gets maximized.
So please tell me any way to prevent this thing happening...
There are actually two completely separate issues at work here. Granted, they look basically the same from a functional perspective, but from an implementation-model perspective (as Windows itself would naturally use) they are unrelated.
As others have answered, you can prevent your form from being maximized by setting its MaximizeBox property to false. This removes the WS_MAXIMIZEBOX style on the form's underlying window, which prevents the window manager from allowing the window to be maximized. It disables the maximize box on the window's caption bar (the one between the minimize box and the close box), disables the "Maximize" option on the window/system menu, and any other methods of maximizing a window that I may have forgotten.
However, this has nothing to do with the Win+↑ keyboard shortcut, which invokes Aero Snap the same as would dragging the window to the the magic position sat the edges of the screen. The window manager, whether as a result of a bug or a feature of Aero Snap, still allows you to effectively "maximize" windows that should not otherwise be maximized. Setting the MaximizeBox property doesn't affect this, so if you truly want to prevent the user from changing the window's size this way, you will need to disable Aero Snap's effect on your window.
Unfortunately, there's no method (at least, not to my knowledge) of programmatically disabling Aero Snap on a per-window or per-process basis (also see this related question). If the user has Aero Snap enabled, you can assume that they want that behavior and applications aren't supposed to tamper with it. The only thing you can do is completely disable resizing your window. In WinForms, you do that by setting the FormBorderStyle property to one of the following: FormBorderStyle.FixedSingle, FormBorderStyle.Fixed3D, or FormBorderStyle.FixedDialog. If you still want your window to be resizable in other ways, you will need to write the code to handle that manually, which is not a particularly easy task.
Thus, I encourage you to very carefully consider whether this is really necessary. All other non-maximizable windows accomplish this simply by setting the MaximizeBox property (or doing the equivalent in their UI framework), which means that they can still be effectively maximized via Aero Snap. If this works for everyone else's windows, including those that are part of Windows itself, it should probably work for you.
The form has a property called MaximizeBox - set this to false.
In regard to your second question, check out this question and it's answers for the best ways to implement keyboard shortcuts in WinForms.
this.FormBorderStyle = FormBorderStyle.FixedSingle;
That line of code will prevent the user from re-sizing the Window.
In addition to that you hide/disable the maximize box and that should do what you asked.
To disable the maximize box use this
this.MaximizeBox = false;
To hide the maximize box use this as well
this.MinimizeBox = false;
If Maximize and Minimize are set to false the buttons disappear.
Setting the MaximumSize equal to the Size (or some size) at least stops the windows from going full-screen. It still snaps to the top left corner but it's still a window at least and looks right - like it's Windows being stupid instead of your program.
You can prevent the windows snapping to the upper left corner by setting:
private void toolbox_Move(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
}
in the move event of the form.
There is a property of the Form class named "MaximumBox" you have to set False in the properties window of your form... This actually will disable the form from being maximized by any way... Also if you want to control your form sizes you can work with such properties as "MinimumSize, MaximumSize" setting their values at your discretion or creating an event handler for the MaximumSizeChanged and MinimumSizeChanged events...
You can try to RegisterHotKey Win+Up if your window or application is activated and unregister this hot key if it is deactivated. You must catch the hotkey and return appropriate value to prevent further processing.
Look at WM_ACTIVATEAPP, WM_ACTIVATE and WM_NCACTIVATE. The first can be used if you want to disable the Win+UP for all your windows.
Set formborderstyle to fixedsingle
Maximizebox=false
Set the maximumsize to the size of winform's default size
Ex: if size(646,385) then set maximumsize(646,385)
One thing you can do is set the MaximumSize and MinimumSize in the same value but not 0,0.
It's easy easy! Apply the folling code to maintain window's state to normal when a resize event is triggered.
protected override void OnResize(EventArgs e) {
base.OnResize(e);
WindowState = FormWindowState.Normal;
}

WPF window pre-loading

When switching between windows in my full screen WPF app, if it is the first time the screen has been shown, The monitor will go black momentarily before the windows is displayed. This only happens the first time the window is shown. Is there a way to pre-load each window so this does not happen?
This is what I've tried:
myWindow.Show();
myWindow.Hide();
but there is a visible flicker.
Then I tried:
myWindow.Height = 0;
myWindow.Width = 0;
myWindow.WindowState = WindowState.Normal;
myWindow.Show();
myWindow.Hide();
myWindow.Height = Screen.PrimaryScreen.Bounds.Height;
myWindow.Width = Screen.PrimaryScreen.Bounds.Width;
myWindow.WindowState = WindowState.Maximized;
No flicker, but when I then shown the window it showed itself in a weird state for a split second then updated to display properly.
I had a similar problem. In the end I created a splash screen and loaded the controls up behind the splash screen in a seperate thread. This stopped the flashing, but made the initail app load longer.
Apparently this person asked a clearer question.
How can I avoid flicker in a WPF fullscreen app?

Categories

Resources