WPF Minimized and Maximized - c#

I would like to have two startup options in my application, to start it maximized, and to start it minimized. No problem here, but I would also like them to have both checked, and in that case I would like it to start minimized, but if the user clicks the application to show it then it should be maximized (cover the whole screen). I thought that if I first maximized it befor minimizing it should stay that way, but thats not the case, here instead it just get minimized, and then when opened it's in the "normal" state.
if (ConfigHandler.Instance.Fullscreen)
this.WindowState = WindowState.Maximized;
if (ConfigHandler.Instance.Minimized)
this.WindowState = WindowState.Minimized;

It's StateChanged event you're looking for.
public MainWindow()
{
InitializeComponent();
if (ConfigHandler.Instance.Minimized)
WindowState = System.Windows.WindowState.Minimized;
this.StateChanged += MainWindow_StateChanged;
}
void MainWindow_StateChanged(object sender, EventArgs e)
{
if (ConfigHandler.Instance.Fullscreen)
WindowState = System.Windows.WindowState.Maximized;
this.StateChanged -= MainWindow_StateChanged;//to prevent further effect
}

Related

Maximizing form on secondary screen with different resolution?

I am developping a simple windows application (in C#) and I want it to display a form that is maximized in my second monitor. To do so, I am doing the following on the "Load" event of the form:
private void FormTest_Load(object sender, EventArgs e)
{
Screen[] screens = Screen.AllScreens;
this.WindowState = FormWindowState.Normal;
this.Location = screens[1].WorkingArea.Location;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
The problem I have is that when I execute this, the form only takes part of the screen. My primary screen has a resolution of 1024x768 and my secondary screen has a resolution of 1920x1080, and it seems that the form is taking the size of the primary screen in my secondary screen.
Also, I have a button that runs the following code to maximize the screen or turn it back to normal:
private void ChangeSize() {
if (this.WindowState == System.Windows.Forms.FormWindowState.Maximized)
{
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
}
else
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
}
}
When I click on the button twice (first to de-maximize the form, and then to maximize it again) the form does cover the entire secondary screen perfectly, but if I try to just run that function in code twice (for the sake of testing) right after the code in "FormTest_Load", the screen will still not cover correctly the entire screen.
I am probably making a noob mistake here but I have struggled with this for some time, so I would really appreciate if anyone can shed some light on what is wrong with my code.
I've tried to get the same result as you describe (changing resolutions, etc), but all works fine in my computer and screens. May be... what is exactly the variable screens? I've assumed that is Screen.AllScreens, like this:
protected override void OnLoad(EventArgs e)
{
Screen[] screens = Screen.AllScreens;
int screenNumber = 1;
this.WindowState = FormWindowState.Normal;
this.Location = screens[screenNumber].WorkingArea.Location;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
As I said, it works fine for me.
Also, you can check the screen settings of your computer: is the size of text and apps set to 100% (recommended value)?

c# override CreateParams and WndProc without form [duplicate]

I've successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs when trying to start with the app minimized. At first the problem was that the app would be minimized but would still appear in the alt-tab dialog. Changing the FormBorderStyle to one of the ToolWindow options (from the "None" option) fixed this, but introduced another problem. When the app first starts the titlebar of the minimized window is visible just above the start menu:
Opening the form and the closing it causes it to hide properly. I've tried lots of variations, but here's essentially how it's working right now...
WindowState is set to Minimized in the Designer. After some initialization in the constructor I have the following lines:
this.Visible = false;
this.ShowInTaskbar = false;
When the NotifyIcon is double-clicked I have the following:
this.WindowState = FormWindowState.Normal;
this.Visible = true;
this.ShowInTaskbar = true;
Like I said, I've tried lots of minor variations on this (this.Hide(), etc.). Is there a way to have the NotifyIcon be the primary component such that I can completely start and dispose of the form while leaving the NotifyIcon running? There's got to be a way to start the app with the form minimized without any of the weirdness. Please help me find it!
The right way to do this is to prevent the form from getting visible in the first place. That requires overriding SetVisibleCore(). Let's assume a context menu for the NotifyIcon with a Show and Exit command. You can implement it like this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
notifyIcon1.ContextMenuStrip = contextMenuStrip1;
this.showToolStripMenuItem.Click += showToolStripMenuItem_Click;
this.exitToolStripMenuItem.Click += exitToolStripMenuItem_Click;
}
private bool allowVisible; // ContextMenu's Show command used
private bool allowClose; // ContextMenu's Exit command used
protected override void SetVisibleCore(bool value) {
if (!allowVisible) {
value = false;
if (!this.IsHandleCreated) CreateHandle();
}
base.SetVisibleCore(value);
}
protected override void OnFormClosing(FormClosingEventArgs e) {
if (!allowClose) {
this.Hide();
e.Cancel = true;
}
base.OnFormClosing(e);
}
private void showToolStripMenuItem_Click(object sender, EventArgs e) {
allowVisible = true;
Show();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
allowClose = true;
Application.Exit();
}
}
Note a wrinkle with the Load event, it won't fire until the main form is first shown. So be sure to do initialization in the form's constructor, not the Load event handler.
I'm reading all the answers and see hacks and black magic... (no offense, mates)
No hacks needed. You don't even have to set "ShowInTaskbar=false" and other stuff. Just do this:
//"Form Shown" event handler
private void Form_Shown(object sender, EventArgs e)
{
//to minimize window
this.WindowState = FormWindowState.Minimized;
//to hide from taskbar
this.Hide();
}
NOTE: I strongly recommend NOT TOUCHING the "ShowInTaskbar" property. For example, if your application registers system-wide hotkeys or other similar stuff (hooks, etc) - setting ShowInTaskBar=false and minimizing your app will prevent Windows from sending some messages to your window... And your hooks/hotkeys/etc will stop working.
In the constructor, remove these two lines:
this.Visible = false;
this.ShowInTaskbar = false;
and add after InitializeComponent();:
this.WindowState = FormWindowState.Minimized;
In designer, set ShowInTaskbar to false & FormWindowState to Normal.
EDIT:
If you post the same in Load event, the window does get minimized but still shows minimized on the desktop. I think this is a bug.
When minimizing an application and you want to hide it from Alt+Tab:
You also need to set the Opacity to stop the titlebar showing near the Start Menu when you set the Border Style to a Tool Window.
On Minimize Event:
this.Visible = false;
this.Opacity = 0;
this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
this.ShowInTaskbar = false;
On Normalize Event:
this.Visible = true;
this.Opacity = 100;
this.FormBorderStyle = FormBorderStyle.FixedSingle; //or whatever it was previously set to
this.ShowInTaskbar = true;
Move the following code from the Form's constructor to Form_Main_Load(). With the same setup on Notification_Icon when Form_Resize().
// Hide the Form to System Tray
this.WindowState = FormWindowState.Minimized;
This "quick and dirty fix" worked for me:
$form1.FormBorderStyle = "fixedtoolwindow"
$form1.top = -1000000
$form1.Left = -1000000
$form1.Width = 10
$form1.Height = 10
$form1.WindowState = "normal"
$form1.ShowInTaskbar = $False
$form1.Opacity = 0
$form1.Hide()
Hope it helps someone else...

How to make a maximize and normal windowstate all in one button

I want a simple way to maximize and normal windowstate all in one button (click me for image)
Method (code) c# coding -
int maxornot;
private void MaxButton_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
maxornot = 1;
if (WindowState == FormWindowState.Minimized);
{
maxornot = 0;
}
if (maxornot == 0);
{
}
}
if this method is pointless and there is a way to simplify the code then leave a code below.
p.s i didn't put much thought into how to get this method to work cause im just having headaches :P
From what you already showed in your code-example you want a button to Switch from FormWindowState.Normal to FormWindowState.Maximized and the other way as well.
Now instead of Setting the FormWindowState of your form to Maximized at the start of your click Even you should first check the current state of your Window:
if(this.WindowState == FormWindowState.Maximized)
... do something
FormWindowState has 3 different states: Normal, Minimized and Maximized. In your case you don't need Minimized. All you have to do now is Switch between normal and maximized in your method depending on what is current active:
if(this.WindowState == FormWindowState.Maximized)
this.WindowState = FormWindowState.Normal;
else
this.WindowState = FormWindowState.Maximized;
This 4 rows of code are all you need in the click event method.
This simple if-else may also be converted to a ternary:
this.WindowState = this.WindowState == FormWindowState.Maximized ? FormWindowState.Normal : FormWindowState.Maximized;

Why Won't Form Get Focus?

I have a form that is launched modally like this:
private void find_street_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
Form findForm = new FindStreet();
findForm.ShowDialog();
this.WindowState = FormWindowState.Normal;
}
The form launches correctly, and the cursor is in the first text box, whose TabIndex is set to 1.
Along with the InitializeComponent(); call, these commands are present.
public FindStreet()
{
InitializeComponent();
this.TopMost = true;
this.BringToFront();
this.Focus();
}
I have looked at and tried a number of examples. The cursor appears in the correct control, but the form's window does not have the focus. The problem is that if a user starts typing, even though the newly launched form is visible, those keystrokes are not going into the text box.
Remove the code in public FindStreet() and in load event of FindStreet add:
this.TopMost = true; //i don't know why you need this.
this.Activate();
When you minimize your main form the next one in z-order get the cursor. this.Focus() doesn't do anything. You need to Activate the dialog.
A dialog requires an owner, that cannot be a minimized window. Now accidents start to happen, starting with your WindowState assignment. Your app doesn't have a window left that can receive the focus so Windows is forced to find another one, that will be one owned by another application. Same problem happens when you close the dialog.
You can still get the intended effect, you must hide your main window after the dialog is displayed, show it again before the dialog closes. That requires a bit of hackorama:
using (var dlg = FindStreet()) {
// Show main window when dialog is closing
dlg.FormClosing += new FormClosingEventHandler((s, cea) => {
if (!cea.Cancel) this.Show();
});
// Hide main window after dialog is shown
this.BeginInvoke(new Action(() => {
this.Hide();
}));
dlg.StartPosition = FormStartPosition.Manual;
dlg.Location = this.Location;
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
// etc...
}
}
And remove the hacks from the FindStreet constructor. Watch out for event order if you have a FormClosing event handler in FindStreet, be sure to override OnFormClosing() instead.
If you want to set a specific control as the current active control then try this:
this.ActiveControl = myTextBox;
This will place the cursor you want as the main focus, when the form loads. So try this out:
public FindStreet()
{
InitializeComponent();
this.TopMost = true;
this.BringToFront();
this.Focus();
this.ActiveControl = myTextBox;
}
Here is the link to Focus() which should explain why your focus call was not working.

Show window after notify icon click

I'm trying to show my form when the user clicks on my notifyicon after the form has been minimized to the system tray. However upon calling Show() the form reappears but is always minimized, i have to click the taskbar icon to un-minimize. Here is my code.
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
Hide();
}
}
private void notifyIcon1_Click(object sender, EventArgs e)
{
Show();
//BringToFront();
}
I've also tried adding BringToFront() but to no avail.
I need noifyIcon click to show the form, but for it not to be minimized afterward.
Set the WindowState property to Normal.
Set your window state back to not minimized. For example :
this.WindowState = FormWindowState.Maximized;
Try adding
this.WindowState = FormWindowState.Normal;

Categories