How to disable all resize possibilities for a window? - c#

I have a line of code to forbid all possibilities to resize the window of my application.
this.ResizeMode = ResizeMode.NoResize;
Through this the maximize and minimize button on the top right of the window are not shown and you cannot maximize the window by double clicking the menubar.
I thought it works but then I found out that you can minimize the window with a double click on the menubar. I was very confused that this is possible. Has someone an answer to this question that you can not maximize the window with the double click but it is possible to minimize it?

You can easily prevent minimization my detecting the change with the Resize event and setting it back to its original size.
NOTE: You will see the window minimize briefly and then come back.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Resize += new EventHandler(Form1_Resize);
}
void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
}
}
}

Related

Restored WPF Window

I have an option to start my mainwindow minimized. The window is set to SizeToContent="WidthAndHeight". when I click to restore the window it opens larger than the initial WidthAndHeight. I use the following in the MainWindow.xaml.cs
private void WindowStart()
{
if (LocalSystem.StartMinimized)
{
WindowState = WindowState.Minimized;
ShowInTaskbar = true;
}
if (LocalSystem.StartOnTop)
{
Topmost = true;
}
Activate();
}
Consider keeping track of the state change of your WPF window. Add an event handler of the StateChanged event of your Window and keep track of the Window width and height.
You could keep track of the size of the window like this:
public MainWindow() {
InitializeComponent();
_lastState = this.WindowState;
_initialWidth = this.ActualWidth;
_initialHeight = this.ActualHeight;
this.StateChanged += Window_StateChanged;
}
WindowState _lastState;
double _initialwidth, _initialHeight;
private void Window_StateChanged(object sender, EventArgs e)
{
if (this.WindowState != _lastState)
{
if (_lastState == WindowState.Minimized && this.WindowState == WindowState.Maximized){
this.Width = _initialWidth;
this.Height = _initialHeight;
}
_lastState = this.WindowState;
}
}
Note the use of ActualHeight and ActualWidth. The Window actual dimensions will be known after InitializeComponent is run inside its constructor. A restored or unminized, as you so eloquently put it, window is window where the State goes from WindowState.Minimized to WindowState.Maximized.
Note that we try to minimize the amount of code behind in WPF apps, as a best practice is to use MVVM. You could though consider creating a WPF behavior class for restoring initial size of Window, after all it is a generic behavior.

Winforms taskbar hide/overlay

I m trying to solve problem in my fullscreen application. When I start the app it is fullscreen. That is handled by this code
public MainForm()
{
//this.TopMost = true;
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
}
private void Hlavni_Load (object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
//this.Bounds = Screen.PrimaryScreen.Bounds;
}
When opening another fullscreen Form, everything works fine. Taskbar is cover with and doesn't show up. The problem occurs when I want to open 3rd form. This one is not meant to be fullscreen and serves for purpose similar to MessageBox.
While is this form open, taskbar is also visible untill the form is closed again. I tried somehow to update/change bounds of that form but it only led to making this form fullscreen.
Any idea how to show desired form and keep taskbar hidden?
Thanks in advance
EDIT
Adding the code for 3rd form
public RadForm3()
{
InitializeComponent();
//this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
this.ShowInTaskbar = false;
}
private void RadForm3_Load(object sender, EventArgs e)
{
//this.Bounds = Screen.PrimaryScreen.Bounds;
this.CenterToScreen();
...
}

c# make a program minimize to task bar

I am trying to make my application minimize to task bar/tray
Here is my code so far that I have pulled together from other SO posts and other people seem to have it working but my app minimizes to the tray but when I click it in the tray it doesn't reopen.
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.notifyIcon1.Visible = true;
this.notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
just to explain again the problem is the application minimizes to the tray but when I click on the icon it doesn't restore the app to normal. Instead it does nothing.
I found my problem.
what I didn't do was this
Set the NotifyIcon's visible property to false in the property editor. Now go to the property editor of Form1, click on the little lightning symbol to access the events, and double click on the Resize event, and change the code to:
and I also didn't do this
Finally we need the code to make the program show up again when the icon is double clicked. So double click on NotifyIcon1 in the designer,
I found this information here
Dreamincode
This is what I have used in the past but I fund a copy online at the link below
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
found here: http://www.codeproject.com/Articles/27599/Minimize-window-to-system-tray

How to trigger the event associated with maximize in C#

Consider the following code:
Window myWindow = new MyWindowSubclass();
myWindow.BringIntoView();
myWindow.Show();
// Code which is effective as pressing the maximize button
Also, how to detect if the window is indeed in maximized state.
In WPF, you can use the WindowState property:
myWindow.WindowState = WindowState.Maximized;
You can of course query that property to obtain the current window state:
if (myWindow.WindowState == WindowState.Maximized) {
// Window is currently maximized.
}
For WinForms, you can use
bool maximized = this.WindowState == System.Windows.Forms.FormWindowState.Maximized;
to test if the window is maximized.
The SizeChanged and Resize events should capture all changes to the window state.
In WinForms, do
// Code which is effective as pressing the maximize button
myWindow.WindowState = FormWindowState.Maximized;
Of course you can test it the same way:
if (myWindow.WindowState == FormWindowState.Maximized) { ... }

How to start WinForm app minimized to tray?

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...

Categories

Resources