Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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() { }
    }
like image 969
sidney.andrews Avatar asked Jun 06 '11 18:06

sidney.andrews


2 Answers

Use ResizeMode="CanMinimize". This will allow you to minimize to the taskbar.

like image 127
gaurav Avatar answered Oct 21 '22 02:10

gaurav


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();
        }
    }
 }
like image 26
Rachel Avatar answered Oct 21 '22 02:10

Rachel