Custom window style with minimized animation

I wanted to have a custom window to follow a few tutorials that allow this by setting the window style to none, and then adding the title / restore / minimize / close buttons myself. Minimization is achieved by simply accessing the click event and smoothing the state of the window, but this does not show the minimization animation that you see in Windows 7, and just hides the window, which seems very strange when used with other windows that expect, since you tend to feel the closure of the application.

So, is there a way to enable this animation? .. It seems to be disabled when you change WindowStyle to none.

Edit: test code

public partial class MainWindow : Window
{
    public MainWindow()
    {
        WindowStyle = WindowStyle.None;
        InitializeComponent();
    }

    [DllImport("user32.dll")]
    static extern int SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        base.OnMouseLeftButtonDown(e);

        // this doesnt seem to animate
        SendMessage(new WindowInteropHelper(this).Handle, 0x0112, (IntPtr)0xF020, IntPtr.Zero);
    }

    protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
    {
        base.OnMouseRightButtonDown(e);

        WindowStyle = WindowStyle.SingleBorderWindow;
        WindowState = WindowState.Minimized;
    }

    protected override void OnActivated(EventArgs e)
    {
        base.OnActivated(e);

        Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() => WindowStyle = WindowStyle.None));
    }
}
+5
4

, .

: 1. :

private void Button_OnClick(object sender, RoutedEventArgs e)
{
    //change the WindowStyle to single border just before minimising it
    this.WindowStyle = WindowStyle.SingleBorderWindow;
    this.WindowState = WindowState.Minimized;
}

private void MainWindow_OnActivated(object sender, EventArgs e)
{
    //change the WindowStyle back to None, but only after the Window has been activated
    Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() => WindowStyle = WindowStyle.None));
}

- , .

2. , WM_SYSCOMMAND SC_MINIMIZE , (HwndSource.FromHwnd(m_hWnd).AddHook(WindowProc)).

internal class ApiCodes
{
    public const int SC_RESTORE = 0xF120;
    public const int SC_MINIMIZE = 0xF020;
    public const int WM_SYSCOMMAND = 0x0112;
}

private IntPtr hWnd;

[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);


private void Window_Loaded(object sender, RoutedEventArgs e)
{
    hWnd = new WindowInteropHelper(this).Handle;
    HwndSource.FromHwnd(hWnd).AddHook(WindowProc);
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    SendMessage(hWnd, ApiCodes.WM_SYSCOMMAND, new IntPtr(ApiCodes.SC_MINIMIZE), IntPtr.Zero);
}

private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    if (msg == ApiCodes.WM_SYSCOMMAND)
    {
        if (wParam.ToInt32() == ApiCodes.SC_MINIMIZE)
        {
            WindowStyle = WindowStyle.SingleBorderWindow;
            WindowState = WindowState.Minimized;
            handled = true;
        }
        else if (wParam.ToInt32() == ApiCodes.SC_RESTORE)
        {
            WindowState = WindowState.Normal;
            WindowStyle = WindowStyle.None;
            handled = true;
        }
    }
    return IntPtr.Zero;
}

, . , , , . , , .

+6

.NET . WindowStyle = "SingleBorder" "ThreeDBorder". ResizeMode = "CanResize".

XAML

<Window>
  <WindowChrome.WindowChrome>
    <WindowChrome GlassFrameThickness="0" CornerRadius="0" CaptionHeight="0" UseAeroCaptionButtons="False" ResizeBorderThickness="7"/>
  </WindowChrome.WindowChrome>
</Window>

, . , .

WindowStyle = "None". , . AllowsTransparency = "True" .

+4

WM_NCCALCSIZE, 0, WM_NCHITTEST, ( ), 0, WindowStyle SingleBorder, , .

, WM_GETMINMAXINFO, - , - SingleBorder.

+1

, AllowTransparency = True. , . . , , // , , widht, , . , , , YourWindow - WindowStyle None AllowTransparency True. Window Style SingleBorderWindow AllowTransparency = false. ( , ). . , , . Fake Window (BackgroundColor ..) YourWindow, .

public partial Class YourWindowClass : Window
{

    Window w;
    public YourWindowClass()
    {
        InitializeComponent();
        w = new Window();
        w.Width = Width;
        w.Height = Height;
        w.WindowStartupLocation = this.WindowStartupLocation;           
    }

:

 private void YourWindowClass_StateChanged(object sender, EventArgs e)
    {
        w.Left = Left;
        w.Top = Top;
        w.Width = Width;
        w.Height = Height;
        w.Show();

        if (WindowState == WindowState.Minimized)
        {
            if (w.WindowState == WindowState.Minimized) w.WindowState = WindowState.Normal;
            w.WindowState = WindowState.Minimized;
            CloseWindow();
        }
        if (WindowState == WindowState.Normal)
        {
            w.WindowState = WindowState.Normal;
            w.Left = this.Left;
            Activate();
            CloseWindow();

        }
        if (WindowState == WindowState.Maximized)
        {              
            w.WindowState = WindowState.Maximized;
            Activate();
            CloseWindow();
        }   
    }

, async YourWindowClass. , .

    public async Task CloseWindow()
    {
        await Task.Delay(600);
        w.Visibility = Visibility.Hidden;
    }

, , , . , , , . , :

    private void YourWindowClass_Closed(object sender, EventArgs e)
    {
        w.Close();
    }
0

Source: https://habr.com/ru/post/1524176/


All Articles