WPF Minimize on Taskbar Click
I have a WPF application that by stakeholder requirement must have a WindowStyle="None", ResizeMode="NoResize" and AllowTransparency="True". I know that by not using the Windows chrome, you have to re-implement many of the OS window-handling features. I was able to create a working custom minimize button, however I was not able to re-implement the feature where Windows minimize the application when you click on the Taskbar icon at the bottom of your screen.
The user requirement is such that the application should minimize on taskbar icon click and restore on clicking again. The latter has never stopped working but I have not been able to implement the former. Here is the code that I am using:
public ShellView(ShellViewModel viewModel)
{
InitializeComponent();
// Set the ViewModel as this View's data context.
this.DataContext = viewModel;
this.Loaded += new RoutedEventHandler(ShellView_Loaded);
}
private void ShellView_Loaded(object sender, RoutedEventArgs e)
{
var m_hWnd = new WindowInteropHelper(this).Handle;
HwndSource.FromHwnd(m_hWnd).AddHook(WindowProc);
}
private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if 开发者_如何学运维(msg == NativeMethods.CS_DBLCLKS)
{
this.WindowState = WindowState.Minimized;
// handled = true
}
return IntPtr.Zero;
}
/// <summary>
/// http://msdn.microsoft.com/en-us/library/ms646360(v=vs.85).aspx
/// </summary>
internal class NativeMethods
{
public const int SC_RESTORE = 0xF120;
public const int SC_MINIMIZE = 0xF020;
public const int SC_CLOSE = 0xF060;
public const int WM_SYSCOMMAND = 0x0112;
public const int WS_SYSMENU = 0x80000;
public const int WS_MINIMIZEBOX = 0x20000;
public const int CS_DBLCLKS = 0x8;
NativeMethods() { }
}
Use ResizeMode="CanMinimize"
. This will allow you to minimize to the taskbar.
I have used this code in the past to minimize/maximize Windows using WPF's WindowStyle=None
private void MinimizeButton_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
private void MaximizeButton_Click(object sender, RoutedEventArgs e)
{
AdjustWindowSize();
}
private void AdjustWindowSize()
{
if (this.WindowState == WindowState.Maximized)
{
this.WindowState = WindowState.Normal;
}
else
{
this.WindowState = WindowState.Maximized;
}
}
private void FakeTitleBar_MouseDown(object sender, MouseButtonEventArgs e)
{
if(e.ChangedButton == MouseButton.Left)
{
if (e.ClickCount == 2)
{
AdjustWindowSize();
}
else
{
Application.Current.MainWindow.DragMove();
}
}
}
I just realized that if ResizeMode=NoResize than this happens, if it is equal to CanResize than you do not disable the OS feature to minimize via Taskbar icon click. I'm voting to close this question.
精彩评论