Can't restore minimized WinForms app from taskbar - c#

I have a very strange problem, in which a standard WinForms C# application can sometimes not be restored from the taskbar, after it has been minimized or if another application was used in maximized mode.
This happens on Windows Server 2012 and on Windows 10 (I haven't tested it in other OS'es). If the mouse hovers over my applications taskbar icon, a thumbnail view of my application shows up. Moving the mouse cursor up, to hover over the thumbnail, the application actually shows up on the desktop again, but if I left click on the thumbnail or moves the cursor off the thumbnail, it disappears again. If instead I right click the thumbnail, I can choose "Maximize" or "Restore" from the popup menu, and either works as expected. So the issue only affects regular left clicking on the taskbar icon or the popup thumbnail.
I'm using .NET Framework 4.6 for my application, and the application consists only of a single form with default property values and no WndProc overrides or other fancy things.
I have no idea how to debug this, and the bug is not reproducible 100% of the times, so any suggestions on how I can capture and provide more information to help solve the issue would be very welcome.

For me, the problem was an always visible modeless Form that was owned by the main window. Apparently when the main window is minimized, the modeless form needs to be minimized as well. WinForms doesn't do this automatically for you. I had to subscribe to the main window Resize event, and set the state in there:
private void MainWindow_OnResize(object sender, EventArgs e)
{
modelessForm.WindowState = WindowState;
}
This issue was not encountered on Windows 7, only on Windows 10.

Related

Winforms ShowDialog(this) blocks the switching windows of other applications to the foreground

When I open any dialog in my Winforms application then Windows 10 behaves oddly in these ways:
ALT-TAB no longer displays the list of windows
I cannot switch to hidden applications using taskbar icons - if I select a taskbar icon of a window that is not currently visible (even if it is not hidden by the winforms app) the icon just flashes and then stays highlighted, but is not brought to the foreground. I can use other applications that are visible. As soon as I close the dialog form the other I can use the windows of other applications correctly
If I switch to the application window that is behind the winforms application by clicking on it, I cannot go back to the winforms app either by ALT-TAB or by clicking on the taskbar icon. I can get back to it by minimizing the other application
I'm opening the dialogs with
dialogFormName.ShowDialog(this);
TopMost is set false on all forms and is not set in the code.
I've read about 50 related articles and the only problems seem to be either TopMost is set or ShowDialog is not called with the parent form. I'm not a regular Winforms developer so I'm probably doing something stupid. This is driving me crazy, so I'd really appreciate any help!
Edit: The same issues occur with MessageBox.Show(this, "test"). The issue does not occur with a newly created app just with a single button calling MessageBox.Show(this, "test"). The problem application uses EntityFramework but no other packages and the problem existed before I added EF.
After trying different scenarios I eventually found the issue. In my case I was calling ShowDialog() after a user clicks an item on a ContextMenu. The blocking of ALT-TAB was caused by the following code that attached the ContextMenu to the ListView that the menu was contextually for:
lstList.ContextMenu = myContextMenu;
As soon as I removed that association, the ShowDialog no longer blocked ALT-TAB.
Form.ShowDialog() blocks the parent form until it's closed.
Use Show() to display the form separately without blocking its parent.

Disappearing System Tray icons

Im creating a system tray application in visual studio 2010, using C#.
When the application starts i create my thread and a system tray icon. THe icon shows, however whenever i mouse over the icon, it disappears ( the application is still running ), and even if i click the button to show all hidden icons, it doesnt display.
However, if i dont try to mouse over on it, then it stays their in the system tray.
Any Thoughts or experience?
Thanks in advance
Thanks for the answers guys.
Uhh, something i did to fix before so although for those who are perhaps curious.
I initially wasnt using a windows form, and this is when the problem occured. However when i set my app to be a windows form, and just hide the form, and not show it in the taskbar, it worked.
Paste this code into your form class:
protected override void OnFormClosing(FormClosingEventArgs e) {
notifyIcon1.Visible = false;
base.OnFormClosing(e);
}
This ensures the icon will disappear without lingering in the tray. Now set a breakpoint on that code and find out why your form is closing. Copy and paste the stack trace into your question if you cannot figure out why.
This means that tray icon has been removed. That usually happens after process terminates but the tray stays there - it is a windows bug.
So for some reason, your tray icon perhaps "crashes".
Without seeing your code, it would be impossible to comment any further.
If you are creating the icon object and letting it go out of scope without any reference to it, the next garbage collection will call it's destructor and this will happens.
When the Windows Explorer restarts ,windows will clear all the icons in the notification area and sends a broadcast message TaskbarCreated .One has to use the message to add the notification icon again .
You can use the following code to listen to the event :
UINT WM_TaskBarCreated = ::RegisterWindowMessage(L"TaskbarCreated");
and the use windowproc or MessageHandler to add the icon back in the notifiation area.

How can I use two NotifyIcons in C# without the second one disappearing when i hover the mouse over?

I Have a VS2008 app that needs to display two notifyIcons in the System tray. One is the main application icon, which allows quick access to the GUI. The second is a "new message" icon, which is displayed when a new message arrives, and hidden when the user double-clicks on it, much like the new Outlook mail message icon.
The problem i am having is the second message icon is hidden whenever the mouse is moved over the system tray. The first icon behaves as expected, and remains in the system tray while the application is running.
Any ideas?
Thanks - this was partly due to windows glitch and a bug in my code.
Seems the icon will not stay visible when showing a baloon tip - it must be forced visible before showing the baloon tip. The icon did not refresh until i moved my mouse over the tray.

What event is sent to a Windows Forms window when its button from the taskbar is clicked?

When the user clicks the window title/button in the taskbar, what .NET event (or Windows API message) is sent to the window?
I'm using C#/.NET 2.0/Windows Forms.
Thank you
To discover the messages that are send to a window at various points you should consider using Microsoft Spy++. On my machine it is located at the following path
"D:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\spyxx.exe"
Note I am using 64bit OS hence the (x86).
The type and number of messages is quite big and I am sure varies slightly between OS versions like XP and Windows 7. However the key events/mesages would probably remain pretty constant.
WM_ACTIVATE (When activating and deactivating)
WM_WINDOWPOSCHANGING
WM_WINDOWPOSCHANGED
etc.
Another good tool is ManagedSpy, but I have not tried it on anything other than XP, it does not seem to run on Windows 7 64bit, but I have not done much investigation (Yet!).
http://msdn.microsoft.com/en-us/magazine/cc163617.aspx
The Activated and Deactivate events usually occur when you click on the taskbar button, but not always. They also occur when you just click on the window or click away to another window.
The one time I found that they don't occur is when the window is active, you click on the taskbar button to minimize the app, and Deactivate occurs as well as Activated. Then when you click on the taskbar button to restore the window, the Activated event doesn't fire. This seems weird to me.

Question about an application not getting focus in XP/W2K3

I have an application written in .NET. The previous version had no problems: you double-click on the icon or run it from a command line and when it starts up, it's the main window and has focus as you'd expect.
The latest version displays a splash screen before the main window and now the splash screen comes to the foreground ok but the main one does not always end up the main window. Sometimes it does, sometimes it doesn't. (When launched from the command line it invariably doesn't). When the main window does not come to the foreground and get focus, the taskbar icon shows as a steady orange.
I see lots of hits on the net about how MS added a facility to prevent applications stealing focus from others, centered around the ForegroundLockTimeout registry setting and related settings, but the behaviors described above for the different versions happen on the same machine.
I have tried called Activate in the main form when it finally gets created, and also SetForegroundWindow, all to no avail.
Any help is appreciated.
You should probably have the splash screen set focus to the main app window as it is going away.
As far as Window's knows your splash screen IS your app since it's the first toplevel window shown after the process is started. So that window gets the focus, but any OTHER window trying to grab focus on that same app startup (the icon click/run command) is believed to be a focus thief.
You can get around this by having the window that Window's believes has a right to have focus be the one to transfer focus to a new window.
So you splash should SetFocus on the main window BEFORE the splash is destroyed. If you destroy the focus window, then the focus is nowhere, which is probably what is happening currently in your app.
Sounds like your main window doesn't always get the focus back from the splash screen. Have you tried simply calling SetFocus in the main form's OnLoad handler?MSDN
I just corrected a similar problem. Just make sure your splash screen closes after your main form as shown (it will show in background of the splash if your splash is "always on top").

Categories