Minimized application showing above the task bar - c#

I have an application that I do not want to show in the taskbar. When the application is minimized it minimizes to the SysTray.
The problem is, when I set ShowInTaskbar = false the minimized application shows above the task bar, just aboe the Windows 7 start button. If I set ShowInTaskbar = true the application minimizes correctly, but obviously the application shows in the taskbar.
Any idea why this is happening and how I can fix it?
EDIT: For the sake of clarity, here is the code I'm using:
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized) {                                                                                    
this.Hide();
this.Visible = false;
notifyIcon1.Visible = true;
     }
else
{
notifyIcon1.Visible = false;
}
}
private void btnDisable_Click(object sender, EventArgs e)
{
// Minimize to the tray
notifyIcon1.Visible = true;
WindowState = FormWindowState.Minimized; // Minimize the form
}

There is no event handler to establish when a form's minimise request has been fired. So to 'get in' before the form has been minimised you'll have to hook into the WndProc procedure
private const int WM_SYSCOMMAND = 0x0112;
private const int SC_MINIMIZE = 0xF020;
[SecurityPermission(SecurityAction.LinkDemand,
Flags = SecurityPermissionFlag.UnmanagedCode)]
protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case WM_SYSCOMMAND:
int command = m.WParam.ToInt32() & 0xfff0;
if (command == SC_MINIMIZE && this.minimizeToTray)
{
PerformYourOwnOperation(); // For example
}
break;
}
base.WndProc(ref m);
}
and then you can merely hide the form in the PerformYourOwnOperation(); method
public void PerformYourOwnOperation()
{
Form form = GetActiveForm();
form.Hide();
}
You will then need some mechanism to Show() the hidden form otherwise it will be lost.
Another way is using the the form's resize event. However, this fires after the form is minimised. To do this you can do something like
private void Form_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
// Do some stuff.
}
}
I hope this helps.

My guess is that you also have to hide the window. To get this behavior in WPF, I have to do the following:
WindowState = WindowState.Minimized;
Visibility = Visibility.Hidden;
ShowInTaskbar = false;
Since WPF and WinForms both ultimately come down to Win32 windows, you probably have to do the same thing.

Here is one of the simplest solutions (I think so):
//Deactivate event handler for your form.
private void Form1_Deactivate(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized) Hide();
}

You can use a NotifyIcon to get the program to show up in the system tray, and watch for the window's resize event to toggle the visibility to hidden.
Here is how to watch for the window's resize event.
How to detect when a windows form is being minimized?
Here is a tutorial for using the NotifyIcon provided by CodeProject. The NotifyIcon is a windows forms element so it will still work in your application.
http://www.codeproject.com/Articles/36468/WPF-NotifyIcon

Add handler for resizing:
private void Form1_Resize(object sender, EventArgs e)
{
Visible = WindowState != FormWindowState.Minimized;
}

Related

Parts of the form disappear when WindowState is set to maximized

This problem is happening when the FormBorderStyle = None and a button is used to set the WindowState. Chunks of the form turn invisible when the form is restored from a minimized state or is maximized from a normal state.
The code for maximizing and minimize:
private void MaximizeButton_Click(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Normal)
{
WindowState = FormWindowState.Maximized;
}
else
{
WindowState = FormWindowState.Normal;
}
}
private void minimize_button_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
}
I have failed to recreate this glitch in any other winform based application of mine.
I appreciate the help , hope you all are having a comfortable covid stay.
Simply refresh the window once its state becomes maximized. Unlike WPF, Winforms doesn't have a StateChanged event, so use SizeChanged to detect if the window it's maximized, and refresh it.
private void Window_SizeChanged(object sender, EventArgs e)
{
if(WindowState == FormWindowState.Maximized)
Refresh();
}

Why closing event not working after resuming wpf application from system tray?

My problem is, I am hiding my app in system tray when close event is fired,
User can resume it by double clicking on notify icon. Close event only works for the first time in my wpf app.
private void SparkWindow_Closing(object sender, CancelEventArgs e)
{
DialogResult dr = MessageBox.Show("Do you really want to close application?","Confirmation",MessageBoxButton.YesNo,MessageBoxIcon.Question);
if(dr==DialogResult.No)
{
e.Cancel = true;
this.Hide();
notifyIcon.Visibility = System.Windows.Visibility.Visible;
}
else
{
Application.Current.ShutDown();
}
}
this is how i handle the closing event.
and below given code is used to bring it back in front of from system tray.
private void OnNotifyIconDoubleClick(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
this.Show();
this.WindowState = this.lastWindowState;
this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
}
}
but when it resumes it doesn't fire the closing event 2nd time.

c# reshow window by click on notifyicon

I'm new with c# but know c++ and c, my problem is that I can't get my form to show up again after it got minimized to the system tray.
That's the code I used to hide it:
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
bool cursorNotInBar = Screen.GetWorkingArea(this).Contains(Cursor.Position);
if (this.WindowState == FormWindowState.Minimized && cursorNotInBar)
{
this.ShowInTaskbar = false;
notifyIcon.Visible = true;
this.Hide();
}
}
You need to undo the changes you made to the form to make it hide...to make it display again:
private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
this.Show();
this.ShowInTaskbar = true;
this.WindowState = FormWindowState.Normal;
notifyIcon.Visible = false;
}
}
on notifyIcon click event try:
this.Show();
(I'm not sure but this one could work too: this.Visible = true)
by the way try to handle OnClosing event on your form instead of OnResize
(I have suitable code in home, when i got there ill share it)
You can use this.Show() to show the form again after minimized. If this is not working, just tell me, will provide another solution.

C# Minimize to Tray

I know you will be thinking "Not again this question", as I found like a hundred results when I searched for it. But when I put in the code as described on the pages here, it just minimizes to right above the start menu.
This is the code I use (I added a message box to see if the code gets triggered, but the message box never pops up):
private void Form1_Resize(object sender, EventArgs e)
{
MessageBox.Show("Works1");
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
Because I don't know if it links to Form1 or Form, I have tried both, to no avail.
private void Form_Resize(object sender, EventArgs e)
{
MessageBox.Show("Works");
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
Now, when you double click on the Form, it puts this line in the Form1.Designer.cs:
this.Load += new System.EventHandler(this.Form1_Load);
Do I need a similar line to trigger the minimize event?
As you can see, I am completely lost :)
Oh, and it doesn't minimize to the taskbar, as I am using the following code to hide the form on run:
protected override void OnLoad(EventArgs e)
{
Visible = false; // Hide form window.
ShowInTaskbar = false; // Remove from taskbar.
base.OnLoad(e);
}
You need the event
private void Form1_Resize(object sender, EventArgs e)
{
}
Creating Event Handlers on the Windows Forms Designer
Add a NotifyIcon component to your Form. Make sure you set an icon via the properties pane otherwise it will be invisible.
Create an event handler for the form's Control.SizeChanged event. In that event handler place the following code:
sample code:
private void MainForm_SizeChanged(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
ShowInTaskbar = false;
}
And then to make the form visible again the NotifyIcon.MouseDoubleClick event handler you can place the following code:
private void trayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
WindowState = FormWindowState.Normal;
ShowInTaskbar = true;
}
The basic thing you need to know is events. Events are triggered when certain things happen to your form (or any control). For example, when the form is resized, or loaded, or clicked, an event is raised. You can hook into this event to execute your own code when the event happens.
In your case you want to execute code to minimize the form, on the event that the form is resized. So you need to hook your method to the resize event. The name of your method is not relevant, so let's use a better name:
private void HideWhenMinimized(object sender, EventArgs e)
{
MessageBox.Show("Works1");
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
To hook your HideWhenMinimized method into the Resize event of the form, you have to do it like this:
this.Resize += new System.EventHandler(this.HideWhenMinimized);
If you add that line of code in the form's constructor or Load event, then your code gets called as soon as the form is resized.

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