Daily Tip: How to Activate Minimized Window (Form)
This solution can be used in WPF and in WinForms (with some improvements).
Many times UI programmer needs to activate window, the activation can be done by using “Activate()” function in window. The problem is that this function will not show window if it is minimized. In that case you can use this workaround:
using System;
using System.Windows;
namespace Test
{
public partial class MyWindow
{
/// <summary>
/// Gets Previouse Window State
/// </summary>
public WindowState PreviouseWindowState { get; private set; }
/// <summary>
/// Constructor
/// </summary>
public MyWindow()
{
InitializeComponent();
// store 1st value
PreviouseWindowState = WindowState;
// attach to event (used to store prev. win. state)
LayoutUpdated += Window_LayoutUpdated;
}
/// <summary>
/// Occures on layout change
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_LayoutUpdated(object sender, EventArgs e)
{
PreviouseWindowState = WindowState;
}
/// <summary>
/// Activates Window
/// </summary>
/// <param name="restoreIfMinimized">if [true] restore prev. win. state</param>
/// <returns></returns>
public bool Activate(bool restoreIfMinimized)
{
if (restoreIfMinimized && WindowState == WindowState.Minimized)
{
WindowState = PreviouseWindowState == WindowState.Normal
? WindowState.Normal : WindowState.Maximized;
}
return Activate();
}
}
}
As you can see I added new overloaded function “Activate(bool)” that allows activation of minimized window, thanks to new property “PreviouseWindowState” that stores previous value of “WindowState” when “Window_LayoutUpdated” in called.
Source Code: Window.cs
Remarks: Any comments/improvements will be accepted with pleasure. Of course, you must understand that this code is a contribution and I’m not responsible for any damage that may be caused by using it. All rights reserved ®.